Involved classes:
Client:
1. system. runtime. remoting. Channel. tcp. tcpclientchannel class: used for remote call implementation
The client channel for transmitting messages over TCP.
A channel transmits messages across remote processing boundaries (such as computer or application domains.TcpclientchannelClass uses the TCP protocol to transmit messages.
. NET Framework remote processing infrastructure uses Channel Transmission for remote calls. When the client calls a remote object, the call is serialized as a message, which is sent through the client channel and received through the server channel. Then deserialize and process it. All return values are transmitted through the server channel and received through the client channel.
2. system. runtime. remoting. Channel. channelservice class: provides static methods to help you remotely process channel registration, resolution, and URL discovery. This class cannot be inherited.
3. system. activator class: contains specific methods for creating object types locally or remotely, or obtaining references to existing remote objects. This class cannot be inherited.
Server:
1. system. runtime. remoting. Channel. channelservice class: provides help for remote channel registration, parsing, and
Static Method of URL discovery. This class cannot be inherited.
2. system. runtime. remoting. Channel. tcp. tcpserverchannel class: used for remote call implementation
The server channel for transmitting messages over TCP.
3. system. runtime. remoting. remotingconfiguration class: provides a variety of static methods for configuring remote processing structures.
4. system. runtime. remoting. wellknownobjectmode enumeration: defines how to activate known objects.
| |
Member name |
Description |
| |
Singlecall |
Each incoming message is provided by the new object instance. |
| |
Singleton |
Each incoming message is provided by the same object instance. |
Steps:
1. design your own remote object class: remoteobject.
2. The server registers the remote object with the remotingconfiguration. registerwellknownservicetype method as a wellknownservicetypeentry instance.
3. The server registers the tcpserverchannel to the channelservice.
4. The client registers a new tcpclientchannel to channelservice.
5. The client uses the activator class to obtain remote objects from the URL of the remote server.
6. The client uses a remote object.
Remote Object Code:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace RemoteSampleLib{ public class RemoteObject : MarshalByRefObject { public RemoteObject() { Console.WriteLine("New RemoteObject Added!"); } public int Add(int a, int b) { return a + b; } }}
Server code:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels.Tcp;using RemoteSampleLib;using System.Runtime.Remoting.Channels;namespace RemoteServer{ class RemoteServer { static void Main(string[] args) { TcpServerChannel channel = new TcpServerChannel(6666); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteSampleLib.RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall); System.Console.WriteLine("Press Any Key"); System.Console.ReadLine(); } }}
Client code:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using RemoteSampleLib;namespace RemoteClient{ class RemoteClient { static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel()); RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://localhost:6666/RemoteObject"); Console.WriteLine("1+2=" + remoteobj.Add(1, 2).ToString()); Console.ReadLine(); } }}
Note that the marshalbyref class should be implemented for remote objects to allow remote objects to be accessed across program boundaries.
Design Pattern reference: http://www.verydemo.com/demo_c92_i15233.html