[Share] C # remoting Introduction

Source: Internet
Author: User
I found the introduction to C # remoting on the internet, and I think it is easy to understand. I would like to share it with you.
 
 
I. remoting has two channels: 1. tcpchannel Channel 2. Http channel type 2. remote object activation method 1. server-side activation, also known as wellknow. Server activation is divided into singleton and singlecall modes. Singleton mode: stateful mode. If it is set to singleton activation 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. Singlecall mode: singlecall 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. 2. Activate the client. Unlike the wellknown mode, remoting assigns a URI for each client activation type when activating each object instance. 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. Depending on the activation mode, different server-side implementations of channel types are also different. In general, the server side should be divided into three steps: 1. The registration channel must span applicationsProgram Channel is required for communication. As mentioned above, remoting provides the ichannel interface, which contains two types of channels: tcpchannel and httpchannel. In addition to the performance and the format of serialized data, these two types are implemented in the same way. Therefore, we will take tcpchannel as an example. To register a tcpchannel, first add the reference "system. runtime. remoting" in the project, and then using namespace: system. runtime. remoting. Channel. TCP. Code As follows: tcpchannel channel = new tcpchannel (8080); channelservices. registerchannel (Channel); when instantiating a channel object, pass the port number as a parameter. Then call the static method registerchannel () to register the channel object. 2. Register a remote object. to activate a remote object after registering a channel, you must register the object in the channel. The method for registering objects varies depending on the activation mode. (1) For wellknown objects in singleton mode, you can use the static remotingconfiguration method. registerwellknownservicetype (): remotingconfiguration. registerwellknownservicetype (typeof (serverremoteobject. serverobject), "servicemessage", wellknownobjectmode. singleton); (2) the method for registering an object in singlecall mode is basically the same as that in singleton mode. You only need to change the enumeration parameter wellknownobjectmode to singlecall. Remotingconfiguration. registerwellknownservicetype (typeof (serverremoteobject. serverobject), "servicemessage", wellknownobjectmode. singlecall); (3) Client activation mode for client activation mode, the methods used are different, but the difference is not big, you can see the code at a glance. Remotingconfiguration. applicationname = "servicemessage"; remotingconfiguration. registeractivatedservicetype (typeof (serverremoteobject. serverobject); why do you need to set the applicationname Attribute before registering an object method? In fact, this property is the URI of the object. For the wellknown mode, Uri is placed in the parameters of the registerwellknownservicetype () method. Of course, you can assign values to the applicationname attribute. The registeractivatedservicetype () method does not have the applicationname parameter, so it must be separated. 3. If you want to disable the remoting service, you need to cancel the channel or disable the channel listening. In remoting, channel listening is automatically enabled when we register a channel. If listening to the channel is disabled, the channel cannot accept client requests, but the channel still exists. If you want to register the Channel again, an exception is thrown. // Obtain the currently registered channel; ichannel [] channels = channelservices. registeredchannels; // disable the channel named mytcp; foreach (ichannel eachchannel in channels) {If (eachchannel. channelname = "mytcp") {tcpchannel = (tcpchannel) eachchannel; // disable the listener; tcpchannel. stoplistening (null); // logout channel; channelservices. unregisterchannel (tcpchannel) ;}} in the Code, the registerdchannel attribute obtains the currently registered channel. In remoting, multiple channels can be registered at the same time.

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.