C# Remoting的一個簡單例子

來源:互聯網
上載者:User

.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用的對象:

 1namespace RemoteSample
 2{
 3    public class RemoteObject : System.MarshalByRefObject
 4    {
 5        public RemoteObject()
 6        {
 7            System.Console.WriteLine("New Referance Added!");
 8        }
 9
10        public int sum(int a, int b)
11        {
12            return a + b;
13        }
14    }
15}

將其編譯為一個lib檔案:csc /t:library RemoteObject.cs

Server端: 1using System;
 2using System.Runtime;
 3using System.Runtime.Remoting;
 4using System.Runtime.Remoting.Channels;
 5using System.Runtime.Remoting.Channels.Tcp;
 6using RemoteSample;
 7
 8namespace RemoteSampleServer
 9{
10    public class RemoteServer
11    {
12        public static void Main(String[] args)
13        {
14             TcpServerChannel channel = new TcpServerChannel(6666);
15             ChannelServices.RegisterChannel(channel);
16             RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
17                 "RemoteObject", WellKnownObjectMode.SingleCall);
18             System.Console.WriteLine("Press Any Key");
19             System.Console.ReadLine();
20         }
21    }
22}

將其編譯為一個exe檔案:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteServer.cs

Client端: 1using System;
 2using System.Runtime.Remoting;
 3using System.Runtime.Remoting.Channels;
 4using System.Runtime.Remoting.Channels.Tcp;
 5using RemoteSample;
 6
 7namespace RemoteSampleClient
 8{
 9    public class RemoteClient
10    {
11        public static void Main(string[] args)
12        {
13            ChannelServices.RegisterChannel(new TcpClientChannel());
14            RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject),
15            "tcp://localhost:6666/RemoteObject");
16            Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString());
17            Console.ReadLine();
18        }
19    }
20}

同樣的,將其編譯為exe檔案:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteClient.cs

好了,一次運行產生的RemoteServer.exe和RemoteClient.exe,你就會發現原來Remoting是這樣簡單。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.