Introduction to. Net remoting Program Development (III)

Source: Internet
Author: User
Iv. Client

The client mainly performs two tasks: first, registering a channel. As shown in figure 1, both the server side and the client side of remoting must transmit messages through channels to obtain remote objects. The second step is to obtain the remote object.

1. Registration channel:

Tcpchannel channel = new tcpchannel ();
Channelservices. registerchannel (Channel );

Note that when the client instantiates a channel, it is the default constructor called, that is, no port number is passed. In fact, this port number is indispensable, but its designation is put behind as part of the URI.

2. Obtain the remote object.

Similar to the server, different activation modes determine the implementation of the client. However, the difference is only the difference between the wellknown activation mode and the client activation mode. For singleton and singlecall modes, the client implementation is identical.

(1) wellknown activation mode

To obtain a well-known remote object on the server, you can obtain it using the GetObject () method of the activator process:

Serverremoteobject. serverobject serverobj = (serverremoteobject. serverobject) activator. GetObject (
Typeof (serverremoteobject. serverobject), "TCP: // localhost: 8080/servicemessage ");

First, it is activated in wellknown mode. The client obtains the object by using GetObject (). The first parameter is the remote object type. The second parameter is the server Uri. For the HTTP channel, http: // localhost: 8080/servicemessage is used. Because I use a local machine, it is localhost. you can replace it with a specific server IP address. The port must be the same as the server port. The following is the remote object service name defined by the server, that is, the content of the applicationname attribute.

(2) Client activation mode

As mentioned above, the wellknown mode can only call the default constructor when creating an object on the client.CodeThis is explained because the GetObject () method cannot pass the parameters of the constructor. The client activation mode allows you to create remote objects through custom constructor.

There are two methods to activate the client:

1) Call the static method registeractivatedclienttype () of remotingconfiguration (). The return value of this method is void, which only registers the remote object on the client. The object class constructor must be called for actual instantiation.

Remotingconfiguration. registeractivatedclienttype (typeof (serverremoteobject. serverobject ),
"TCP: // localhost: 8080/servicemessage ");
Serverremoteobject. serverobject serverobj = new serverremoteobject. serverobject ();

2) Call the createinstance () method of the activator process. This method creates class objects of the specified type of method parameters. Unlike the previous GetObject () method, GetObject () is used to call constructor on the client, while GetObject () only obtains the object, and instance creation is completed on the server. The createinstance () method has many reloads. I will focus on two of them.

A. Public static object createinstance (type, object [] ARGs, object [] activationattributes );

Parameter description:

Type: Type of the object to be created.
ARGs: array of parameters that match the number, sequence, and type of parameters to call the constructor. If ARGs is an empty array or a null reference (nothing in Visual Basic), the constructor without any parameters is called (default constructor ).
Activationattributes: contains one or more arrays of attributes that can be activated.

The args parameter here is an object [] array type. It can pass parameters in the constructor of the object to be created. Here, we can draw a conclusion: the remote object class passed by the wellknown activation mode can only use the default constructor, while the activated mode can be used for user-defined constructor. The activationattributes parameter is usually used to transmit the server URL in this method.

Assume that our remote object class serverobject has a constructor:

Serverobject (string pname, string comment X, int page)
{
Name = pname;
Sex = sex X;
Age = page;
}

The implementation code is:

Object [] attrs = {New urlattribute ("TCP: // localhost: 8080/servicemessage ")};
Object [] objs = new object [3];
Objs [0] = "wayfarer ";
Objs [1] = "male ";
Objs [2] = 28;
Serverremoteobject. serverobject = activator. createinstance (
Typeof (serverremoteobject. serverobject), objs, attrs );

As you can see, the objs [] array transmits the parameters of the constructor.

B. Public static objecthandle createinstance (string assemblyname, string typename, object [] activationattribute );

Parameter description:

Assemblyname: searchesProgramSet Name. If assemblyname is a null reference (nothing in Visual Basic), search for the Assembly being executed.
Typename: name of the preferred type.
Activationattributes: contains one or more arrays of attributes that can be activated.

The parameter description is clear at a glance. Note that the returned value of this method is of the objecthandle type, so the code is different from the previous one:

Object [] attrs = {New urlattribute ("TCP: // localhost: 8080/echomessage ")};
Objecthandle handle = activator. createinstance ("serverremoteobject ",
"Serverremoteobject. serverobject", attrs );
Serverremoteobject. serverobject OBJ = (serverremoteobject. serverobject) handle. Unwrap ();

This method is actually the default constructor called. The objecthandle. Unwrap () method returns the encapsulated object.

Note: To use urlattriation, you must add using system. runtime. remoting. activation in the namespace;

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.