. NET Remoting Study Notes (2) Activation Method
Activation Method before accessing an object instance of the remote type, you must create and initialize it through a process named Activation. This client creates a remote object through a channel, which is called object activation. Server-side activation, also known as WellKnow (well-known object), is published on a well-known Uniform Resource Identifier (URI) before activating an object instance. The server process then configures a WellKnown object for this type and publishes the object based on the specified port or address. Server activation is divided into SingleTon mode SingleCall mode SingleTon mode and SingleTon mode. Remoting creates the same object instance for all clients. When an object is active, the SingleTon instance processes all subsequent client access requests, regardless of whether they are the same client or other clients. The SingleTon instance will remain in the status of the method call. For example, if a remote object has an accumulation method (I = 0; ++ I), it is called by multiple clients (for example, two. If it is set to SingleTon mode, the first customer gets the value 1, and the second customer gets the value 2 because they get the same object instance. If you are familiar with Asp. Net status management, we can regard it as an Application status. Code: 1. creates the remote call processing class copy code using System; using System. runtime. remoting. metadata;/* code interpretation */namespace MessageMarshal {/* create a message sending delegate */public delegate void SendMessageHandler (string messge); [Serializable] public class TestMessageMarshal: export albyrefobject {private Guid ID {get; set;}/* re-create the ID number when creating an object instance */public TestMessageMarshal () {ID = Guid. newGuid ();}/* create a message sending event */public static event SendMessa GeHandler SendMessageEvent;/* Send message */[SoapMethod (XmlNamespace = "MessageMarshal", SoapAction = "MessageMarshal # SendMessage")] public void SendMessage (string messge) {if (SendMessageEvent! = Null) SendMessageEvent (ID. toString () + "\ t" + messge) ;}} copy Code 2. create the server code and copy the code using System; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. http; using MessageMarshal; namespace TestRemotingServer {/* code: Sakya Nakhon */class Program {static void Main (string [] args) {/* Create an HTTP channel */HttpChannel channel = new HttpChannel (8226);/* register the channel server */ChannelServices. registerChannel (channel, false);/* set the mode to Singleton */RemotingConfiguration. registerWellKnownServiceType (typeof (TestMessageMarshal), "test", WellKnownObjectMode. singleton); Console. writeLine ("started... ");/* receive client events */TestMessageMarshal. sendMessageEvent + = new SendMessageHandler (TestMessageMarshal_SendMessageEvent); Console. read ();} static void TestMessageMarshal_SendMessageEvent (string messge) {Console. writeLine (messge) ;}} copy code 3. create the client code and copy the code using System; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. http; using System. threading;/* code Releaser */namespace TestRemotingClient {class Program {static void Main (string [] args) {HttpChannel channel = new HttpChannel (); ChannelServices. registerChannel (channel, false);/* Registration channel Remote processing type */RemotingConfiguration. registerWellKnownClientType (typeof (MessageMarshal. testMessageMarshal )," http://localhost:8226/test ");/* Create a message entity */MessageMarshal. testMessageMarshal TestMessage = new MessageMarshal. testMessageMarshal (); while (true) {TestMessage. sendMessage ("DateTime. now: "+ System. dateTime. now. toString (); Console. writeLine ("send message... "); Thread. sleep (2000) ;}}} copy code 4. after running the server, open two client programs and check the results as follows: In the code diagram, when TestMessageMarshal has a new instance, its constructor will create different identifiers (guids ), the server receives the data request from the client and outputs the ID number to the interface. It can be seen from the interface that the server uses one channel (a real For processing. SingleCall mode is a stateless mode. Once set to SingleCall mode, Remoting creates a remote object instance for each client when the client calls the remote object method. GC automatically manages the destruction of the object instance. In the same example, the two clients accessing the remote object obtain 1. We can still consider Asp. Net as a Session state. We modify the server code as follows, and the client does not need to modify:/* set the mode to SingleCall */RemotingConfiguration. registerWellKnownServiceType (typeof (TestMessageMarshal), "test", WellKnownObjectMode. singleCall); enable the server, and then open a client. As shown in the output, each time the server creates a remote object instance for each client request. The client activation mode is different from the WellKnown mode. When Remoting activates each object instance, it assigns a URI to each client activation type. Once a client request is obtained in the client activation mode, an instance reference is created for each client. The SingleCall mode differs from the client activation mode: first, the creation time of the object instance is different. The client activation method is instantiated once the customer sends a call request, while the SingleCall method is created when the object method is called. Second, the objects activated in SingleCall mode are stateless, and the object lifecycle is managed by GC, while the objects activated on the client are stateful, and their lifecycles can be customized. Third, the two activation modes have different implementation methods on the server side and the client side. Especially on the client, the SingleCall mode is activated by GetObject (), which calls the default constructor of the object. The client activation mode is activated through CreateInstance (), which can pass parameters, so you can call a custom constructor to create an instance. 1. modify the server code and copy the code using System; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. http; using System. runtime. remoting. channels. tcp; using MessageMarshal; namespace TestRemotingServer {/* code: Sakya Nakhon */class Program {static void Main (string [] args) {/* Create an HTTP channel */HttpChannel channel = new HttpChannel (8226);/* register the channel server */ChannelServices. registerChannel (channel, false); RemotingConfiguration. applicationName = "test"; RemotingConfiguration. registerActivatedServiceType (typeof (TestMessageMarshal); Console. writeLine ("started... ");/* receive client events */TestMessageMarshal. sendMessageEvent + = new SendMessageHandler (TestMessageMarshal_SendMessageEvent); Console. read ();} static void TestMessageMarshal_SendMessageEvent (string messge) {Console. writeLine (messge) ;}} copy Code 2. modify the client code and copy the code using System; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. http; using System. threading;/* code Releaser */namespace TestRemotingClient {class Program {static void Main (string [] args) {HttpChannel channel = new HttpChannel (); ChannelServices. registerChannel (channel, false);/* Registration channel Remote processing type */RemotingConfiguration. registerActivatedClientType (typeof (MessageMarshal. testMessageMarshal )," http://localhost:8226/test ");/* Create a message entity */MessageMarshal. testMessageMarshal TestMessage = new MessageMarshal. testMessageMarshal (); while (true) {TestMessage. sendMessage ("DateTime. now: "+ System. dateTime. now. toString (); Console. writeLine ("send message... "); Thread. sleep (2000 );}}}