Basics:
What is remoting? In short, we can regard it as a distributed processing method. From the perspective of Microsoft products, it can be said that remoting is an upgrade of DCOM, which improves many functions and is well integrated into the. NET platform. Microsoft. NET remoting provides a method for allowing objects to pass through applications. Program Framework for interaction between a domain and another object. This is exactly why remoting is used. Why? In Windows, applications are separated into separate processes. This process forms an application Code And a boundary around the data. If the inter-process communication (RPC) mechanism is not used, the Code executed in one process cannot access another process. This is an operating system's protection mechanism for applications. However, in some cases, We need to cross the application domain and communicate with another application domain, that is, cross the border. In remoting, a channel is used to implement object communication between two application domains. First, the client obtains the server object through the remoting access channel, and then parses it as the client object through the proxy. This provides a possibility to publish server objects as a service. Remote Object code can be run on a server (such as an object activated by the server and an object activated by the client), and then the client connects to the server through remoting, obtain the service object and run it on the client through serialization. In remoting, in addition to the channel type and port number, the designer does not need to know the data packet format for the objects to be transmitted. However, it must be noted that when obtaining the server-side object, the client does not obtain the actual server-side object, but obtains its reference. This ensures loose coupling between client and server objects, and optimizes communication performance.
Two remoting channels:
There are two main remoting channels: TCP and HTTP. In. net, system. runtime. remoting. Channels defines the ichannel interface. The ichannel interface includes the tcpchannel and HTTP channel types. They correspond to the two types of remoting channels respectively. The tcpchannel type is stored in the namespace system. runtime. remoting. channels. TCP. The TCP channel provides a socket-based transmission tool that uses the TCP protocol to transmit serialized message streams across the remoting boundary. The tcpchannel type serializes message objects in binary format by default, so it has higher transmission performance. The httpchannel type is stored in the namespace system. runtime. remoting. channels. HTTP. It provides an HTTP protocol for transmitting serialized message streams over the Internet through the firewall. By default, the httpchannel type serializes message objects in soap format, so it has better interoperability. Generally, we use tcpchannel more in the LAN; if we want to cross the firewall, we use httpchannel.
How to activate a remote object:
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. In remoting, remote object activation can be divided into two categories: server-side activation and client activation. Server activation is also called the wellknow method. Many of them are translated as well-known objects. Why is it called the activation mode of well-known objects? The reason is that 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 .. Net remoting divides server activation into Singleton mode and singlecall mode. 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 always maintain its state in 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, both clients accessing the remote object obtain 1. We can still consider ASP. NET as a session state. 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.
Comparison:
1: Socket vs remoting
Using socket is undoubtedly the most efficient. However, in a complex interface environment, the socket development efficiency is also the lowest. Therefore, with both development efficiency, remoting can be used instead of socket development. And:
1. The remoting speed of the TCP channel is very fast.
You can use the port viewing tool to find that the content transmitted by remoting is the same order of magnitude than that transmitted by direct socket. My other concern is whether the remoting transmission efficiency will be very low in the case of a large number of clients. The result is tested on the site and the data communication is performed on 300 clients, so there is no information loss.
2. Although it is remote, It is very close to the local call object.
That is, it fully complies with the object-oriented ideology.
3. The object state can be maintained.
Directly Using the socket transmission mechanism requires a lot of effort to handle exceptions, network disconnection, and crashes. Using remoting will greatly simplify this process.
2: remoting vs WebService
1. In framework2.0 status, WebService can only host on application servers such as IIS. Microsoft didn't provide servicehost to host WebService until 3.0, which greatly limits the flexibility of WebService in use. In the framework2.0 environment, if an application is stored out of IIS, You have to discard WebService. (Unless you want code to implement a Web application server)
2. remoting can be hosted in your own code, Windows Services, and IIS. It maximizes the flexibility of development and deployment.
3. When using the HTTP channel, remoting also supports penetration routing like WebService.
4. remoting provides two-way communication compared with websercie. It is supported even if you host remoting in IIS.
5. The proxy class automatically generated by the WebService client is complicated. Remoting generally writes client code manually.
6. Of course, the main advantage of WebService is that it is an industry standard, while remoting is only an internal standard of Microsoft. If your application is to break away from Microsoft's platform, you can only use WebService.
3: remoting vs WCF
Compared with WCF, it is more about the popularity of the platform. In the current environment, the popularity of 2.0 is still the highest. If 3.0 or even 4.0 of the day is popular, of course, WCF is the best.