public sealed class RemotingServices
|
Connect | Overloaded:Connect(Type classToProxy, string url) Takes in the Type and URL of the well-known object to which you want to connect to, and creates a proxy object for it. |
Connect | Overloaded:Connect(Type classToProxy, string url, object data) Takes in the Type and URL of the well-known object to which you want to connect, and as well as channel specific data, and creates a proxy for it. |
Disconnect | Stops an object from receiving any further messages through the registered remoting channels. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
ExecuteMessage | Connects to the specified remote object, and executes the provided IMethodCallMessage on it. |
GetEnvoyChainForProxy | Returns a chain of envoy sinks that should be used when sending messages to the remote object represented by the specified proxy. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLifetimeService | Returns a lifetime service object that controls the lifetime policy of the specified object. |
GetMethodBaseFromMethodMessage | Returns the method base from the given IMethodMessage. |
GetObjectData | Serializes the specified marshal by reference object into the provided SerializationInfo. |
GetObjectUri | Retrieves the URI for the specified object. |
GetObjRefForProxy | Returns the ObjRef that represents the remote object from the specified proxy. |
GetRealProxy | Returns the real proxy backing the specified transparent proxy. |
GetServerTypeForUri | Returns the Type of the object with the specified URI. |
GetSessionIdForMethodMessage | Retrieves a session ID for a message. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsMethodOverloaded | Returns a Boolean value indicating whether the method in the given message is overloaded. |
IsObjectOutOfAppDomain | Returns a Boolean value indicating whether the object specified by the given transparent proxy is contained in a different AppDomain than the object that called the current method. |
IsObjectOutOfContext | Returns a Boolean value indicating whether the object represented by the given proxy is contained in a different context than the object that called the current method. |
IsOneWay | Returns a Boolean value indicating whether the client that called the method specified in the given message is waiting for the server to finish processing the method before continuing execution. |
IsTransparentProxy | Returns a Boolean value indicating whether the given object is a transparent proxy or a real object. |
LogRemotingStage | Public reserved method, for internal use only. |
Marshal | Overloaded:Marshal(MarshalByRefObject Obj) Takes a MarshalByRefObject, registers it with the remoting infrastructure, and converts it into an instance of the ObjRef class. |
Marshal | Overloaded:Marshal(MarshalByRefObject Obj, string URI) Converts the given MarshalByRefObject into an instance of the ObjRef class with the specified URI. |
Marshal | Overloaded:Marshal(MarshalByRefObject Obj, string ObjURI, Type RequestedType) Takes a MarshalByRefObject and converts it into an instance of the ObjRef class with the specified URI, and the provided Type. |
SetObjectUriForMarshal | Sets the URI for the subsequent call to the RemotingServices.Marshal method. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Unmarshal | Overloaded:Unmarshal(ObjRef objectRef) Takes an ObjRef and creates a proxy object out of it. |
Unmarshal | Overloaded:Unmarshal(ObjRef objectRef, bool fRefine) Takes an ObjRef and creates a proxy object out of it, refining it to the type on the server. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
classToProxy
url
Console.WriteLine("Connecting to SampleNamespace.SampleWellKnown."); SampleWellKnown proxy = (SampleWellKnown)RemotingServices.Connect(typeof(SampleWellKnown), SERVER_URL); Console.WriteLine("Connected to SampleWellKnown"); // Verifies that the object reference is to a transparent proxy. if (RemotingServices.IsTransparentProxy(proxy)) Console.WriteLine("proxy is a reference to a transparent proxy."); else Console.WriteLine("proxy is not a transparent proxy. This is unexpected."); // Calls a method on the server object. Console.WriteLine("proxy.Add returned {0}.", proxy.Add(2, 3));
classToProxy
url
data
The data object is used to communicate information to the channel, and is passed to the IChannelSender.CreateMessageSink method.
public static bool Disconnect( |
obj
Exception Type | Condition |
---|---|
ArgumentNullException | The obj parameter is null. |
ArgumentException | The obj parameter is a proxy. |
TcpChannel channel = new TcpChannel(9000); ChannelServices.RegisterChannel(channel); SampleWellKnown objectWellKnown = new SampleWellKnown(); // After the channel is registered, the object needs to be registered // with the remoting infrastructure. So, Marshal is called. ObjRef objrefWellKnown = RemotingServices.Marshal(objectWellKnown, "objectWellKnownUri"); Console.WriteLine("An instance of SampleWellKnown type is published at {0}.", objrefWellKnown.URI); Console.WriteLine("Press enter to unregister SampleWellKnown, so that it is no longer available on this channel."); Console.ReadLine(); RemotingServices.Disconnect(objectWellKnown); Console.WriteLine("Press enter to end the server process."); Console.ReadLine();
public static IMethodReturnMessage ExecuteMessage( |
target
reqMsg
public void ProcessMessageStart(IMessage requestMessage, bool bClientSide, bool bAsyncCall) { Console.WriteLine("\nProcessMessageStart"); Console.WriteLine("requestMessage = {0}", requestMessage); try { Console.WriteLine("SessionId = {0}.", RemotingServices.GetSessionIdForMethodMessage((IMethodMessage)requestMessage)); } catch (InvalidCastException) { Console.WriteLine("The requestMessage is not an IMethodMessage."); } IMethodCallMessage requestMethodCallMessage; try { requestMethodCallMessage = (IMethodCallMessage)requestMessage; // Prints the details of the IMethodCallMessage to the console. Console.WriteLine("\nMethodCall details"); Console.WriteLine("Uri = {0}", requestMethodCallMessage.Uri); Console.WriteLine("TypeName = {0}", requestMethodCallMessage.TypeName); Console.WriteLine("MethodName = {0}", requestMethodCallMessage.MethodName); Console.WriteLine("ArgCount = {0}", requestMethodCallMessage.ArgCount); Console.WriteLine("MethodCall.Args"); foreach(object o in requestMethodCallMessage.Args) Console.WriteLine("\t{0}", o); // Sends this method call message to another server to replicate // the call at the second server. if (requestMethodCallMessage.Uri == replicatedServiceUri) { SampleService replicationService = (SampleService)Activator.GetObject(typeof(SampleService), replicationServerUrl + replicatedServiceUri); IMethodReturnMessage returnMessage = RemotingServices.ExecuteMessage(replicationService, requestMethodCallMessage); // Prints the results of the method call stored in the IMethodReturnMessage. Console.WriteLine("\nMessage returned by ExecuteMessage."); Console.WriteLine("\tException = {0}", returnMessage.Exception); Console.WriteLine("\tReturnValue = {0}", returnMessage.ReturnValue); Console.WriteLine("\tOutArgCount = {0}", returnMessage.OutArgCount); Console.WriteLine("Return message OutArgs"); foreach(object o in requestMethodCallMessage.Args) Console.WriteLine("\t{0}", o); } } catch (InvalidCastException) { Console.WriteLine("The requestMessage is not a MethodCall"); } }
~RemotingServices(); |
public static IMessageSink GetEnvoyChainForProxy( |
obj
public virtual int GetHashCode(); |
public static object GetLifetimeService( |
obj
using System; using System.Net.Sockets; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Messaging; using System.Runtime.Remoting.Lifetime; using TimerSample; namespace GroupCoffeeTimer { public class TimerClient : MarshalByRefObject, ISponsor { public static void Main() { TimerClient myClient = new TimerClient(); } public TimerClient() { // Registers the HTTP Channel so that this client can receive // events from the remote service. ChannelServices.RegisterChannel(new HttpChannel(0)); WellKnownClientTypeEntry remoteType = new WellKnownClientTypeEntry(typeof(TimerService), "http://localhost:9000/MyService/TimerService.soap"); RemotingConfiguration.RegisterWellKnownClientType(remoteType); TimerService groupTimer = new TimerService(); groupTimer.MinutesToTime = 4.0; // Registers this client as a lease sponsor so that it can // prevent the expiration of the TimerService. ILease leaseObject = (ILease)RemotingServices.GetLifetimeService(groupTimer); leaseObject.Register(this); // Subscribes to the event so that the client can receive notifications from the server. groupTimer.TimerExpired += new TimerExpiredEventHandler(OnTimerExpired); Console.WriteLine("Connected to TimerExpired event"); groupTimer.Start(); Console.WriteLine("Timer started for {0} minutes.", groupTimer.MinutesToTime); Console.WriteLine("Press enter to end the client process."); Console.ReadLine(); } public void OnTimerExpired (object source, TimerServiceEventArgs e) { Console.WriteLine("TimerHelper.OnTimerExpired: {0}", e.Message); } public TimeSpan Renewal(ILease lease) { Console.WriteLine("TimerClient: Renewal called."); return TimeSpan.FromMinutes(0.5); } } }
public static MethodBase GetMethodBaseFromMethodMessage( |
msg
public static void GetObjectData( |
obj
info
context
Exception Type | Condition |
---|---|
ArgumentNullException | The obj or info parameter is null. |
public static string GetObjectUri( |
obj
RealProxy proxy = RemotingServices.GetRealProxy(obj); Console.WriteLine("Real proxy type: {0}", proxy.GetProxiedType().ToString()); Console.WriteLine("Object URI: {0}", RemotingServices.GetObjectUri(obj).ToString()); IMessageSink msgSink = RemotingServices.GetEnvoyChainForProxy(obj).NextSink;
public static ObjRef GetObjRefForProxy( |
obj
ObjRef s contain information that describes the Type and class of the object being marshaled, a URI that uniquely identifies the specific object instance, and communication related information on how to reach the remote application where the object is located.
ObjRef objRefSample = RemotingServices.GetObjRefForProxy(myRemoteObject); Console.WriteLine("***ObjRef Details***"); Console.WriteLine("URI:\t{0}", objRefSample.URI); object[] channelData = objRefSample.ChannelInfo.ChannelData; Console.WriteLine("Channel Info:"); foreach(object o in channelData) Console.WriteLine("\t{0}", o.ToString()); IEnvoyInfo envoyInfo = objRefSample.EnvoyInfo; if (envoyInfo == null) { Console.WriteLine("This ObjRef does not have envoy information."); } else { IMessageSink envoySinks = envoyInfo.EnvoySinks; Console.WriteLine("Envoy Sink Class: {0}", envoySinks); } IRemotingTypeInfo typeInfo = objRefSample.TypeInfo; Console.WriteLine("Remote type name: {0}", typeInfo.TypeName); Console.WriteLine("Can my object cast to a Bitmap? {0}", typeInfo.CanCastTo(typeof(System.Drawing.Bitmap), objRefSample));
proxy
The transparent proxy is backed by an instance of a managed runtime class of type RealProxy. The RealProxy implements a part of the functionality needed to forward the operations from the transparent proxy. Note that a proxy object inherits the associated semantics of managed objects such as garbage collection, support for fields and methods, and can be extended to form new classes. The proxy acts as an object of the same class as the remote object (transparent proxy), and is also a managed object.
URI
Console.WriteLine("Server type: {0}", RemotingServices.GetServerTypeForUri(myObjectUri));
public static string GetSessionIdForMethodMessage( |
msg
For more information on identifying sessions and session IDs, see the conceptual topic at MSDN: sessionstate.
public void ProcessMessageStart(IMessage requestMessage, bool bClientSide, bool bAsyncCall) { Console.WriteLine("\nProcessMessageStart"); Console.WriteLine("requestMessage = {0}", requestMessage); try { Console.WriteLine("SessionId = {0}.", RemotingServices.GetSessionIdForMethodMessage((IMethodMessage)requestMessage)); } catch (InvalidCastException) { Console.WriteLine("The requestMessage is not an IMethodMessage."); } IMethodCallMessage requestMethodCallMessage; try { requestMethodCallMessage = (IMethodCallMessage)requestMessage; // Prints the details of the IMethodCallMessage to the console. Console.WriteLine("\nMethodCall details"); Console.WriteLine("Uri = {0}", requestMethodCallMessage.Uri); Console.WriteLine("TypeName = {0}", requestMethodCallMessage.TypeName); Console.WriteLine("MethodName = {0}", requestMethodCallMessage.MethodName); Console.WriteLine("ArgCount = {0}", requestMethodCallMessage.ArgCount); Console.WriteLine("MethodCall.Args"); foreach(object o in requestMethodCallMessage.Args) Console.WriteLine("\t{0}", o); // Sends this method call message to another server to replicate // the call at the second server. if (requestMethodCallMessage.Uri == replicatedServiceUri) { SampleService replicationService = (SampleService)Activator.GetObject(typeof(SampleService), replicationServerUrl + replicatedServiceUri); IMethodReturnMessage returnMessage = RemotingServices.ExecuteMessage(replicationService, requestMethodCallMessage); // Prints the results of the method call stored in the IMethodReturnMessage. Console.WriteLine("\nMessage returned by ExecuteMessage."); Console.WriteLine("\tException = {0}", returnMessage.Exception); Console.WriteLine("\tReturnValue = {0}", returnMessage.ReturnValue); Console.WriteLine("\tOutArgCount = {0}", returnMessage.OutArgCount); Console.WriteLine("Return message OutArgs"); foreach(object o in requestMethodCallMessage.Args) Console.WriteLine("\t{0}", o); } } catch (InvalidCastException) { Console.WriteLine("The requestMessage is not a MethodCall"); } }
public Type GetType(); |
public static bool IsMethodOverloaded( |
msg
tp
// Create a remote version of TempConverter.Converter. TempConverter.Converter converter1 = (TempConverter.Converter) Activator.GetObject( typeof(TempConverter.Converter), "http://localhost:8085/TempConverter"); // Create a local version of TempConverter.Converter. TempConverter.Converter converter2 = new TempConverter.Converter(); // Returns true, converter1 is remote and in a different appdomain. System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain( converter1); // Returns false, converter2 is local and running in this appdomain. System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain( converter2); // Returns true, converter1 is remote and in a different context. System.Runtime.Remoting.RemotingServices.IsObjectOutOfContext( converter1); // Returns false, converter2 is local and running in this context. System.Runtime.Remoting.RemotingServices.IsObjectOutOfContext( converter2);
tp
// Create a remote version of TempConverter.Converter. TempConverter.Converter converter1 = (TempConverter.Converter) Activator.GetObject( typeof(TempConverter.Converter), "http://localhost:8085/TempConverter"); // Create a local version of TempConverter.Converter. TempConverter.Converter converter2 = new TempConverter.Converter(); // Returns true, converter1 is remote and in a different appdomain. System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain( converter1); // Returns false, converter2 is local and running in this appdomain. System.Runtime.Remoting.RemotingServices.IsObjectOutOfAppDomain( converter2); // Returns true, converter1 is remote and in a different context. System.Runtime.Remoting.RemotingServices.IsObjectOutOfContext( converter1); // Returns false, converter2 is local and running in this context. System.Runtime.Remoting.RemotingServices.IsObjectOutOfContext( converter2);
public static bool IsOneWay( |
method
One way methods cannot have a return value or any out parameters.
public class HelloServer : MarshalByRefObject { public HelloServer() { Console.WriteLine("HelloServer activated."); } [OneWay()] public void SayHelloToServer(string name) { Console.WriteLine("Client invoked SayHelloToServer(\"{0}\").", name); } // Note the lack of the OneWayAttribute adornment on this method. public string SayHelloToServerAndWait(string name) { Console.WriteLine("Client invoked SayHelloToServerAndWait(\"{0}\").", name); Console.WriteLine( "Client waiting for return? {0}", RemotingServices.IsOneWay(MethodBase.GetCurrentMethod()) ? "No" : "Yes" ); return "Hi there, " + name + "."; } }
proxy
The transparent proxy is itself housed by an instance of a managed runtime class of type RealProxy. The RealProxy implements a part of the functionality needed to forward the operations from the transparent proxy. A proxy object inherits the associated semantics of managed objects such as garbage collection, support for fields and methods, and can be extended to form new classes. Thus the proxy has a dual nature; on the one hand it needs to act as an object of the same class as the remote object (transparent proxy), and on the other it is a managed object itself.
A proxy object can be used without regard to any remoting subdivisions within an AppDomain. Applications need not distinguish between proxy references and object references. However, service providers dealing with issues such as activation, lifetime management, and transactions need to make such distinctions.
// Creates an instance of a context-bound type SampleSynchronized. SampleSyncronized sampSyncObj = new SampleSyncronized(); // Checks whether the object is a proxy, since it is context-bound. if (RemotingServices.IsTransparentProxy(sampSyncObj)) Console.WriteLine("sampSyncObj is a proxy."); else Console.WriteLine("sampSyncObj is NOT a proxy.");
[Conditional("")] |
stage
public static ObjRef Marshal( Marshal( |
Obj
Exception Type | Condition |
---|---|
RemotingException | The Obj parameter is an object proxy. |
During marshaling, the context from the current thread is used, not the context that was active when the object was created. If a URI was not explicitly set by the RemotingServices.SetObjectUriForMarshal method, it is automatically generated by the remoting identity infrastructure.
You cannot associate a URI with a proxy for one of two reasons: either the URI was generated at the server side for the object it represents, or the object is well known, in which case the URI is known. For this reason, if the Obj parameter is a proxy, an exception will be thrown. For custom proxies this restriction is relaxed because the transparent proxy is treated as the server object.
public static ObjRef Marshal( Marshal( |
Obj
URI
Exception Type | Condition |
---|---|
RemotingException | Obj is an object proxy, and the URI parameter is not null. |
During marshaling, the context from the current thread is used, not the context that was active when the object was created.
You cannot associate a URI with a proxy for one of two reasons: either the URI was generated at the server side for the object it represents, or the object is well known, in which case the URI is known. For this reason, if the Obj parameter is a proxy, an exception will be thrown. For custom proxies this restriction is relaxed because the transparent proxy is treated as the server object.
TcpChannel channel = new TcpChannel(9000); ChannelServices.RegisterChannel(channel); SampleWellKnown objectWellKnown = new SampleWellKnown(); // After the channel is registered, the object needs to be registered // with the remoting infrastructure. So, Marshal is called. ObjRef objrefWellKnown = RemotingServices.Marshal(objectWellKnown, "objectWellKnownUri"); Console.WriteLine("An instance of SampleWellKnown type is published at {0}.", objrefWellKnown.URI); Console.WriteLine("Press enter to unregister SampleWellKnown, so that it is no longer available on this channel."); Console.ReadLine(); RemotingServices.Disconnect(objectWellKnown); Console.WriteLine("Press enter to end the server process."); Console.ReadLine();
public static ObjRef Marshal( Marshal( |
Obj
ObjURI
RequestedType
Exception Type | Condition |
---|---|
RemotingException | Obj is a proxy of a remote object, and the ObjUri parameter is not null. |
The specified Type is used by the remoting infrastructure to limit the scope of the exposed type hierarchy. For example, if object A derives from object B, which derives from object C, and RemotingServices.Marshal is called, then the client can cast the proxy between C and B but not to A.
During marshaling, the context from the current thread is used, not the context that was active when the object was created.
You cannot associate a URI with a proxy for one of two reasons: either the URI was generated at the server side for the object it represents, or the object is well known, in which case the URI is known. For this reason, if the Obj parameter is a proxy, an exception will be thrown. For custom proxies this restriction is relaxed because the transparent proxy is treated as the server object.
protected object MemberwiseClone(); |
public static void SetObjectUriForMarshal( |
obj
uri
Exception Type | Condition |
---|---|
RemotingException | obj is not a local object, has already been marshaled, or the current method has already been called on. |
using System; using System.Runtime.Remoting; public class SetObjectUriForMarshalTest { class TestClass : MarshalByRefObject { } public static void Main() { TestClass obj = new TestClass(); RemotingServices.SetObjectUriForMarshal(obj, "testUri"); RemotingServices.Marshal(obj); Console.WriteLine(RemotingServices.GetObjectUri(obj)); } }
public virtual string ToString(); |
objectRef
Exception Type | Condition |
---|---|
ArgumentException | The ObjRef instance specified in the objectRef parameter is not well formed. |
ObjRef s contain information that describes the Type and class of the object being marshaled, a URI that uniquely identifies the specific object instance, and communication related information on how to reach the remoting subdivision where the object is located.
ChannelServices.RegisterChannel(new HttpChannel()); SampleService objectSample = (SampleService)Activator.GetObject(typeof(SampleService), "http://localhost:9000/MySampleService/SampleService.soap"); // The GetManuallyMarshaledObject() method uses RemotingServices.Marshal() // to create an ObjRef object for a SampleTwo object. ObjRef objRefSampleTwo = objectSample.GetManuallyMarshaledObject(); SampleTwo objectSampleTwo = (SampleTwo)RemotingServices.Unmarshal(objRefSampleTwo); objectSampleTwo.PrintMessage("ObjRef successfuly unmarshaled.");
objectRef
fRefine
Exception Type | Condition |
---|---|
ArgumentException | The ObjRef instance specified in the objectRef parameter is not well formed. |
ObjRef s contain information that describes the Type and class of the object being marshaled, a URI that uniquely identifies the specific object instance, and communication related information on how to reach the remoting subdivision where the object is located.
When first created, the proxy is of type MarshalByRefObject. As you cast it into different types, the remoting infrastructure keeps track of the most used type to avoid loading the type unnecessarily.