C# Remoting的一个简单例子
Posted ChineseMoonGod
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Remoting的一个简单例子相关的知识,希望对你有一定的参考价值。
.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用的对象:
2
![](https://image.cha138.com/20210610/450909853fe24eb6b45a4d3445cfc9d5.jpg)
3
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
4
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
5
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
6
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
7
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
8
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
9
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
10
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
11
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
12
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
13
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
14
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
15
![](https://image.cha138.com/20210610/452ee9d384044f059e6c6223047389dc.jpg)
将其编译为一个lib文件:csc /t:library RemoteObject.cs
Server端:
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
2
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
3
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
4
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
5
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
6
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
7
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
8
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
9
![](https://image.cha138.com/20210610/450909853fe24eb6b45a4d3445cfc9d5.jpg)
10
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
11
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
12
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
13
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
14
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
15
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
16
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
17
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
18
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
19
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
20
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
21
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
22
![](https://image.cha138.com/20210610/452ee9d384044f059e6c6223047389dc.jpg)
将其编译为一个exe文件:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteServer.cs
Client端:
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
2
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
3
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
4
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
5
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
6
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
7
![](https://image.cha138.com/20210610/360a2ac261ff48c6bbc071d7664c629a.jpg)
8
![](https://image.cha138.com/20210610/450909853fe24eb6b45a4d3445cfc9d5.jpg)
9
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
10
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
11
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
12
![](https://image.cha138.com/20210610/c4a049c9be144f89847678b9a695ad93.jpg)
13
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
14
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
15
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
16
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
17
![](https://image.cha138.com/20210610/2d79d22c5ac548ed9245a2c8231173c2.jpg)
18
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
19
![](https://image.cha138.com/20210610/d60c3f3585104f6c90e47605c738ef5f.jpg)
20
![](https://image.cha138.com/20210610/452ee9d384044f059e6c6223047389dc.jpg)
同样的,将其编译为exe文件:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteClient.cs
好了,一次运行生成的RemoteServer.exe和RemoteClient.exe,你就会发现原来Remoting是这样简单。
以上是关于C# Remoting的一个简单例子的主要内容,如果未能解决你的问题,请参考以下文章
求一个C#最简单的TCP传输信息例子(实现聊天和传送文件,再简单点实现聊天也行)