C#实现远程调用主要用到“System.Runtime.Remoting”这个东西。下面从三个方面给于源码实例。
·服务端:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.Remoting.Channels;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.Remoting.Channels.Tcp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.Remoting;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace RemoteSample
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
class server
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
static int Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//注册通道
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TcpChannel chan = new TcpChannel(8085);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChannelServices.RegisterChannel(chan);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string sshan = chan.ChannelName;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Console.WriteLine(sshan);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//注册远程对象,即激活.
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteSample.HelloServer), "SayHello", WellKnownObjectMode.SingleCall);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Console.WriteLine("Hit <ennter> to exit...");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Console.ReadLine();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
·客户端:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.Remoting.Channels.Tcp;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.Remoting.Channels;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace RemoteSample
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class Client
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static int Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
TcpChannel chan = new TcpChannel();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ChannelServices.RegisterChannel(chan);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HelloServer obj =(HelloServer)Activator.GetObject(typeof(HelloServer), "tcp://localhost:8085/SayHello");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (obj == null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Console.WriteLine("Could not locate server");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else Console.WriteLine(obj.HelloMethod("Caveman"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
·远程对象:(重点),该对象是一个dll的程序集,同时被客户端和服务器端引用。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace RemoteSample
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//客户端获取到服务端的对象实际是一个对象的引用,因此必须继承:MarshalByRefObject
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class HelloServer : MarshalByRefObject
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public HelloServer()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("HelloServer activated");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public String HelloMethod(String name)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Console.WriteLine("Hello.HelloMethod : {0}", name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return "Hi there " + name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//说明1:在Remoting中的远程对象中,如果还要调用或传递某个对象,例如类,或者结构,则该类或结构则必须实现串行化Attribute[SerializableAttribute]:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//说明2:将该远程对象以类库的方式编译成Dll。这个Dll将分别放在服务器端和客户端,以添加引用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//说明3:在Remoting中能够传递的远程对象可以是各种类型,包括复杂的DataSet对象,只要它能够被序列化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
注意上述代码的注释,由于远程服务的特殊性,因此在此做了详细的批注,怕大伙不理解。
OK。C#的远程调用就实现完成了,这中应用一般在三层架构中应该比较平常使用。至于这种方式的优缺点,在下还不好说,希望有过实际应用的同志给总结一些,谢谢!!!