构建.NET Remoting应用程序
步骤如下:
1:创建远程对象
选择MBV或MBR模式
2:创建远程对象服务器即远程对象的托管程序
选择通道类型并初始化通道
为远程对象注册通道
注册远程对象
3:创建客户端应用程序
初始化客户端通道,要与服务器保持一致的通道类型
为远程对象注册客户端通道
初始化并调用远程对象的方法
----------------------------------------------------------------------------------------
1:创建远程对象
// ClockServer.cs
using System;
public class Clock : MarshalByRefObject
{
public string GetCurrentTime ()
{
return DateTime.Now.ToLongTimeString ();
}
}
2:创建远程对象的托管程序
// TimeServer.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
class MyApp {
static void Main () {
TcpServerChannel channel = new TcpServerChannel (1234);
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterWellKnownServiceType
(typeof (Clock), "Clock", WellKnownObjectMode.SingleCall);
Console.WriteLine ("Press Enter to terminate...");
Console.ReadLine ();
}
}
3:创建客户端应用程序
// TimeClient.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
class MyApp {
static void Main () {
TcpClientChannel channel = new TcpClientChannel ();
ChannelServices.RegisterChannel (channel);
RemotingConfiguration.RegisterWellKnownClientType
(typeof (Clock), "tcp://localhost:1234/Clock");
Clock clock = new Clock ();
Console.WriteLine (clock.GetCurrentTime ());
}
}