次の方法で共有


IMessageSink インターフェイス

メッセージ シンクのインターフェイスを定義します。

この型のすべてのメンバの一覧については、IMessageSink メンバ を参照してください。

Public Interface IMessageSink
[C#]
public interface IMessageSink
[C++]
public __gc __interface IMessageSink
[JScript]
public interface IMessageSink

IMessageSink を実装するクラス

クラス 説明
AsyncResult 非同期デリゲートでの非同期操作の結果をカプセル化します。
BinaryClientFormatterSink BinaryFormatter を使用するクライアント フォーマッタ シンクの実装を提供します。
SoapClientFormatterSink SoapFormatter を使用するクライアント フォーマッタ シンクの実装を提供します。

解説

メソッドの呼び出しがプロキシで実行される場合、リモート処理インフラストラクチャは、実際のオブジェクトにリモート処理の境界を超えて引数を渡したり、その引数を指定してそのオブジェクトのメソッドを呼び出したり、その結果をプロキシ オブジェクトのクライアントに返したりするために必要なサポートを提供します。

リモートのメソッド呼び出しは、クライアント側からサーバー側に送信された後、可能な限りクライアント側に返されるメッセージです。リモートのメソッド呼び出しは、途中でリモート処理の境界を超える場合には、 IMessageSink オブジェクトのチェインを通じて渡すことができます。チェイン内の各シンクは、メッセージ オブジェクトを受信し、特定の操作を実行してから、チェイン内の次のシンクに処理を任せます。プロキシ オブジェクトには、チェインを開始するために必要な最初の IMessageSink への参照が格納されます。

非同期の呼び出しでは、各シンクは次のシンクに処理を任せるときに、応答シンク (別の IMessageSink) を提供します。この応答シンクは、応答が返されるときに該当する次のシンクによって呼び出されます。

異なる型のシンクは、受信したメッセージ オブジェクトの型に応じて異なる操作を実行します。たとえば、ロックの取得を発生させたり、セキュリティの呼び出しを強制したり、フロー呼び出し制御や信頼サービスを実行したり、異なる AppDomain 、別のプロセス、別のコンピュータに呼び出しを転送したりするなど、シンクによって実行される操作はさまざまです。チェイン内の複数のメッセージ シンクが、特定の各アクションに関して相互に対話できます。

実装時の注意: 同期呼び出しを非同期呼び出しに変換したり、その逆に変換したりすることが可能であるため、このインターフェイスを実装するコードは、 SyncProcessMessageAsyncProcessMessage の両方の実装を提供する必要があります。シンクが非同期処理をサポートしていない場合でも、これらの両メソッドを実装する必要があります。

使用例

 

Imports System
Imports System.Collections
Imports System.Threading
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Proxies
Imports System.Runtime.Remoting.Messaging
Imports Share


Namespace MyNameSpace

   Public Class MyProxy
      Inherits RealProxy
      Private myUrl As String
      Private myObjectURI As String
      Private myMessageSink As IMessageSink


      Public Sub New(myType As Type, myUrl1 As String)
         MyBase.New(myType)

         myUrl = myUrl1

         Dim myRegisteredChannels As IChannel() = ChannelServices.RegisteredChannels

         Dim channel As IChannel
         For Each channel In  myRegisteredChannels
            If TypeOf channel Is IChannelSender Then
               Dim myChannelSender As IChannelSender = CType(channel, IChannelSender)

               myMessageSink = myChannelSender.CreateMessageSink(myUrl, Nothing, myObjectURI)
               If Not (myMessageSink Is Nothing) Then
                  Exit For
               End If
            End If
         Next channel
         If myMessageSink Is Nothing Then
            Throw New Exception("A supported channel could not be found for myUrl1:" + myUrl)
         End If
      End Sub 'New


      Public Overrides Function Invoke(ByVal myMesg As IMessage) As IMessage
         Console.WriteLine("MyProxy.Invoke Start")

         If TypeOf myMesg Is IMethodCallMessage Then
            Console.WriteLine("IMethodCallMessage")
         End If
         If TypeOf myMesg Is IMethodReturnMessage Then
            Console.WriteLine("IMethodReturnMessage")
         End If

         Console.WriteLine("Message Properties")
         Dim myDictionary As IDictionary = myMesg.Properties
         Dim myEnum As IDictionaryEnumerator = CType(myDictionary.GetEnumerator(), IDictionaryEnumerator)

         While myEnum.MoveNext()
            Dim myKey As Object = myEnum.Key
            Dim myKeyName As String = myKey.ToString()
            Dim myValue As Object = myEnum.Value

            Console.WriteLine( "{0} : {1}", myKeyName, myEnum.Value)
            If myKeyName = "__Args" Then
               Dim myArgs As Object() = CType(myValue, Object())
               Dim myInt As Integer
               For myInt = 0 To myArgs.Length - 1
                  Console.WriteLine(  "arg: {0} myValue: {1}", myInt, myArgs(myInt))
               Next myInt
            End If
            If myKeyName = "__MethodSignature" And Not (myValue Is Nothing) Then
               Dim myArgs As Object() = CType(myValue, Object())
               Dim myInt As Integer
               For myInt = 0 To myArgs.Length - 1
                  Console.WriteLine("arg: {0} myValue: {1}", myInt, myArgs(myInt))
               Next myInt
            End If
         End While

         Console.WriteLine("myUrl1 {0} object URI{1}", myUrl, myObjectURI)

         myDictionary("__Uri") = myUrl
         Console.WriteLine("URI {0}", myDictionary("__URI"))

         Dim myRetMsg As IMessage = myMessageSink.SyncProcessMessage(myMesg)

         If TypeOf (myRetMsg) Is IMethodReturnMessage Then
            Dim myMethodReturnMessage As IMethodReturnMessage = CType(myRetMsg, IMethodReturnMessage)
         End If

         Console.WriteLine("MyProxy.Invoke - Finish")
         Return myRetMsg
      End Function 'Invoke
   End Class 'MyProxy

   '
   ' Main class that drives the whole sample
   '
   Public Class ProxySample

      Public Shared Sub Main()
         ChannelServices.RegisterChannel(New HttpChannel())

         Console.WriteLine("Remoting Sample:")

         Console.WriteLine("Generate a new MyProxy using the Type")
         Dim myType As Type = GetType(MyHelloService)
         Dim myUrl1 As String = "https://localhost/myServiceAccess.soap"
         Dim myProxy As New MyProxy(myType, myUrl1)

         Console.WriteLine("Obtain the transparent proxy from myProxy")
         Dim myService As MyHelloService = CType(myProxy.GetTransparentProxy(), MyHelloService)

         Console.WriteLine("Calling the Proxy")
         Dim myReturnString As String = myService.myFunction("bill")

         Console.WriteLine("Checking result : {0}", myReturnString)

         If myReturnString = "Hi there bill, you are using .NET Remoting" Then
            Console.WriteLine("myService.HelloMethod PASSED : returned {0}", myReturnString)
         Else
            Console.WriteLine("myService.HelloMethod FAILED : returned {0}", myReturnString)
         End If
      End Sub 'Main
   End Class 'ProxySample
End Namespace 'MyNameSpace

[C#] 
using System;
using System.Collections;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using Share;    

namespace MyNameSpace
{
   
   public class MyProxy : RealProxy
   {
      string myUrl;
      string myObjectURI;
      IMessageSink myMessageSink;

      public MyProxy(Type myType, string myUrl1)
         : base(myType)
      {
         
         myUrl = myUrl1;

         IChannel[] myRegisteredChannels = ChannelServices.RegisteredChannels;
         foreach (IChannel channel in myRegisteredChannels )
         {
            if (channel is IChannelSender)
            {
               IChannelSender myChannelSender = (IChannelSender)channel;

               myMessageSink = myChannelSender.CreateMessageSink(myUrl, null, out myObjectURI);
               if (myMessageSink != null)
                  break;
            }
         }

         if (myMessageSink == null)
         {
            throw new Exception("A supported channel could not be found for myUrl1:"+ myUrl);
         }
      }


      public override IMessage Invoke(IMessage myMesg)
      {
         Console.WriteLine("MyProxy.Invoke Start");

         if (myMesg is IMethodCallMessage)
            Console.WriteLine("IMethodCallMessage");

         if (myMesg is IMethodReturnMessage)
            Console.WriteLine("IMethodReturnMessage");

         Console.WriteLine("Message Properties");
         IDictionary myDictionary = myMesg.Properties;
         IDictionaryEnumerator myEnum = (IDictionaryEnumerator) myDictionary.GetEnumerator();

         while (myEnum.MoveNext())
         {
            object myKey = myEnum.Key;
            string myKeyName = myKey.ToString();
            object myValue = myEnum.Value;

            Console.WriteLine("{0} : {1}", myKeyName, myEnum.Value);
            if (myKeyName == "__Args")
            {
               object[] myArgs = (object[])myValue;
               for (int myInt = 0; myInt < myArgs.Length; myInt++)
                  Console.WriteLine("arg: {0} myValue: {1}", myInt, myArgs[myInt]);
            }

            if ((myKeyName == "__MethodSignature") && (null != myValue))
            {
               object[] myArgs = (object[])myValue;
               for (int myInt = 0; myInt < myArgs.Length; myInt++)
                  Console.WriteLine("arg: {0} myValue: {1}", myInt, myArgs[myInt]);
            }
         }

         Console.WriteLine("myUrl1 {0} object URI{1}",myUrl,myObjectURI);

         myDictionary["__Uri"] = myUrl;
         Console.WriteLine("URI {0}", myDictionary["__URI"]);
         IMessage myRetMsg = myMessageSink.SyncProcessMessage(myMesg);

         if (myRetMsg is IMethodReturnMessage)
         {
            IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)myRetMsg;
         }

         Console.WriteLine("MyProxy.Invoke - Finish");
         return myRetMsg;
      }
   }

   //
   // Main class that drives the whole sample
   //
   public class ProxySample
   {
      public static void Main()
      {
         ChannelServices.RegisterChannel(new HttpChannel());

         Console.WriteLine("Remoting Sample:");

         Console.WriteLine("Generate a new MyProxy using the Type");
         Type myType = typeof(MyHelloService);
         string myUrl1 = "https://localhost/myServiceAccess.soap";
         MyProxy myProxy = new MyProxy(myType, myUrl1);

         Console.WriteLine("Obtain the transparent proxy from myProxy");
         MyHelloService myService = (MyHelloService)myProxy.GetTransparentProxy();

         Console.WriteLine("Calling the Proxy");
         string myReturnString = myService.myFunction("bill");

         Console.WriteLine("Checking result : {0}", myReturnString);

         if (myReturnString == "Hi there bill, you are using .NET Remoting")
         {
            Console.WriteLine("myService.HelloMethod PASSED : returned {0}", myReturnString);
         }
         else
         {
            Console.WriteLine("myService.HelloMethod FAILED : returned {0}", myReturnString);
         }
      }
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Runtime.Remoting.dll>
#using <System.dll>
#using <IMessageSink_Share.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Threading;
using namespace System::Runtime::Remoting;
using namespace System::Runtime::Remoting::Channels;
using namespace System::Runtime::Remoting::Channels::Http;
using namespace System::Runtime::Remoting::Proxies;
using namespace System::Runtime::Remoting::Messaging;
using namespace Share;    

public __gc class MyProxy : public RealProxy
{
    String* myUrl;
    String* myObjectURI;
    IMessageSink* myMessageSink;

public:
    MyProxy(Type* myType, String* myUrl1)
        : RealProxy(myType)
    {

        myUrl = myUrl1;

        IChannel* myRegisteredChannels[] = ChannelServices::RegisteredChannels;
        IEnumerator* myEnum = myRegisteredChannels->GetEnumerator();
        while (myEnum->MoveNext())
        {
            IChannel* channel = __try_cast<IChannel*>(myEnum->Current);
            if (dynamic_cast<IChannelSender*>(channel))
            {
                IChannelSender* myChannelSender = dynamic_cast<IChannelSender*>(channel);

                myMessageSink = myChannelSender->CreateMessageSink(myUrl, 0, &myObjectURI);
                if (myMessageSink != 0)
                    break;
            }
        }

        if (myMessageSink == 0)
        {
            throw new Exception(String::Format( S"A supported channel could not be found for myUrl1:{0}", myUrl ));
        }
    }

    IMessage* Invoke(IMessage* myMesg)
    {
        Console::WriteLine(S"MyProxy.Invoke Start");

        if (dynamic_cast<IMethodCallMessage*>(myMesg))
            Console::WriteLine(S"IMethodCallMessage");

        if (dynamic_cast<IMethodReturnMessage*>(myMesg))
            Console::WriteLine(S"IMethodReturnMessage");

        Console::WriteLine(S"Message Properties");
        IDictionary* myDictionary = myMesg->Properties;
        IDictionaryEnumerator* myEnum = dynamic_cast<IDictionaryEnumerator*> (myDictionary->GetEnumerator());

        while (myEnum->MoveNext())
        {
            Object* myKey = myEnum->Key;
            String* myKeyName = myKey->ToString();
            Object* myValue = myEnum->Value;

            Console::WriteLine(S"{0} : {1}", myKeyName, myEnum->Value);
            if (myKeyName->Equals(S"__Args"))
            {
                Object* myArgs[] = (Object*[])myValue;
                for (int myInt = 0; myInt < myArgs->Length; myInt++)
                    Console::WriteLine(S"arg: {0} myValue: {1}", __box(myInt), myArgs[myInt]);
            }

            if ((myKeyName->Equals(S"__MethodSignature")) && (0 != myValue))
            {
                Object* myArgs[] = (Object*[])myValue;
                for (int myInt = 0; myInt < myArgs->Length; myInt++)
                    Console::WriteLine(S"arg: {0} myValue: {1}", __box(myInt), myArgs[myInt]);
            }
        }

        Console::WriteLine(S"myUrl1 {0} object URI{1}",myUrl,myObjectURI);

        myDictionary->Item[S"__Uri"] = myUrl;
        Console::WriteLine(S"URI {0}", myDictionary->Item[S"__URI"]);
        IMessage* myRetMsg = myMessageSink->SyncProcessMessage(myMesg);

        if (dynamic_cast<IMethodReturnMessage*>(myRetMsg))
        {
            IMethodReturnMessage* myMethodReturnMessage = dynamic_cast<IMethodReturnMessage*>(myRetMsg);
        }

        Console::WriteLine(S"MyProxy.Invoke - Finish");
        return myRetMsg;
    }
};

//
// Main function that drives the whole sample
//
int main()
{
    ChannelServices::RegisterChannel(new HttpChannel());

    Console::WriteLine(S"Remoting Sample:");

    Console::WriteLine(S"Generate a new MyProxy using the Type");
    Type* myType = __typeof(MyHelloService);
    String* myUrl1 = S"https://localhost/myServiceAccess.soap";
    MyProxy* myProxy = new MyProxy(myType, myUrl1);

    Console::WriteLine(S"Obtain the transparent proxy from myProxy");
    MyHelloService* myService = dynamic_cast<MyHelloService*>(myProxy->GetTransparentProxy());

    Console::WriteLine(S"Calling the Proxy");
    String* myReturnString = myService->myFunction(S"bill");

    Console::WriteLine(S"Checking result : {0}", myReturnString);

    if (myReturnString->Equals(S"Hi there bill, you are using .NET Remoting"))
    {
        Console::WriteLine(S"myService.HelloMethod PASSED : returned {0}", myReturnString);
    }
    else
    {
        Console::WriteLine(S"myService.HelloMethod FAILED : returned {0}", myReturnString);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Runtime.Remoting.Messaging

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

IMessageSink メンバ | System.Runtime.Remoting.Messaging 名前空間