I have been learning Remoting over the past few days. I have read many posts on the Internet. I think they write well, but none of them have introduced all the content I need, however, each has its own focus. For the sake of memo, I have integrated the content of these posts. It is a learning note. It should be used as the memo content to prevent some things from being remembered in the future, let's go over it. If you have any mistakes, please correct them.
I. Meaning of Remoting
Remoting means remote access, that is, allowing you to access and call remote objects across application domains, machines, or even LAN.
Ii. Remoting Channel
Cross-origin access to remote objects involves communication, and the intermediary for communication between the two sides is the channel. There are two main Remoting channels: Tcp and Http. The Tcp channel is Socket-based and transmits message streams through the Tcp protocol. The Http Channel transmits message streams through the Http protocol. Its biggest advantage is that it can pass through the firewall.
Therefore, if you want to write the Remoting program in the LAN, you can use the Tcp channel, which is fast. If the program is on the internet and needs to pass through the firewall, you need to use the Http channel.
Iii. Remoting procedure
Declare an Object on the server, register a channel, and publish the Object to the channel. Wait for the client to obtain the Object. The client also registers the same channel to obtain the Object (not the Object itself, but a proxy ), in this case, you can use the remote Object as if using a local Object on the client.
1. Object Declaration
This step is simple, just creating a class. Note that because the instance of this class is remotely transferred, it must inherit from the base class alalbyrefobject. It is best to encapsulate objects that need to be remotely transferred in the same Dll, because the server and client must have signatures for these classes. If an object to be remotely transmitted is only a structure (struct), you only need to use the data in your business logic. You do not need to call the method, or inherit MarshalByRefObject, however, you need to add the [Serializable] attribute to its definition so that it can be serialized.
2. Register Channel 1 // Tcp Channel
2 TcpChannel channel = new TchChannel (8080 );
3 ChannelServices. RegisterChannel (channel, false );
4 // Http Channel
5 HttpChannel channel = new HttpChannel (8080 );
6 ChannelServices. RegisterChannel (channel, false );
7
3. Publish remote objects
When this Object is published on the server, it can be instantiated or not instantiated (only the Object type is provided and not initialized, that is, it does not call its constructor)
Let's talk about the situation that is not instantiated:
For example, I have defined a class Say. For convenience, I use the interface ISay to express its specific implementation, as shown below: 1 interface ISay
2 {
3 void SayHello ();
4}
The process of instantiating uninstantiated objects is called "activation". There are two different Remoting methods.
1) server activation is also called the WellKnow method, and many of them are translated as well-known objects. As the name suggests, this object is instantiated on the server. 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.
There are two server face activation modes: SingleTon and SingleCall.
SingleTon mode: SingleTon mode provides the same object for all clients. Similar to global variables, SingleTon mode is a stateful mode, that is, the modifications made by a client to this object, it will affect the access of another client to this object. 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. 1 // Server Release
2RemotingConfiguration. RegisterWellKnownServiceType (typeof (ISay), "SayHello", WellKnownObjectMode. Singleton );
3
4
5 // client call
6 ISay sayer = (ISay) Activator. GetObject (typeof (ISay), "tcp: // localhost: 8080/SayHello ");
7sayer. SayHello ();
I personally think that the exact time for Object Instantiation should be the first time when the SayHello method was called. The proxy will send the call information back to the server, and the server will find that this object does not exist in the channel, create this object.
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.
1 // Server Release
2RemotingConfiguration. RegisterWellKnownServiceType (typeof (ISay), "SayHello", WellKnownObjectMode. SingleCall );
3
4
5 // client call
6 ISay sayer = (ISay) Activator. GetObject (typeof (ISay), "tcp: // localhost: 8080/SayHello ");
7sayer. SayHello ();