sing System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemoteServer
{
class MyServer
{
[STAThread]
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(9999);
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.MyObject),
"RemoteObject", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Press Any Key");
System.Console.ReadLine();
}
}
}
4.3Client端
新建项目 RemoteClient(控制台), Client端也要引用MyObject的DLL。
using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemoteClient
{
class MyClient
{
[STAThread]
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel());
RemoteObject.MyObject remoteobj = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),
"tcp://localhost:9999/RemoteObject");
Console.WriteLine("1 + 2 = " + remoteobj.Add(1, 2).ToString());
Console.WriteLine("i = " + remoteobj.Count().ToString());
Console.ReadLine();
}
}
}
好了,一次运行生成的RemoteServer.exe和RemoteClient.exe ,你就会发现原来Remoting是这样简单。 |