C# Remoting的一个简单例子
来源:百度文库 编辑:神马文学网 时间:2024/04/27 20:09:12
2007-10-26 09:07 .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是这样简单。
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是这样简单。
C# Remoting的一个简单例子
一个spring的简单例子
一个简单的XML Schema的例子
类的简单例子
对Jena的简单理解和一个例子_1
对Jena的简单理解和一个例子_2
对Jena的简单理解和一个例子_1
对Jena的简单理解和一个例子_2
对Jena的简单理解和一个例子
用C#创建一个简单的文本编辑器
一个BPEL的例子
一个BPEL的例子
struts2.0简单的例子
iBATIS实现的一个例子
一个真实的大学生例子
堕落:一个负面的例子
以一个最简单的例子把OO的JavaScript说明白
以一个最简单的例子把OO的JavaScript说明白
计算机科学论坛--一个yale-cas实现单点登录的简单例子实现
Digester学习笔记(包括一个简单的例子)_scofield's technology...
100分全给,求C#中反射和remoting之类的学习之经验,和通俗些的解释 - .NET技术 / C#
简单的列表框ListBox的例子
通过一个简单例子复习一下rman - Oracle - Sky_欧彬
载荷为位置函数的一个例子