Summary
Remote execution is an infrastructure that enables developers to use remote objects in C #. A remote object is an object that is outside the caller's application domain. The examples in this article illustrate how to use two remote object access mechanisms (value delivery and address delivery), and it also illustrates the powerful capabilities of remote objects in distributed computing through a simple, powerful task server implementation.
The task server can accept all objects that implement the ITask interface and run them in its application domain. More importantly, it can accept tasks from multiple clients at a time.
After completing this article, the reader will be able to:
━━ establish a connection between server/client objects.
━━ passes objects by value.
━━ passes an object by address.
━━ understand the concept of remote task assignment.
Remote objects
A remote object passes an object through an address or through the value of an object.
In the first case, the address of an object is passed by application domain A to application domain B, but the object's method invocation is between application domain A and application domain B. The object exists and runs in application domain A, but it is also like a local object in application domain B.
In the second case, the entire object and its associated entities (called object graphs) are serialized into byte form and transferred from application domain A to application domain B. The object is then "drag" in application domain B and reverts to its original state. The object now exists and runs on application domain B.
Establish object host (also known as server)
The first step in setting up a server is to establish a channel between the two application domains in which the object communicates, which can be a TCP/IP channel or an HTTP channel. The TCP channel is fast, it is suitable for the network use that the packet transmission in the network is less limited, the HTTP channel is more flexible, it is suitable to use on the internet and so on WAN.
We will use the TCP/IP channel and will run both the server and client side on the same machine with two different application domains. Therefore, enter the following code to create the channel MyChannel on the 8065 port on the TCP/IP stack:
TcpChannel myChannel = new TcpChannel(8065);
Here is the direction. NET Channel service registers the MyChannel channel, which allows the channel to be accessed outside the server application domain. We can do this by using the following code:
ChannelServices.RegisterChannel(myChannel);
The last step is to tell. NET remote execution infrastructure about the objects we want to open, we need to advertise the type and location of the object, the name that the client uses to locate the object, and the. NET's remote infrastructure on how to handle this object invocation. We can get the type of the object through the following code:
Type objectType = new MyCoolObject().GetType()
You can use the following code to. NET remote and infrastructure registers the object:
RemotingConfiguration.RegisterWellKnownServiceType(
objectType, "MyCoolObject",
WellKnownObjectMode.Singleton);
There are two ways to handle the invocation of an object: Singleton and SingleCall. In the Singleton method, the object is created at the first client method call, and the object exists until the client aborts the connection or the object dies naturally; In the SingleCall mode, each client's method call creates an object that exists only during a method call, once the method call has ended , the object will die. In SingleCall mode, the client connection is not aborted with the end of the method call, only the object is killed as the method call ends.