IMessage Interface

IMessage Interface

Contains communication data sent between cooperating message sinks.

Namespace:  System.Runtime.Remoting.Messaging
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)]
public interface IMessage

Message sinks that use IMessage can be placed in the client sink chains or in the server sink chains. A message object is passed from message sink to message sink through the chain, and carries a set of named properties such as action identifiers, envoy information, and parameters.

An object that implements the IMessage interface meets the minimum qualifications to be considered a message object. The exact object received by a message sink does not have to be passed on to the next sink, but this is often the case.

Although the objects in the property bag do not have to be serializable, the message sink implementer needs to consider this, since the properties that flow out of the application domain must be serializable.

// Create a custom 'RealProxy'. 
public class MyProxy : RealProxy
{
   String myURIString;
   MarshalByRefObject myMarshalByRefObject;   

   [PermissionSet(SecurityAction.LinkDemand)]
   public MyProxy(Type myType) : base(myType)
   {
      // RealProxy uses the Type to generate a transparent proxy.
      myMarshalByRefObject = (MarshalByRefObject)Activator.CreateInstance((myType));
      // Get 'ObjRef', for transmission serialization between application domains.
      ObjRef myObjRef = RemotingServices.Marshal(myMarshalByRefObject);
      // Get the 'URI' property of 'ObjRef' and store it.
      myURIString = myObjRef.URI;
      Console.WriteLine("URI :{0}", myObjRef.URI);
   }

   [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.Infrastructure)]
   public override IMessage Invoke(IMessage myIMessage)
   {
      Console.WriteLine("MyProxy.Invoke Start");
      Console.WriteLine("");

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

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

      Type msgType = myIMessage.GetType();
      Console.WriteLine("Message Type: {0}", msgType.ToString());
      Console.WriteLine("Message Properties");
      IDictionary myIDictionary = myIMessage.Properties;
      // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
      myIDictionary["__Uri"] = myURIString;
      IDictionaryEnumerator myIDictionaryEnumerator = 
         (IDictionaryEnumerator) myIDictionary.GetEnumerator();

      while (myIDictionaryEnumerator.MoveNext())
      {
         Object myKey = myIDictionaryEnumerator.Key;
         String myKeyName = myKey.ToString();
         Object myValue = myIDictionaryEnumerator.Value;

         Console.WriteLine("\t{0} : {1}", myKeyName, 
            myIDictionaryEnumerator.Value);
         if (myKeyName == "__Args")
         {
            Object[] myObjectArray = (Object[])myValue;
            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, 
                  myObjectArray[aIndex]);
         }

         if ((myKeyName == "__MethodSignature") && (null != myValue))
         {
            Object[] myObjectArray = (Object[])myValue;
            for (int aIndex = 0; aIndex < myObjectArray.Length; aIndex++)
               Console.WriteLine("\t\targ: {0} myValue: {1}", aIndex, 
                  myObjectArray[aIndex]);
         }
      }

      IMessage myReturnMessage;

      myIDictionary["__Uri"] = myURIString;
      Console.WriteLine("__Uri {0}", myIDictionary["__Uri"]);

      Console.WriteLine("ChannelServices.SyncDispatchMessage");
      myReturnMessage = ChannelServices.SyncDispatchMessage(myIMessage);

      // Push return value and OUT parameters back onto stack.

      IMethodReturnMessage myMethodReturnMessage = (IMethodReturnMessage)
         myReturnMessage;
      Console.WriteLine("IMethodReturnMessage.ReturnValue: {0}", 
         myMethodReturnMessage.ReturnValue);

      Console.WriteLine("MyProxy.Invoke - Finish");

      return myReturnMessage;
   }
}
// Create a custom 'RealProxy'.
public __gc class MyProxy : public RealProxy
{
   String* myURIString;
   MarshalByRefObject* myMarshalByRefObject;   

public:
   MyProxy(Type* myType) : RealProxy(myType)
   {
      // RealProxy uses the Type to generate a transparent proxy.
      myMarshalByRefObject = dynamic_cast<MarshalByRefObject*>(Activator::CreateInstance((myType)));
      // Get 'ObjRef', for transmission serialization between application domains.
      ObjRef* myObjRef = RemotingServices::Marshal(myMarshalByRefObject);
      // Get the 'URI' property of 'ObjRef' and store it.
      myURIString = myObjRef->URI;
      Console::WriteLine(S"URI :{0}", myObjRef->URI);
   }

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

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

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

      Type* msgType = myIMessage->GetType();
      Console::WriteLine(S"Message Type: {0}", msgType);
      Console::WriteLine(S"Message Properties");
      IDictionary* myIDictionary = myIMessage->Properties;
      // Set the '__Uri' property of 'IMessage' to 'URI' property of 'ObjRef'.
      myIDictionary->Item[S"__Uri"] = myURIString;
      IDictionaryEnumerator* myIDictionaryEnumerator = 
         dynamic_cast<IDictionaryEnumerator*> (myIDictionary->GetEnumerator());

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

         Console::WriteLine(S"\t{0} : {1}", myKeyName, 
            myIDictionaryEnumerator->Value);
         if (myKeyName->Equals(S"__Args"))
         {
            Object* myObjectArray[] = (Object*[])myValue;
            for (int aIndex = 0; aIndex < myObjectArray->Length; aIndex++)
               Console::WriteLine(S"\t\targ: {0} myValue: {1}", __box(aIndex), 
                  myObjectArray[aIndex]);
         }

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

      IMessage* myReturnMessage;

      myIDictionary->Item[S"__Uri"] = myURIString;
      Console::WriteLine(S"__Uri {0}", myIDictionary->Item[S"__Uri"]);

      Console::WriteLine(S"ChannelServices.SyncDispatchMessage");
      myReturnMessage = ChannelServices::SyncDispatchMessage(myIMessage);

      // Push return value and OUT parameters back onto stack.

      IMethodReturnMessage* myMethodReturnMessage = dynamic_cast<IMethodReturnMessage*>(myReturnMessage);
      Console::WriteLine(S"IMethodReturnMessage.ReturnValue: {0}", 
         myMethodReturnMessage->ReturnValue);

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

      return myReturnMessage;
   }
};

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft