. NET Remoting Learning Notes (ii) how to activate

Source: Internet
Author: User

Catalogue
    • . NET Remoting Learning Notes (i) concepts
    • . NET Remoting Learning Notes (ii) how to activate
    • . NET Remoting Learning Note (c) channel

Reference: Baidu encyclopedia ♂ Windmill Car. Net

Activation mode Concept

Before accessing an object instance of a remote type, it must be created and initialized through a process called activation. This client creates a remote object through the channel, called the activation of the object.

activation is divided into two main categories : server-side activation client activation

server-side activation

Also known as Wellknow (well-known object)

The server application publishes this type on a well-known Uniform Resource Identifier (URI) before activating the 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-side activation is divided into: Singleton mode SingleCall mode

Singleton mode

is set to singleton activation mode, remoting will establish the same object instance for all clients. When an object is active, the singleton instance processes all subsequent client access requests, whether they are the same client or another client. The singleton instance will maintain its state in the method call. For example, if a remote object has an additive method (I=0;++i), it is called by multiple clients (for example, two). If set to Singleton, the first customer gets a value of 1, and the second customer gets a value of 2 because the object instance they obtained is the same. If you are familiar with ASP. NET state management, we can think of it as a kind of application state.

Paste the following code:

1. Creating a class for Remote call processing

usingSystem;usingSystem.Runtime.Remoting.Metadata;/*code to release the bitter monk*/namespacemessagemarshal{/*Create a Send message delegate*/     Public Delegate voidSendmessagehandler (stringMessge); [Serializable] Public classTestmessagemarshal:marshalbyrefobject {PrivateGuid ID {Get;Set; } /*re-create the identification number when creating a new object instance*/         PublicTestmessagemarshal () {ID=Guid.NewGuid (); }        /*Create a Send message event*/         Public Static EventSendmessagehandler sendmessageevent; /*Send Message*/[Soapmethod (XmlNamespace="Messagemarshal", SOAPAction ="Messagemarshal#sendmessage")]         Public voidSendMessage (stringMessge) {            if(Sendmessageevent! =NULL) sendmessageevent (ID. ToString ()+"\ t"+Messge); }    }}

2. Create the service-side code

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Http;usingMessagemarshal;namespacetestremotingserver{/*code: The Buddha of the bitter monk*/     classProgram {Static voidMain (string[] args) {            /*creating an HTTP channel*/HttpChannel Channel=NewHttpChannel (8226); /*Register channel service side*/ChannelServices.RegisterChannel (channel,false); /*set mode to Singleton*/RemotingConfiguration.RegisterWellKnownServiceType (typeof(Testmessagemarshal),"Test", Wellknownobjectmode.singleton); Console.WriteLine ("started ..."); /*Receiving client Events*/testmessagemarshal.sendmessageevent+=NewSendmessagehandler (testmessagemarshal_sendmessageevent);        Console.read (); }                 Static voidTestmessagemarshal_sendmessageevent (stringMessge)        {Console.WriteLine (MESSGE); }    }}

3. Create client-side code

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Http;usingSystem.Threading;/*code to release the bitter monk*/namespacetestremotingclient{classProgram {Static voidMain (string[] args) {HttpChannel Channel=NewHttpChannel (); ChannelServices.RegisterChannel (channel,false); /*Remote processing type for registered channels*/Remotingconfiguration.registerwellknownclienttype (typeof(Messagemarshal.testmessagemarshal),"http://localhost:8226/test"); /*Create a message entity*/Messagemarshal.testmessagemarshal TestMessage=NewMessagemarshal.testmessagemarshal ();  while(true) {testmessage.sendmessage ("DateTime.Now:"+System.DateTime.Now.ToString ()); Console.WriteLine ("Send Message ..."); Thread.Sleep ( -); }        }    }}

4. After running the server, open two client programs to view the results as follows:

In the code schematic, when Testmessagemarshal has a new instance, its constructor creates a different identity (GUID), the server receives the client's data request, and outputs the identification number to the interface, as can be seen from the interface, multiple client-requested channels, The server is processed using a single channel (one instance).

SingleCall Mode

SingleCall is a stateless mode. Once set to SingleCall mode, when the client invokes the method of the remote object, remoting establishes a remote object instance for each client, and the destruction of the object instance is automatically managed by the GC. As an example above, the two customers accessing the remote object received 1. We can still learn from ASP. NET state management, think of it as a session state.

We modify the server-side code as follows, and the client does not need to modify:

            /* Set the mode to SingleCall */            RemotingConfiguration.RegisterWellKnownServiceType (typeof (Testmessagemarshal), "test", WellKnownObjectMode.SingleCall);   

Open the server, then open a client, as follows

As you can see from the output, a remote object instance is created for each client request each time the server is requested.

Client Activation

Unlike wellknown mode, remoting assigns each client-activated type a URI when it activates each instance of the object. Client activation mode Once the client's request is received, an instance reference will be established for each client. There is a difference between the SingleCall mode and the client-activated mode: First, the object instance is created in a different time. Client activation is instantiated as soon as a request is made by the customer, and SingleCall is created when the object method is called. Second, the SingleCall mode activates an object that is stateless, the management of the object's lifetime is managed by the GC, and the client-activated object has a state and its life cycle can be customized. Three, the two activation modes on the server side and the client implementation method is different. Especially at the client, the SingleCall mode is activated by GetObject (), which invokes the object's default constructor. While the client-activated mode is activated by CreateInstance (), it can pass parameters, so you can call a custom constructor to create an instance.

1. Modify the service-side code

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Http;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingMessagemarshal;namespacetestremotingserver{/*code: The Buddha of the bitter monk*/     classProgram {Static voidMain (string[] args) {            /*creating an HTTP channel*/HttpChannel Channel=NewHttpChannel (8226); /*Register channel service side*/ChannelServices.RegisterChannel (channel,false); Remotingconfiguration.applicationname="Test"; Remotingconfiguration.registeractivatedservicetype (typeof(Testmessagemarshal)); Console.WriteLine ("started ..."); /*Receiving client Events*/testmessagemarshal.sendmessageevent+=NewSendmessagehandler (testmessagemarshal_sendmessageevent);        Console.read (); }                 Static voidTestmessagemarshal_sendmessageevent (stringMessge)        {Console.WriteLine (MESSGE); }    }}

2. Modify the client code

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSystem.Runtime.Remoting.Channels.Http;usingSystem.Threading;/*code to release the bitter monk*/namespacetestremotingclient{classProgram {Static voidMain (string[] args) {HttpChannel Channel=NewHttpChannel (); ChannelServices.RegisterChannel (channel,false); /*Remote processing type for registered channels*/Remotingconfiguration.registeractivatedclienttype (typeof(Messagemarshal.testmessagemarshal),"http://localhost:8226/test"); /*Create a message entity*/Messagemarshal.testmessagemarshal TestMessage=NewMessagemarshal.testmessagemarshal ();  while(true) {testmessage.sendmessage ("DateTime.Now:"+System.DateTime.Now.ToString ()); Console.WriteLine ("Send Message ..."); Thread.Sleep ( -); }        }    }}

3. Test, open the server and two clients

You can see that each server side creates an instance object for each client.

The test directory structure is as follows, otherwise the client remote object and the server will not correspond. An exception that will be reported "the requested service could not be found "

This is the three ways to activate Remoting, if you have questions, please correct me.

The source of the Buddhist monk: http://www.cnblogs.com/woxpp/p/3995366.html This copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article page obvious location to the original link.

. NET Remoting Learning Notes (ii) how to activate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.