.Net Remoting 提供 .Net 应用高效的分布式运算能力,一个最简单的示例便于了解其使用机制。
使用接口,因为可以使用其它的实现替换现有模块,而使用它的模块却不需要做任何修改,所以,这种方式可以提供更灵活的实现,所以写了一个 Hello world 应用,以为示例。
对于大多数 .Net Remoting 应用,都需要有一个公共项目,同时被客户端和服务器端引用,在这个例子中,公共项目中只有一个接口 ICommand :
对于服务器端,需要有一个实现此接口的类,在这里实现的是 ConsoleOutCommand,为了让它在服务器端,而不是客户端运行,必须从 MarshalByRefObject 继承:
另外,服务器端需要注册成一个 Remoting 的服务器,当然,根据需要,可以分别注册成 SingleCall 或者 Singleton,在这里使用的是 SingleCall:
而客户端,只需要通过 Remoting 协议,取得这个对象的代理就可以使用它了:
这样,这个例子就可以运行了。
另外,如果要传送的参数是一个自定义的类,则必须保证它是可序列化的,可以通过标记成 Serializable 或者继承 ISerializable 接口,一般来说,标记成 Serializable 就可以了:
使用接口,因为可以使用其它的实现替换现有模块,而使用它的模块却不需要做任何修改,所以,这种方式可以提供更灵活的实现,所以写了一个 Hello world 应用,以为示例。
对于大多数 .Net Remoting 应用,都需要有一个公共项目,同时被客户端和服务器端引用,在这个例子中,公共项目中只有一个接口 ICommand :
using System;
namespace Sample.Remoting
{
public interface ICommand
{
void Run(string Command);
}
}
namespace Sample.Remoting
{
public interface ICommand
{
void Run(string Command);
}
}
对于服务器端,需要有一个实现此接口的类,在这里实现的是 ConsoleOutCommand,为了让它在服务器端,而不是客户端运行,必须从 MarshalByRefObject 继承:
using System;
namespace Sample.Remoting
{
public class ConsoleOutCommand : MarshalByRefObject, ICommand
{
public ConsoleOutCommand()
{
}
public void Run(string Command)
{
Console.WriteLine(Command);
}
}
}
namespace Sample.Remoting
{
public class ConsoleOutCommand : MarshalByRefObject, ICommand
{
public ConsoleOutCommand()
{
}
public void Run(string Command)
{
Console.WriteLine(Command);
}
}
}
另外,服务器端需要注册成一个 Remoting 的服务器,当然,根据需要,可以分别注册成 SingleCall 或者 Singleton,在这里使用的是 SingleCall:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Sample.Remoting
{
class Program
{
[STAThread]
static void Main(string[] args)
{
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ConsoleOutCommand), "Sample", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Hit <enter> to exit...");
Console.ReadLine();
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Sample.Remoting
{
class Program
{
[STAThread]
static void Main(string[] args)
{
TcpChannel chan = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ConsoleOutCommand), "Sample", WellKnownObjectMode.SingleCall);
System.Console.WriteLine("Hit <enter> to exit...");
Console.ReadLine();
}
}
}
而客户端,只需要通过 Remoting 协议,取得这个对象的代理就可以使用它了:
using System;
using System.Runtime.Remoting;
namespace Sample.Remoting
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string url = "tcp://localhost:8085/Sample";
Type type = typeof(ICommand);
ICommand Command = (ICommand)Activator.GetObject(type, url);
Command.Run("Remoting out!!!");
}
}
}
using System.Runtime.Remoting;
namespace Sample.Remoting
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string url = "tcp://localhost:8085/Sample";
Type type = typeof(ICommand);
ICommand Command = (ICommand)Activator.GetObject(type, url);
Command.Run("Remoting out!!!");
}
}
}
这样,这个例子就可以运行了。
另外,如果要传送的参数是一个自定义的类,则必须保证它是可序列化的,可以通过标记成 Serializable 或者继承 ISerializable 接口,一般来说,标记成 Serializable 就可以了:
[Serializable]
public class Student
{
public string Name;
public string Password;
public int Age;
}
public class Student
{
public string Name;
public string Password;
public int Age;
}