This article introduces several simple concepts of net remoting and tries to help you understand what remoting is. At the same time, this article includes an example of using HTTP channel to call the remoting server, and discusses the differences between different server objects and the object lifecycle.
Through this articleArticleYou can understand the basic concepts of remoting and know the differences between several server-side objects and the concept of Server Object lifecycle.
About remoting
I believe that many people see remoting in different places. In fact, its concept is very simple. We use remoting for different applications.ProgramCommunication between two computers, whether on the same computer or on different computers in the LAN, even on different Internet Operating Systems (of course, the corresponding operating system must also be implemented.. NET Framework ). Remoting provides the following functions:
1) communication between different processes.
2) communication between objects of different processes in different application domains (appdomain) (multiple communication protocols can be used ).
. Net remoting framework
To use remoting,. NET provides a complete framework to make the distributed call transparent. Shows its framework:
Figure 1. Net remoting Architecture
The remoting concepts commonly used include:
Remoting channel: this refers to the communication protocol between the client and the server. For example, we can use TCP or HTTP.
Serializer: the format used for data transmission. For example, we can use binary or soap to transmit XML data.
. Net tries to simplify the programming of these concepts, so the protocol and format described above can be switched by changing the configuration file. This is also a problem that programmers do not have to worry about. For example, the content of a typical client configuration file is:
| <Configuration> <System. runtime. remoting> <Application> <Channels> <Channel ref = "HTTP" clientconnectionlimit = "200"> <Clientproviders> <Formatter ref = "binary"> </Clientproviders> </Channel> </Channels> </Application> </System. runtime. remoting> </Configuration> |
RemotingA typical example
Let's first look at a typical instance. We will have a server program server.exeand a client program caoclient.exe. The client program calls the remotetype. dll object and method on the server through the HTTP channel. We will check the results of these calls under several different server objects.
ServerCode
Server. CS Using system; Using system. runtime. remoting;Public class server { Public static void main (string [] ARGs ){ // Load the configuration file Remotingconfiguration. Configure ("server.exe. config "); Console. writeline ("the server is listening. Press enter to exit ...."); Console. Readline (); Console. writeline ("GC 'ING ."); GC. Collect (); GC. waitforpendingfinalizers (); } } |
Table 1: Server. CSSource code
Server.exe. config <System. runtime. remoting> <Application> <Service> <Activated type = "clientactivatedtype, remotetype"> </Service> <Channels> <Channel ref = "HTTP" Port = "8088"> </Channels> </Application> </System. runtime. remoting> </Configuration> |
Table 2: source code of server.exe. config
Remotetype. CS Using system; Using system. runtime. remoting. lifetime; Using system. Security. Principal;Public class clientactivatedtype: financialbyrefobject { Private int I; // Override the lease settings for this object Public override object initializelifetimeservice (){ Return NULL; } Public String remotemethod (){ // Announce to the server that we 've been called. Console. writeline ("clientactivatedtype. remotemethod called ."); // Report our client identity name I = This. gethashcode (); Return "remotemethod called." + I; } Public String remotemethod1 (){ Return "remotemethod1 called." + I; } } |
Table 3: remotetype. Cs source code
Client code
Caoclient. CS Using system; Using system. runtime. remoting; Using system. runtime. remoting. lifetime;Public class client { Public static void main (string [] ARGs ){ // Load the configuration file Remotingconfiguration. Configure ("caoclient.exe. config "); Clientactivatedtype caobject = new clientactivatedtype (); Console. writeline ("client-activated object:" + caobject. remotemethod ()); Console. writeline ("client-activated object:" + caobject. remotemethod1 ()); Console. writeline ("press enter to end the client application domain ."); Console. Readline (); } } |
Table 4 source code of caoclient. CS
Caoclient.exe. config <Configuration> <System. runtime. remoting> <Application> <Client url = "http: // localhost: 8088"> <Activated type = "clientactivatedtype, remotetype"> </Client> <Channels> <Channel ref = "HTTP" Port = "0"> </Channels> </Application> </System. runtime. remoting> </Configuration> |
Table 5: source code of caoclient.exe. config
Compile a file
Use "Visual Studio. NET command prompt =" to compile the above files respectively:
CSC/Target: Library remotetype. CS CSC server. CS CSC-Reference: remotetype. dll caoclient. CS You can see three output files: remotetype. dll, server.exe, and caoclient.exe. Run the remoting Program Start server.exe in Command Line Mode Start caoclient.exe in Command Line Mode |
Program Analysis
This is a very simple remotingprogram. If you drop two configuration files (server.exe.configand caoclient.exe. config), you can hardly see the difference between it and non-remoting programs. In fact, in the two configuration files, we only configured the remoting channel (http: 8088 ).
Why remoting?
We all know that the current web applications are all multi-layer architectures: data layer, business logic layer, and presentation layer. This advantage is the separation of code and performance. This is a common idea of program design. xml and XSLT are also based on the same idea.
Suppose we can write two presentation layers for a program, one is web application, the other is Windows application, and the main business logic is placed on the logic layer. If the classes and objects at the business logic layer are used as remoting objects, we can easily implement this. A specific example is duwamish, which comes with Visual Studio. NET. This is a good example with many good design ideas.
Remoting has many other application scenarios, which are not described here.
Three different remoting server objects
There are three types of remoting server objects, each of which has slight differences with the client interaction mode and lifecycle. In this article, we will explain the differences between these three server objects.
Client activated object
This object is declared on the server in the following way:
<Service> <Activated type = "clientactivatedtype, remotetype"> </Service> |
The client declaration method is:
<Activated type = "clientactivatedtype, remotetype">
Table 6: configuration file of client activated object
In this method of calling a server object, the client maintains a fixed link to the server object call. Therefore, the value of variable I can be saved between two method calls. See table 1 ~ 5. The returned result of the Code is:
Client-activated object: remotemethod called, 162. Client-activated object: remotemethod1 called, 162. |
Of course, there is also an object lifecycle problem. If the object lifecycle is exceeded, the server-side object will be reclaimed by the garbage collection program. This is a very complicated problem. This article does not elaborate. We just use the following function to set the lifecycle to infinite.
Public override object initializelifetimeservice (){ Return NULL; } |
If you are interested in the lifecycle of server objects, refer:
Http://msdn.microsoft.com/library/default.ASP? Url =/library/en-US/cpguide/html/cpconlifetimeleases. asp Single call object |
This object is declared on the server in the following way:
<Service> <Wellknown mode = "singlecall" type = "clientactivatedtype, remotetype" objecturi = "remotetype. Rem"> </Service> |
The client declaration method is:
<Client url = "http: // localhost: 8088"> <Wellknown type = "clientactivatedtype, remotetype" url = "http: // localhost: 8088/remotetype. Rem"> </Client> |
Table 7: single call object configuration file
In this method of calling the server object, the client creates a new connection for each call to the server object. Therefore, the value of variable I cannot be saved between two method calls. See table 1 ~ 5. The returned result of the Code is:
Client-activated object: remotemethod called, 78. Client-activated object: remotemethod1 called, 0. |
Singleton object
This object is declared on the server in the following way:
<Service> <Wellknown mode = "Singleton" type = "clientactivatedtype, remotetype" Objecturi = "remotetype. Rem"> </Service> |
The client declaration method is:
<Client url = "http: // localhost: 8088"> <Wellknown type = "clientactivatedtype, remotetype" url = "http: // localhost: 8088/remotetype. Rem"> </Client> |
Table 7: single call object configuration file
In this method of calling server-side objects, no matter how many clients there are, there will always be only one server-side object. See table 1 ~ 5. The returned result of the Code is:
Client-activated object: remotemethod called, 78. Client-activated object: remotemethod1 called, 78. |
At this time, if another client also starts: caoclient.exe, the returned result is:
Client-activated object: remotemethod called, 78. Client-activated object: remotemethod1 called, 78. |
Simple comparison of three objects
From the comparison above, we can see that Singleton has the highest efficiency among the three server objects, but all client calls can only maintain one server object; client activated has the lowest efficiency, however, it is best to block remoting, just like a local call object. You can select different remoting Objects Based on different application scenarios.
Differences between remoting and Web Service
What is the difference between remoting and web servcie? The following table shows a simple comparison:
Because Web Service is a simple loosely coupled structure, the object state is not saved. This is a bit like the single call object in remoting. Similarly, Web Service does not currently support event and callback functions. In comparison, remoting also supports highly efficient binary encoding.
However, remoting can only run on. NET Framework, and Web services have more and more flexible options.