Initial knowledge using. NET Remoting to develop distributed applications

Source: Internet
Author: User

A Introduction to. NET Remoting:

. NET remoting is, in a sense, a substitute for DCOM. ASP. NET Web services are useful, but this technology is not fast or flexible for some business requests in the enterprise intranet solution, andASP. NET Web services require runtime support. With . NET Remoting technology, you can make a Web service available anywhere in the world. And you can run the Web service in all of the application types .

Two the fundamentals of. NET Remoting:

The architecture diagram is as follows:

Three A few important concepts:

1. Remote objects:

The remote object class is derived from the MarshalByRefObject class. Calling this class across application domains requires the use of proxies. . NET Remoting supports two types of remote objects: well-known (well-known) remote objects and client-activated (client-activated) remote objects. The remote object actually includes two levels of meaning:

Manipulating remote objects: Objects running on remote, client segments sending messages to him;

Pass the remote object: Take the remote object locally, or send the local object to the past and manipulate the copy.

2. Activated:

Use the new operator to activate a remote object. There are other ways to activate remote objects, which I'll introduce in a later essay.

3. Channel:

A remote object uses the channel to send and receive messages. The server chooses a channel to listen for requests, and the client chooses the channel to communicate with the server. Remoting provides built-in channels:TCP channels and HTTP channels, and we can also write our own channels.

4. Grouping:

The process by which an array is passed through an application domain is called marshalling. When a variable is sent as a parameter to a remote object, the variable must be converted so that the variable can be sent through the application domain.

5. Listening:

With listening, some functions can be placed into the method call chain. If you invoke the method of an object, the listener layer can capture the call to convert the method calls or complete some logging. each part of the. NET remoting call chain is monitored.

Four Development Remoting Three-step walk:

Developing . NET Remoting is a three-step walk, as illustrated here in a simple example.

1. To create a remote object:

Inherit System.MarshalByRefObject

usingSystem;usingSystem.Collections;usingSystem.Text;namespacesimpleremoting{ Public classHelloserver:marshalbyrefobject { PublicHelloServer () {///output information, server activationConsole.WriteLine ("Server Activation ..."); }         Publicstring HelloMethod (string name) {Console.WriteLine ("Server side: {0}", name); return "here is:"+name; }    }}

2. To create the host application:

Register Channel

Registering a server-activated remote object

Running the host program

usingSystem;usingSystem.Net;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingSystem.Runtime.Remoting.Channels.Http;namespacesimpleremoting { Public classServer { Public Static intMain (string[] args) {                        ///Create a TCP channelTcpChannel Chan1 =NewTcpChannel (8085); ///creating an HTTP channelHttpChannel chan2 =NewHttpChannel (8086); ///Register ChannelChannelServices.RegisterChannel (CHAN1);            ChannelServices.RegisterChannel (CHAN2); RemotingConfiguration.RegisterWellKnownServiceType (typeof(HelloServer),"SayHello", Wellknownobjectmode.singleton); System.Console.WriteLine ("Press any key to exit! "); ///the following line must not be lessSystem.Console.ReadLine (); return 0; }    }}

3. To create a client program:

Register Channel

get an object proxy based on URL

Invoking a remote object using a proxy

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingSystem.Runtime.Remoting.Channels.Http;usingSystem.IO;namespacesimpleremoting { Public classClient { Public Static voidMain (string[] args) {            ///using TCP channels to get remote objectsTcpChannel Chan1 =NewTcpChannel ();            ChannelServices.RegisterChannel (CHAN1); HelloServer Obj1=(HelloServer) Activator.GetObject (typeof(simpleremoting.helloserver),"Tcp://localhost:8085/sayhello"); if(Obj1 = =NULL) {System.Console.WriteLine ("failed to connect to the TCP server"); }            ///using the HTTP channel to get the remote objectHttpChannel chan2 =NewHttpChannel ();            ChannelServices.RegisterChannel (CHAN2); HelloServer Obj2=(HelloServer) Activator.GetObject (typeof(simpleremoting.helloserver),"Http://localhost:8086/SayHello"); if(Obj2 = =NULL) {System.Console.WriteLine ("failed to connect to HTTP server"); }                        ///Output InformationConsole.WriteLine ("clienttcp hellomethod {0}", Obj1. HelloMethod ("Caveman1")); Console.WriteLine ("clienthttp hellomethod {0}", Obj2. HelloMethod ("Caveman2"));        Console.ReadLine (); }    }}

Concluding remarks: The first knowledge of using. NET remoting to develop distributed applications is here, and I have time to write a series of articles on. NET Remoting technology. Includes lease-based lifetimes, marshalling, asynchronous remote calls, and so on.

Source: http://www.cnblogs.com/Terrylee/archive/2005/11/03/267621.html

Initial knowledge using. NET Remoting to develop distributed applications

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.