. NET Remoting (3)--activation, activation mode

Source: Internet
Author: User

The creation and initialization of a new object is activated when a remote object is built. The remoting system must always be aware of the type of activation required to set the object to be available to clients. Activation is available in two ways: Server activation and client activation

Server activation

Server-activated objects are objects that are directly controlled by the server for lifetime. The server application domain creates these objects only when the client makes method calls to the objects, not when the client calls new or Activator.GetObject. When a client requests an instance of a server-activated type, a proxy is created in the client application domain. The server activation type only allows the default constructor to be used. If you want to create a type of instance using a specific constructor that takes parameters, you can use client-side activation, or you can publish a specific instance dynamically.

Server-activated objects have two activation modes:Singleton and SingleCall, which are called known objects and are identified by the enumeration type: WellKnownObjectMode.

The singleton type has only one instance at any one time. All client requests will be serviced by this instance. If there is no instance, the server creates one, and all subsequent client requests are serviced by this instance. For a single-piece type, it is associated to the default lifetime.

The SingleCall type creates an instance for each client request. The next method call will be serviced by a different server instance, even if the system has not reclaimed a reference to the previous instance.

Class RemotingConfiguration

Method RegisterWellKnownServiceType registers the object type on the server as a known type

public static void RegisterWellKnownServiceType (

Type type,

String Objecturi,

WellKnownObjectMode mode)

All customers who know the URI of the registered known object can obtain a proxy for that object by registering the channel with ChannelServices and then invoking the new or Activator.GetObject method to activate the object.

• When activating an object with new, you must first use the Registerwellknownclienttype method to register the known object type with the client. Call the Registerwellkonwnclienttype method to provide the remote processing infrastructure with the location of the remote object so that the new keyword can create it.

• When using the Activator.GetObject method to activate a known object, the URL of the object must be supplied as an argument to the method, so in this case it is not necessary to register with the client in advance.

When the call arrives at the server, the system extracts the URI from the message, examines the remoting table to locate a reference to the object that matches the URI, instantiates the object, and forwards the method call to the object. If it is SingleCall, the object is destroyed after the method call is complete. Create a new instance of the object for each method.

The difference between Activator.GetObject and new is that activator.getobject can specify a URL as a parameter, and new is to get the URL from the configuration

( 1 ) by Actovator.getobject method to get the proxy

The Activator class is located under System. It contains methods for creating object types locally or remotely, or for obtaining references to existing remote objects. Where GetObject this method has 2 overloads, the meaning of the method is to create a proxy for a known object or XML Web services.

GetObject (Type, String) creates a proxy for the well-known object indicated by the specified type and URL.

GetObject (Type, String, object) creates a proxy for the well-known object indicated by the specified type, URL, and channel data.

Take the first 2 arguments as an example: the first parameter, type, is registered on the server as a well-known type, the second parameter is the URL of a well-known object

Service side:

HttpChannel _channel = new HttpChannel (10001);

ChannelServices.RegisterChannel (_channel,false);

Console.WriteLine ("HTTP channel remoting service begins ...");

RemotingConfiguration.RegisterWellKnownServiceType (

typeof (Selfremoteobject), "Selfremoteobject",

Wellknownobjectmode.singleton);

Client:

public void Testservice ()

{

Selfremoteobject app =

(Selfremoteobject) Activator.GetObject (

typeof (Selfremoteobject),

"Http://localhost:10001/selfRemoteObject");

Assert.AreEqual (13,app. Plus (3, 10));

}

( 2 ) by New to create an agent

When you use new to activate an object, you first use the Registerwellknownclienttype method to register the known object type with the client. Then make the new keyword available to create it.

Server or service side (1)

Client:

public void Testservicenew ()

{

Remotingconfiguration.registerwellknownclienttype (

typeof (Selfremoteobject),

"Http://localhost:10001/selfRemoteObject");

Selfremoteobject app = new Selfremoteobject ();

Assert.AreEqual, App. Plus (3, 10));

}

After creating the agent, the server manages the creation of known types of objects, which are made by default constructors (no parameters) when the method is called.

Client activation

Client-activated objects are objects that invoke the application domain to control their lifetime, as is the case with the application domain controlling the local object lifetime.

When a client-activated object is created, the client invokes the server. The server instantiates the remote object and returns the object reference to the client. The client uses this reference to create a proxy for the remote object. Whenever a client creates an instance of a client-activated object, it receives a proxy that communicates with a specific server instance of the remote object until its lease expires and the memory is recycled.

Steps to use client activation:

• To create an instance of a client-activated object on the server, know its type

• Use the Registeractivatedservicetype method to register it on the server.

Client steps:

• The client registers a channel with the ChannelServices first,

• Activate the object by calling new or Activator.CreateInstance.

There are two ways to create an instance of a client-activated type:

• With new

• Passing a remote object in a Activator.CreateInstance call

Prepare the service side first:

HttpChannel _channel = new HttpChannel (10001);

ChannelServices.RegisterChannel (_channel,false);

Console.WriteLine ("HTTP channel remoting service begins ...");

Remotingconfiguration.applicationname = "Selfremoteobject";

Remotingconfiguration.registeractivatedservicetype

(typeof (Selfremoteobject));

Console.read ();

Server to register the type through the Registeractivatedservicetype method, and first set the ApplicationName, this is equivalent to the URL

Client

( 1 ) to New to carry out

public void Testclientnew ()

{

HttpChannel _channel = new HttpChannel ();

ChannelServices.RegisterChannel (_channel, false);

Remotingconfiguration.registeractivatedclienttype

(

typeof (Selfremoteobject),

"Http://localhost:10001/selfRemoteObject");

Selfremoteobject App1 = new Selfremoteobject ();

Assert.AreEqual, App1. Plus (1, 9));

}

By activating the client object type with new, the object type is registered with the client using the Registeractivatedclienttype method, and then new is available.

( 2 ) Activator.CreateInstance

If you are using CreateInstance, you provide the URL of the remote application as a parameter without registering the type on the client. If you want to use a URL to make arguments, the URL is encapsulated in the Urlattribute class instance.

public void Testclientinstance ()

{

HttpChannel _channel = new HttpChannel ();

ChannelServices.RegisterChannel (_channel,false);

Object[] Url={new Urlattribute

("Http://localhost:10001/selfRemoteObject")};

Selfremoteobject App1 =

(Selfremoteobject) Activator

. CreateInstance (typeof (Selfremoteobject), null, URL);

Assert.AreEqual, App1. Plus (1, 9));

}

Client activation methods You can use the parameter builder to create a remote object instance.

To modify a remote object, add a property with a parameter constructor:

public int Unid {get; set;}

Public Selfremoteobject () {}

Public selfremoteobject (int iunid)

{

Unid = Iunid;

}

The parameter constructor is added to display the declared default parameterless constructor.

Create an object instance by using the new method.

Selfremoteobject App1 = new Selfremoteobject ();

Assert.AreEqual (0, App1. Unid);

Selfremoteobject app2 = new Selfremoteobject (10);

Assert.AreEqual, App2. Unid);

Activation mode, which is how the remote object instance is created. Server-side activation is the creation of a remote object that is managed by the service side, in which case the instance is created when the client invokes the method, and the client activates the creation of the remote object by the client, in this way, when the client makes a call to create the instance.

Type registration method for the RemotingConfiguration class:

Registeractivatedclienttype registers the object type on the client as a type that can be activated on the server.

Registeractivatedservicetype registers the object type on the server as a type that can be activated from the client on request.

Registerwellknownclienttype registers the object type on the client as a known type ("Single call" (SingleCall) or singleton).

RegisterWellKnownServiceType registers the object type on the server as a known type ("Single call" (SingleCall) or singleton).

Disconnect

RemotingServices.Disconnect This method is used to prevent the object from receiving messages through the registered remoting channel. Its argument must be an instantiated object (to be closed), and the server-side registration type is to be performed by the Marshal Method:

HttpChannel _channel = new HttpChannel (10001);

ChannelServices.RegisterChannel (_channel,false);

Console.WriteLine ("HTTP channel remoting service begins ...");

Selfremoteobject obj=new selfremoteobject ();

Remotingservices.marshal (obj, "selfremoteobject");

When disconnecting, pass the Disconnect method:

RemotingServices.Disconnect (obj);

Blog Park Avenue to Jane

http://www.cnblogs.com/jams742003/

Reprint Please specify: Blog Park

. NET Remoting (3)--activation, activation mode

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.