. NET Remoting learning notes (2) activation method, remoting learning notes

Source: Internet
Author: User

. NET Remoting learning notes (2) activation method, remoting learning notes
Directory

  • . NET Remoting learning notes (1) Concepts
  • . NET Remoting Study Notes (2) Activation Method

 

Reference: Baidu encyclopedia♂Windmill. Net

 

Activation Method Concept

Before accessing an object instance of the remote type, you must create it and initialize it through a process named Activation. This client creates a remote object through a channel, which is called object activation.

Activation is divided into two categories: Activate the client on the server

 

Server activation

Also known as WellKnow (well-known object)

The server application will publish this type in 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 activation is divided:SingleTon mode SingleCall Mode

 

SingleTon 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.

Below is the code:

1. Create a remote call processing class

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 SendMessageHandler SendMessageEvent ;/* Send message */[SoapMethod (XmlNamespace = "MessageMarshal", SoapAction = "MessageMarshal # SendMessage")] public void SendMessage (string messge) {if (SendMessageEvent! = Null) SendMessageEvent (ID. ToString () + "\ t" + messge );}}}View Code

2. Create server 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 );}}}View Code

3. Create client 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 );}}}}View Code

4. After running the server, open two client programs. The result is as follows:

In the Code diagram, when TestMessageMarshal has a new instance, its constructor creates different identifiers (guids). The server receives data requests from the client and outputs the ID numbers to the interface, the interface shows that the server processes requests from multiple clients through one channel (one instance.

 

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.

We modify the server code as follows, and the client does not need to be modified:

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

Enable the server, and then open a client, as shown below:

The output results show that each server creates a remote object instance for each client request.

 

Client Activation

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.

1. Modify the server 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 );}}}View Code

2. Modify client 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 );}}}}View Code

3. Test and enable the server and two clients.

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

The test directory structure is as follows, otherwise the remote client object does not correspond to the server. "Not foundRequested service "Exception

This is the three Remoting activation methods. If you have any questions, please correct them.

Author: Sakya bitter monk Source: http://www.cnblogs.com/woxpp/p/3995366.html this article copyright belong to the author and blog park a total, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection.

 


Net Remoting technical issues

. NET remote processing provides an abstract method for inter-process communication, which isolates objects that can be remotely processed from specific client or server application domains and specific communication mechanisms. Therefore, this is flexible and easy to customize. You can use one communication protocol to replace another communication protocol, or use one serialization format to replace another serialization format without re-compiling the client or server. In addition, the remote processing system assumes that there is no special application model. You can use Web applications, console applications, and Windows Services to communicate with almost any program you want. The remote processing server can also be an application domain of any type. Any application can process objects remotely and provide services to any client on its computer or network.
The System. Runtime. Remoting namespace provides classes and interfaces that allow developers to create and configure distributed applications. Some more important classes in the System. Runtime. Remoting namespace are the RemotingConfiguration class, RemotingServices class, And ObjRef class.
The RemotingConfiguration class contains static methods used to connect with configuration settings. The RemotingConfiguration. Configure method allows developers to Configure the remote processing infrastructure by formatting the configuration file in XML. The RemotingConfiguration class also contains several methods for registering the client activation object and the server activation object that reside on the server on the client and the server.
The RemotingServices class provides several methods to help you use and publish remote objects. System. runtime. remoting. remotingServices. the Marshal method provides the ability to store all relevant information required to activate a remote object and communicate with it in an ObjRef instance for future serialization and transmission to a remote location. The System. Runtime. Remoting. RemotingServices. Unmarshal method reverses this process by creating a proxy for remote objects that can be used by applications, regardless of any remote processing branch.
The ObjRef class saves all relevant information required to activate the remote object and communicate with it. This class is a serialized representation of objects transmitted over a channel to a remote location. It is unblocked from being sent over a channel (see Unmarshal) and can be used to create a local proxy for a remote object.

All of the above are MSDN materials. Learn to read MSDN.

The difference between WebService and Remoting in the net Framework

The difference between Web Service and Remoting is as follows:
Web services are divided into five levels:
1. Http Transmission Channel
2. XML data format
3. SOAP Encapsulation Format
4. Description of WSDL
5. UDDI
The Web Service structure in. NET is relatively simple and easy to understand and apply:
Generally, WebService applications in the. NET structure are based on the. net framework and IIS architecture, so it is relatively easy to deploy.
From the implementation perspective,
First, WebService must inherit the class of the method exposed to the client from the base class: System. Web. Services. WebService.
Second, the exposed methods must be preceded by [WebMethod] or [WebMethodAttribute].
Running Mechanism of WebService
First, the client uses the WSDL from the server to the WebService, and claims a Proxy Class on the client)
This proxy class is responsible for Request and Response with the WebService Server
When a data (in XML format) is encapsulated into a data stream in SOAP format and sent to the server, a process object is generated and the SOAP packet that receives the Request is parsed, then, the transaction is processed. After the processing is completed, the computation result is packaged with SOAP, and then the package is used as a Proxy Class sent to the client as a Response. Similarly, this proxy class also parses the SOAP package and then performs subsequent operations.

This is a running process of WebService.

And. net Remoting is:
. Net Remoting is a technology developed on the basis of DCOM. Its main purpose is to achieve cross-platform, cross-language, and penetration of Enterprise Firewall, which is also its basic feature, different from WebService, WebService supports HTTP and TCP channels. It can not only transmit SOAP packets in XML format, but also transmit binary streams in the traditional sense, this makes it more efficient and flexible. In addition, it does not rely on IIS, and users can develop (Development) and deploy their favorite host servers. Therefore, in these aspects, WebService is actually a special case of. net Remoting.

WebService and Remoting are compared as follows:
WebService features Platform-independent, cross-language (any language that supports XML), and enterprise firewall penetration.
However, its disadvantage is that a Web Server needs to be deployed, and the speed is relatively slow;

. Net Remoting features
The advantage is that users can use both TCP-based binary stream communication and HTTP-based SOAP-based communication.
The efficiency is much higher than that of WebService, but its disadvantages are also obvious.. net remoting can only be applied under the. net framework of MS.
In terms of performance, the efficiency of Remoting is very similar to that of traditional DCOM and COM +!

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.