C# Remoting的一个简单例子

来源:互联网 发布:node 删除模块 编辑:程序博客网 时间:2024/05/26 09:54

.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是这样简单。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bmoon/archive/2008/11/21/3347698.aspx

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 内螺纹止规过了怎么办 牌照螺丝装错了怎么办 帽式扳手大了怎么办 内六角扳手小了怎么办 内六角扳手不够大怎么办? 苹果6螺丝拧花了怎么办 苹果电脑螺丝拧花了怎么办 苹果螺丝滑牙了怎么办 苹果7螺丝滑丝了怎么办 外六角螺丝滑牙怎么办 六角螺帽滑丝了怎么办 内六角螺丝滑头了怎么办 内六角螺丝滑失了怎么办 内六角螺钉滑了怎么办 三视图看不出来怎么办 小猫断奶以后母猫涨奶怎么办 手机螺丝滑丝了怎么办 螺丝孔道滑丝了怎么办 螺丝生锈了拧不下来怎么办 钣金加工六角网孔变形怎么办 外六角螺帽滑丝怎么办 内六角螺丝螺帽滑丝怎么办 一字螺丝钉脱扣拧不下来怎么办 一字螺丝拧花了怎么办 小螺丝卸不下来怎么办 机油螺丝滑丝了怎么办 刚滑双板膝盖滑的疼怎么办 lv包真皮弄脏了怎么办 lv包压变形了怎么办 lv的包包被压了怎么办 固态硬盘太小了怎么办 联想笔记本网络连接不可用怎么办 联想g50玩dnf卡怎么办 手机有wifi电脑没有网怎么办 电脑网卡被禁用了怎么办 win8系统装win7蓝屏怎么办 联想笔记本装win7蓝屏怎么办 联想g40-70开机黑屏怎么办 新主机开不了机怎么办 联想720s笔记本闪屏怎么办 华硕k40ie显卡坏了怎么办