标签:register library run main void reg read 技术 分享
.Net对于远程调用提供了两种方法:Remoting和WebService。
WebService现在是如火如荼,特别是有一种比较流行的架构:Winform+WebService(Java、.Net),
我曾经做过的一个项目就是这样子的,分布式、跨平台、极佳的用户体验,这三者结合起来是不是很诱人?
不过,这里我只说Remoting,Remoting具有以下特点:
1、Tcp通道的Remoting速度非常快
2、虽然是远程的,但是非常接近于本地调用对象
3、可以做到保持对象的状态
4、没有应用程序限制,可以是控制台,winform,iis,windows服务承载远程对象
缺点:
1、不是标准的应用,因此有平台限制
2、脱离iis的话需要有自己的安全机制
可以看出来,比起WebService,Remoting更适合于中小型局域网应用,而不适用于企业级的应用。
下面给出一个极其简单的Sample:
Remoting用的对象:
 {
{ public class RemoteObject : System.MarshalByRefObject
    public class RemoteObject : System.MarshalByRefObject {
    { public RemoteObject()
        public RemoteObject() {
        { System.Console.WriteLine("New Referance Added!");
            System.Console.WriteLine("New Referance Added!"); }
        }
 public int sum(int a, int b)
        public int sum(int a, int b) {
        { return a + b;
            return a + b; }
        } }
    } }
}
将其编译为一个lib文件:csc /t:library RemoteObject.cs
Server端:
 using System;
using System; using System.Runtime;
using System.Runtime; using System.Runtime.Remoting;
using System.Runtime.Remoting; using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Tcp; using RemoteSample;
using RemoteSample;
 namespace RemoteSampleServer
namespace RemoteSampleServer {
{ public class RemoteServer
    public class RemoteServer {
    { public static void Main(String[] args)
        public static void Main(String[] args) {
        { TcpServerChannel channel = new TcpServerChannel(6666);
             TcpServerChannel channel = new TcpServerChannel(6666); ChannelServices.RegisterChannel(channel);
             ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
             RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall);
                 "RemoteObject", WellKnownObjectMode.SingleCall); System.Console.WriteLine("Press Any Key");
             System.Console.WriteLine("Press Any Key"); System.Console.ReadLine();
             System.Console.ReadLine(); }
         } }
    } }
}
将其编译为一个exe文件:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteServer.cs
Client端:
 using System;
using System; using System.Runtime.Remoting;
using System.Runtime.Remoting; using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Tcp; using RemoteSample;
using RemoteSample;
 namespace RemoteSampleClient
namespace RemoteSampleClient {
{ public class RemoteClient
    public class RemoteClient {
    { public static void Main(string[] args)
        public static void Main(string[] args) {
        { ChannelServices.RegisterChannel(new TcpClientChannel());
            ChannelServices.RegisterChannel(new TcpClientChannel()); RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject),
            RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://localhost:6666/RemoteObject");
            "tcp://localhost:6666/RemoteObject"); Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString());
            Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString()); Console.ReadLine();
            Console.ReadLine(); }
        } }
    } }
}
同样的,将其编译为exe文件:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteClient.cs
好了,一次运行生成的RemoteServer.exe和RemoteClient.exe,你就会发现原来Remoting是这样简单。
标签:register library run main void reg read 技术 分享
原文地址:http://www.cnblogs.com/ChineseMoonGod/p/6121052.html