Today read a simple example of C # remoting, Bo Master simple introduction of the remoting, the current development of the system also used remoting, deliberately relive the next system, found that in fact, it is also the case, the server-side definition of remote objects, configuration profiles, the client calls the remote object , in fact, not so tall (perhaps I have not yet understood the essence of it).
Show the code first, and then explain the mechanism (based on a simple example of C # Remoting)
1 Create the Remotesample project and compile it into a lib file.
Create three interfaces in this project (Remotearrayinterface,remotestringinterface,remotecacuteinterface, these three interfaces have different uses, dealing with strings, arrays, calculations, respectively).
Using system;using system.collections.generic;using system.linq;using system.text;namespace RemoteSample{ Public interface Remotearrayinterface { int sumarray (int[] a);} }
Using system;using system.collections.generic;using system.linq;using system.text;namespace RemoteSample{ Public interface Remotecalcuteinterface { int sum (int a, int b);} }
Using system;using system.collections.generic;using system.linq;using system.text;namespace RemoteSample{ Public interface Remotestringinterface { string combinestring (String str1, String str2);} }
2, Server Side
1) Create a remote class, inherit the MarshalByRefObject, implement the above interface
Using system;using system.collections.generic;using system.linq;using system.text;using RemoteSample;namespace remotesampleserver{Public class Remoteobject:marshalbyrefobject,remotearrayinterface,remotecalcuteinterface , Remotestringinterface {public remoteobject () { Console.WriteLine ("New Reference Added"); } public int sum (int a, int b) { return a + b; } public int Sumarray (int[] array) { int sum = 0; for (int i = 0; i < array. Length; i++) { sum + = Array[i] ; } return sum; } public string combinestring (string str1, String str2) { return str1 + "was not equal" + str2; } }}
2) Server body part
using system;using system.collections.generic;using System.Linq;using System.text;using remotesample;using system.runtime.remoting;using system.runtime.remoting.channels;using System.runtime.remoting.channels.tcp;namespace remotesampleserver{class RemoteServer {static void Main (Stri Ng[] args) {try {TcpServerChannel TcpServerChannel = new TcpServerChannel (88 88); ChannelServices.RegisterChannel (Tcpserverchannel,false); RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall); Console.WriteLine ("Press any Key"); Console.readkey (); } catch (Exception e) {Console.WriteLine (e.tostring ()); } } }}
The original blog registered channel only used a parameter, the method has been deprecated, there are two parameters, if you enable the group, the second parameter is set to True, otherwise set to False, if set to false, will not make security settings on the TCP or IPC channel invalid.
Server-side activation (Wellknow) is used here, using the SingleCall method.
3 Client
Using system;using system.collections.generic;using system.linq;using system.text;using RemoteSample;namespace Remotesampleclient{class Remoteclient {static void Main (string[] args) {Remotecalcuteint Erface calcuteremote = (remotesample.remotecalcuteinterface) Activator.GetObject (typeof ( Remotesample.remotecalcuteinterface), "Tcp://localhost:8888/remoteobject"); Console.WriteLine ("A +b = {0}", Calcuteremote.sum (1, 5). ToString ()); Remotestringinterface stringremote = (remotesample.remotestringinterface) Activator.GetObject (typeof ( Remotesample.remotestringinterface), "Tcp://localhost:8888/remoteobject"); Console.WriteLine ("{0}", Stringremote.combinestring ("Wei Xiao Bao", "Kang Xi")); int[] array = new Int[4] {1, 3, 4, 5}; Remotearrayinterface arrayremote = (remotesample.remotearrayinterface) Activator.GetObject (typeof ( Remotesample.remotearrayinterface), "Tcp://localhost:8888/remoteobject"); COnsole. WriteLine ("The sum is {0}", Arrayremote.sumarray (array)); Console.readkey (); } }}
The original client to register the channel, seemingly do not register channel, also can run normally, do not know what is the reason.
Personal understanding: Server-side can define a variety of services, each type of service design as an excuse to define a remote object implementation of these services. The client is used on demand and what type is obtained through activation.
Much of this information is set in the code, which is certainly not good in the actual project. At the moment I am contacting the system through configuration file settings, the benefits of configuration files do not need to modify the code, do not need to compile.
Some conceptual knowledge, can consult Baidu Encyclopedia.
C # Remoting Example