刚接触.NET不久。自己琢磨了一下Remoting。发现写一个Remoting的程序不是很复杂。
这类程序大致分为Server端和Client端。在Server端写一个类使其继承
System.MarshalByRefObject这个类就可以在Client端进行调用了.server端主要任务就是使服务启动,注册一个通道,使客户端可以调用.安装部署时候客户端需要放置一个与Server端一样的dll文件提供一个空壳,告诉client端Server端有哪些方法可以调用.
我觉得服务端和客户端最好利用接口来调用,这样就不必在客户端放置一个和服务端一样的dll文件了.
举个例子吧。
Remoting的一个简单例子。
首先在服务端写几个你需要用到的类例子中用到了CMath这个类不过这个类必须是继承System.MarshalByRefObject才能注册为可供客户端调用的类。然后可以用以下语句注册成为SingleCall模式。
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(CMath), "math.rem", WellKnownObjectMode.SingleCall);
服务端的任务完成了。
之后是如何使服务端可以相应客户端的调用。
可以把服务端写成Service。也可以自己写个控制台程序用来启动服务。
static void Main(string[] args)
{
//定义一个通道,端口8085
TcpChannel chan = new TcpChannel(8085);
//注册通道
ChannelServices.RegisterChannel(chan);
CServerRun.RegisterServiceType();
//Hold the server, wait for client
System.Console.WriteLine("Hit to exit...");
System.Console.ReadLine();
}
运行控制台程序这样服务就起来了。
之后是客户端的程序:
public static object GetObject(System.Type InterfaceType, string objectRegName)
{
if(chan == null)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
}
//Create the remote object
object obj = Activator.GetObject( InterfaceType, "tcp://localhost:8085/ + "math.rem");
return obj;
}
这段代码在客户端注册和服务端的通道连接,同时取回刚才在服务端
建立的对象。参数objectRegName是你要取得对象的名称。我写死了。
这样就可以在客户端调用服务端注册好的组件了。
这类程序大致分为Server端和Client端。在Server端写一个类使其继承
System.MarshalByRefObject这个类就可以在Client端进行调用了.server端主要任务就是使服务启动,注册一个通道,使客户端可以调用.安装部署时候客户端需要放置一个与Server端一样的dll文件提供一个空壳,告诉client端Server端有哪些方法可以调用.
我觉得服务端和客户端最好利用接口来调用,这样就不必在客户端放置一个和服务端一样的dll文件了.
举个例子吧。
Remoting的一个简单例子。
首先在服务端写几个你需要用到的类例子中用到了CMath这个类不过这个类必须是继承System.MarshalByRefObject才能注册为可供客户端调用的类。然后可以用以下语句注册成为SingleCall模式。
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(CMath), "math.rem", WellKnownObjectMode.SingleCall);
服务端的任务完成了。
之后是如何使服务端可以相应客户端的调用。
可以把服务端写成Service。也可以自己写个控制台程序用来启动服务。
static void Main(string[] args)
{
//定义一个通道,端口8085
TcpChannel chan = new TcpChannel(8085);
//注册通道
ChannelServices.RegisterChannel(chan);
CServerRun.RegisterServiceType();
//Hold the server, wait for client
System.Console.WriteLine("Hit to exit...");
System.Console.ReadLine();
}
运行控制台程序这样服务就起来了。
之后是客户端的程序:
public static object GetObject(System.Type InterfaceType, string objectRegName)
{
if(chan == null)
{
chan = new TcpChannel();
ChannelServices.RegisterChannel(chan);
}
//Create the remote object
object obj = Activator.GetObject( InterfaceType, "tcp://localhost:8085/ + "math.rem");
return obj;
}
这段代码在客户端注册和服务端的通道连接,同时取回刚才在服务端
建立的对象。参数objectRegName是你要取得对象的名称。我写死了。
这样就可以在客户端调用服务端注册好的组件了。