.NET Remoting
.NET Remoting
.NET Remoting是微软早期的分布式通信技术,虽然微软后来通过WCF通用基础通信框架整合掉了,但是通过回顾学习Remoting,反过来学习理解WCF也是很有帮助的。同时通过这篇博客的学习,可以看到Remoting技术的魅力。
先看一段Wiki百科对于WCF技术的概述。其中就提到了WCF、Web Service、Remoting、Socket等技术。在我提到这些名词的同时,你应该对这些技术有所了解并清楚这些技术所处于的层次。
Windows Communication Foundation (WCF)是由微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,由.NET Framework 3.0开始引入,与Windows Presentation Foundation及 Windows Workflow Foundation并行为新一代Windows操作系统以及WinFX的三个重大应用程序开发类库。
在.NET Framework 2.0以及前版本中,微软发展了Web Service(SOAP with HTTP communication),.NET Remoting(TCP/HTTP/Pipeline communication)以及基础的Winsock等通信支持,由于各个通信方法的设计方法不同,而且彼此之间也有相互的重叠性(例如.NET Remoting可以开发SOAP, HTTP通信),对于开发人员来说,不同的选择会有不同的程序设计模型,而且必须要重新学习,让开发人员在用户有许多不便。同时,服务导向架构(Service-Oriented Architecture)也开始盛行于软件工业中,因此微软重新查看了这些通信方法,并设计了一个统一的程序开发模型,对于数据通信提供了最基本最有弹性的支持,这就是Windows Communication Foundation。
其实在早期,还有一些其它的通信技术,比如:MSMQ(消息队列)、COM、COM+、DCOM等。COM是微软的重大发明,将复杂的通信细节封装到一个一个的COM组件中,让.NET程序员可以不用关心复杂的通信细节,只用操作通信模型就可以实现交互操作。
简单介绍就到这,回过头来看看Remoting的通信架构图:
从架构图可以看到,Remoting可以以对象代理的方式直接操作服务器端的对象的方法。
下面建一个最基本的项目来演示一下Remoting的使用。
先看一下项目的架构:
RemotingServer是一个Remoting服务端控制台项目;RemotingClient是一个Remoting客户端控制台项目;RemotingService是一个类库项目。
RemotingServer和RemotingClient都需要引用System.Runtime.Remoting这个组件。并且引用RemotingService这个类库项目。
RemotingServer中的代码:
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8888);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(PersonIDConverter), "Hi", WellKnownObjectMode.SingleCall);
Console.WriteLine("Remoting服务已经启动");
Console.ReadLine();
}
RemotingClient中的代码:
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpClientChannel(), false);
PersonIDConverter service = (PersonIDConverter)Activator.GetObject(typeof(PersonIDConverter), "tcp://localhost:8888/Hi");
if (!object.Equals(service, null))
{
Console.WriteLine(service.PersonID15To18("130503670401001"));//测试身份证号
}
Console.ReadLine();
}
PersonIDConverter中的代码:
/// <summary>
/// 远程服务类,继承自MarshalByRefObject
/// </summary>
public class PersonIDConverter : MarshalByRefObject
{
/// <summary>
/// 身份证ID 15转18位
/// </summary>
/// <param name="id">15位身份证号</param>
/// <returns>18位身份证号</returns>
public string PersonID15To18(string id)
{
int iS = 0;
int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };//加权因子常数
string LastCode = "10X98765432";//校验码常数
string newId;//新身份证号
newId = id.Substring(0, 6);
newId += "19";//填在第6位及第7位上填上‘1’,‘9’两个数字
newId += id.Substring(6, 9);
//进行加权求和
for (int i = 0; i < 17; i++)
{
iS += int.Parse(newId.Substring(i, 1)) * iW[i];
}
int iY = iS % 11; //取模运算,得到模值
newId += LastCode.Substring(iY, 1);//从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
return newId;
}
}
这里面提供了一个身份证15位转18位的服务。
运行时先启动RemotingServer项目,然后运行RemotingClient项目。
服务端运行后:
客户端运行后:
可以看到15位身份证号成功转为了18位,Remoting调用成功。
.NET Remoting的更多相关文章
- spring remoting源码分析--Hessian分析
1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...
- Visual Studio 2013 Ultimate因为CodeLens功能导致Microsoft.Alm.Shared.Remoting.RemoteContainer.dll高CPU占用率的折中解决方案
1.为什么Microsoft.Alm.Shared.Remoting.RemoteContainer.dll的CPU占用率以及内存使用率会那么高? 在Visual Studio 2013 Ultima ...
- VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误
错误内容: 界面显示内容为: .NET�������������System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆� 鍦 ...
- .Net中Remoting通信机制简单实例
.Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例 本程序采用语言:c# 编译工具:vs2013工程文件 编译环境:.net 4.0 程序模块: Test测试 ...
- .Net中Remoting通信机制
Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式. 从微软的产品角度 ...
- .NET Remoting 应用实例
前言 项目中运用到.NET Remoting ,前段时间也看了下.NET Remoting的相关资料,感觉自己应该动手写个实例来梳理下对.NET Remoting认识和理解,不足的地方请大家指正. 简 ...
- Holographic Remoting
看到微软官方的 Holographic Remoting Player https://developer.microsoft.com/en-us/windows/holographic/hologr ...
- IIS部署Remoting总结
1.在IIS里新建一个网站,命名为test,路径指向 e:\test: 2.在 e:\test下创建目录bin: 3.把Remoting远程对象的Project设置为类库,编译为DLL文件,然后复制到 ...
- .NET Remoting 体系结构 之 在 ASP.NET 中驻留远程服务器
迄今为止,所有服务器示例都是运行在自驻留(self-hosted)的.NET 服务器上.自驻留的服务器必 须手动启动..NET Remoting 服务器也可以在许多其他的应用程序类型中启动.在 Win ...
- Remoting and web services using Spring[摘自官网]
spring document url: http://docs.spring.io/spring/docs/ Using Hessian First we’ll have to create a n ...
随机推荐
- What is XMLHTTP? How to use security zones in Internet Explorer
Types of Security Zones Internet Zone This zone contains Web sites that are not on your computer or ...
- 1. what is Lua?
glue language Lua is a proven, robust language, small.
- JsRender系列demo(7)compline
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery.j ...
- Xamarin for Visual Studio 3.11.658 Alpha 版 破解补丁
注意:此版本为 Alpha 版,版本迭代较频繁,仅供尝鲜 前提概要 全新安装请参考 安装 Xamarin for Visual Studio. 最新稳定版请参考 Xamarin for Visual ...
- hdu 4588 Count The Carries
思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...
- Android Drawable 关于selector中state_pressed="true"的位置顺序
界面中有一个按钮使用这样的样式: <?xml version="1.0" encoding="utf-8"?> <selector xmlns ...
- Project Euler 98:Anagramic squares 重排平方数
Anagramic squares By replacing each of the letters in the word CARE with 1, 2, 9, and 6 respectively ...
- 2014--9=17 软工二班 MyEclipse blue==4
package cn.rwkj.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputS ...
- java:构造函数
class Dog { Dog(){ } } 构造函数没有返回值定义,构造函数名必须和类名相同,如果类里面没有构造函数,编译器会帮你加一个构造函数. 使用this调用构造函数 class Dog { ...
- JavaScript DOM编程基础精华02(window对象的属性,事件中的this,动态创建DOM,innerText和innerHTML)
window对象的属性1 window.location对象: window.location.href=‘’;//重新导航到新页面,可以取值,也可以赋值. window.location.reloa ...