一、远程对象

①RemoteHello.csproj 类库项目,程序集名称 RemoteHello ,默认命名空间 Wrox.ProCSharp.Remoting;

②派生自System.MarsshalByRefObject  ;

③根据不同的代理方式,需要有不同的构造函数(注意客户端的代码会有注释);

④Hello.cs源文件中的代码如下;

namespace Wrox.ProCSharp.Remoting
{
public class Hello:MarshalByRefObject
{
public Hello()
{
Console.WriteLine("创建Hello");
}
public Hello(string name)
{
Console.WriteLine($"创建 {name}");
}
public string Greeting(string name)
{
Console.WriteLine($"Hello {name}!!!!");
return $"Hello {name}!!!!";
}
}
}

二、服务端

  ①tcp、http和ipc三种不同的通讯协议服务端的实现;

  ②每种通讯协议都支持激活知名对象和激活客户端激活的对象的服务创建方式;

  ③每种的通讯协议包含的两种对象创建方式同客户端的代理创建方式都是一 一 对应的,例如:服务端使用的是tcp的服务端激活方式,客户端也必须是tcp的服务端激活方式代理创建方式;

  ④下方代码位于服务控制台项目HelloServer中的Program.cs源文件中;

  ⑤注意服务项目需要依赖远程对象库;

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using Wrox.ProCSharp.Remoting; namespace HelloServer
{
class Program
{
static void Main(string[] args)
{
{// tcp
{// 服务端激活(激活知名对象)
//var tcpChannel = new TcpServerChannel(8085);
//ChannelServices.RegisterChannel(tcpChannel, false);
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
} {// 激活客户端激活的对象
//var tcpChannel = new TcpServerChannel(8085);
//ChannelServices.RegisterChannel(tcpChannel, false);
//RemotingConfiguration.ApplicationName = "Hi";
//RemotingConfiguration.RegisterActivatedServiceType(typeof(Hello));
} } {// http
{// 服务端激活(激活知名对象)
//var httpChannel = new HttpServerChannel(8086);
//ChannelServices.RegisterChannel(httpChannel, false);
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
} {// 激活客户端激活的对象
//var httpChannel = new HttpServerChannel(8086);
//ChannelServices.RegisterChannel(httpChannel, false);
//RemotingConfiguration.ApplicationName = "Hi";
//RemotingConfiguration.RegisterActivatedServiceType(typeof(Hello));
} }
{// ipc 只能用于客户端和服务端在同一操作系统上
{// 服务端激活(激活知名对象)
//var ipcChannel = new IpcServerChannel("myIpcPort");
//ChannelServices.RegisterChannel(ipcChannel, false);
//RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);
} {// 激活客户端激活的对象
var ipcChannel = new IpcServerChannel("myIpcPort");
ChannelServices.RegisterChannel(ipcChannel, false);
RemotingConfiguration.ApplicationName = "Hi";
RemotingConfiguration.RegisterActivatedServiceType(typeof(Hello));
} } Console.WriteLine("Press Enter to exit!");
Console.ReadLine();
}
}
}

三、客户端

  ①tcp、http和ipc三种不同的通讯协议客户端的实现;

  ②每种通讯协议都支持激活知名对象(服务端激活)和激活客户端激活的对象的客户端代理创建方式;

  ③每种的通讯协议包含的两种对象创建方式同客户端的代理创建方式都是一 一 对应的,例如:服务端使用的是tcp的服务端激活方式,客户端也必须是tcp的服务端激活方式代理创建方式;

  ④下方代码位于服务控制台项目HelloClient中的Program.cs源文件中;

  ⑤注意服务项目需要依赖远程对象库;

  ⑥请注意阅读代码的注释,对规则和特性有关键描述;

  ⑦每种通讯协议的客户端激活代码都实现了三种远程代理创建方式,中间空了一行间隔开,请一定注意;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using Wrox.ProCSharp.Remoting; namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
try
{
#region tcp
{// tcp
{ // 服务端激活 对象调用后消失
// 只能默认构造函数
//TcpClientChannel tcpClient = new TcpClientChannel();
//ChannelServices.RegisterChannel(tcpClient, false);
//Hello obj = (Hello)Activator.GetObject(typeof(Hello), @"tcp://10.0.6.207:8085/Hi");
//if (obj == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//} //Console.WriteLine(obj.Greeting("tcp1"));
}
{ // 客户端激活 对象持久
//TcpClientChannel tcpClient = new TcpClientChannel();
//ChannelServices.RegisterChannel(tcpClient, false);
//object[] attrs = { new UrlAttribute(@"tcp://10.0.6.207:8085/Hi") };
////程序集 + 类型 + url属性 默认构造方法
//ObjectHandle handle = Activator.CreateInstance("RemoteHello", "Wrox.ProCSharp.Remoting.Hello", attrs);
//if (handle == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//}
//Hello obj = handle.Unwrap() as Hello;
//Console.WriteLine(obj.Greeting("tcp1")); //TcpClientChannel tcpClient = new TcpClientChannel();
//ChannelServices.RegisterChannel(tcpClient, false);
//object[] attrs = { new UrlAttribute(@"tcp://10.0.6.207:8085/Hi") };
//// 类型 + 参数 + url属性 参数位null,默认构造函数
//Hello obj = (Hello)Activator.CreateInstance(typeof(Hello),new object[] {"周静a" }, attrs);
//if (obj == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//}
//Console.WriteLine(obj.Greeting("tcp2")); //TcpClientChannel tcpClient = new TcpClientChannel();
//ChannelServices.RegisterChannel(tcpClient, false);
//// 注册
//RemotingConfiguration.RegisterActivatedClientType(typeof(Hello), "tcp://10.0.6.207:8085/Hi");
//// 创建
//Hello obj = new Hello("周静");
//if (obj == null)
//{
// Console.WriteLine("注册远程代理对象失败!");
// return;
//}
//Console.WriteLine(obj.Greeting("tcp3"));
}
}
#endregion
#region http
{// http
{ // 服务端激活 对象调用后消失
// 只能默认构造函数
//HttpClientChannel httpClient = new HttpClientChannel();
//ChannelServices.RegisterChannel(httpClient, false);
//Hello obj = (Hello)Activator.GetObject(typeof(Hello), @"http://10.0.6.207:8086/Hi");
//if (obj == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//} //Console.WriteLine(obj.Greeting("http1"));
}
{ // 客户端激活 对象持久
//HttpClientChannel httpClient = new HttpClientChannel();
//ChannelServices.RegisterChannel(httpClient, false);
//object[] attrs = { new UrlAttribute(@"http://10.0.6.207:8086/Hi") };
////程序集 + 类型 + url属性 默认构造方法
//ObjectHandle handle = Activator.CreateInstance("RemoteHello", "Wrox.ProCSharp.Remoting.Hello", attrs);
//if (handle == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//}
//Hello obj = handle.Unwrap() as Hello;
//Console.WriteLine(obj.Greeting("http1")); //HttpClientChannel httpClient = new HttpClientChannel();
//ChannelServices.RegisterChannel(httpClient, false);
//object[] attrs = { new UrlAttribute(@"http://10.0.6.207:8086/Hi") };
//// 类型 + 参数 + url属性 参数位null,默认构造函数
//Hello obj = (Hello)Activator.CreateInstance(typeof(Hello), new object[] { "周静a" }, attrs);
//if (obj == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//}
//Console.WriteLine(obj.Greeting("http2")); //HttpClientChannel httpClient = new HttpClientChannel();
//ChannelServices.RegisterChannel(httpClient, false);
//// 注册
//RemotingConfiguration.RegisterActivatedClientType(typeof(Hello), "http://10.0.6.207:8086/Hi");
//// 创建
//Hello obj = new Hello("周静");
//if (obj == null)
//{
// Console.WriteLine("注册远程代理对象失败!");
// return;
//}
//Console.WriteLine(obj.Greeting("tcp3"));
}
}
#endregion #region ipc
{// ipc 服务端和客户端只能在同一操作系统中,不持支跨域
{ // 服务端激活 对象调用后消失
// 只能默认构造函数
//IpcClientChannel ipcClient = new IpcClientChannel();
//ChannelServices.RegisterChannel(ipcClient, false);
//Hello obj = (Hello)Activator.GetObject(typeof(Hello), @"ipc://myIpcPort/Hi");
//if (obj == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//} //Console.WriteLine(obj.Greeting("ipc1"));
}
{ // 客户端激活 对象持久
//IpcClientChannel ipcClient = new IpcClientChannel();
//ChannelServices.RegisterChannel(ipcClient, false);
//object[] attrs = { new UrlAttribute(@"ipc://myIpcPort/Hi") };
////程序集 + 类型 + url属性 默认构造方法
//ObjectHandle handle = Activator.CreateInstance("RemoteHello", "Wrox.ProCSharp.Remoting.Hello", attrs);
//if (handle == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//}
//Hello obj = handle.Unwrap() as Hello;
//Console.WriteLine(obj.Greeting("ipc1")); //IpcClientChannel ipcClient = new IpcClientChannel();
//ChannelServices.RegisterChannel(ipcClient, false);
//object[] attrs = { new UrlAttribute(@"ipc://myIpcPort/Hi") };
//// 类型 + 参数 + url属性 参数位null,默认构造函数
//Hello obj = (Hello)Activator.CreateInstance(typeof(Hello), new object[] { "周静a" }, attrs);
//if (obj == null)
//{
// Console.WriteLine("获取远程代理失败!");
// return;
//}
//Console.WriteLine(obj.Greeting("ipc2")); //IpcClientChannel ipcClient = new IpcClientChannel();
//ChannelServices.RegisterChannel(ipcClient, false);
//// 注册
//RemotingConfiguration.RegisterActivatedClientType(typeof(Hello), "ipc://myIpcPort/Hi");
//// 创建
//Hello obj = new Hello("周静");
//if (obj == null)
//{
// Console.WriteLine("注册远程代理对象失败!");
// return;
//}
//Console.WriteLine(obj.Greeting("ipc3"));
}
}
#endregion }
catch (Exception ex)
{ Console.WriteLine(ex.Message);
}
Console.WriteLine("Press AnyKey to Exit");
Console.ReadKey(); }
}
}

四、其他注意事项

  ① 本次实现只是remoting的简单实现,对初学者学习应该能很省很多事,其他AOP等方面的深度应用请阅读相关书籍,C#高级编程系列的书籍;

  ② 工程项目是在win10 64操作系统上vs2019中实现验证的,如有错误和疑问,欢迎留言,谢谢!

服务端激活

.net remoting(一)的更多相关文章

  1. spring remoting源码分析--Hessian分析

    1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...

  2. Visual Studio 2013 Ultimate因为CodeLens功能导致Microsoft.Alm.Shared.Remoting.RemoteContainer.dll高CPU占用率的折中解决方案

    1.为什么Microsoft.Alm.Shared.Remoting.RemoteContainer.dll的CPU占用率以及内存使用率会那么高? 在Visual Studio 2013 Ultima ...

  3. VS2015 出现 .NETSystem.Runtime.Remoting.RemotingException: TCP 错误

    错误内容: 界面显示内容为: .NET�������������System.Runtime.Remoting.RemotingException: TCP 淇¢亾鍗忚鍐茬獊: 搴斾负鎶ュご銆� 鍦 ...

  4. .Net中Remoting通信机制简单实例

    .Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例 本程序采用语言:c# 编译工具:vs2013工程文件 编译环境:.net 4.0 程序模块: Test测试 ...

  5. .Net中Remoting通信机制

    Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式. 从微软的产品角度 ...

  6. .NET Remoting 应用实例

    前言 项目中运用到.NET Remoting ,前段时间也看了下.NET Remoting的相关资料,感觉自己应该动手写个实例来梳理下对.NET Remoting认识和理解,不足的地方请大家指正. 简 ...

  7. Holographic Remoting

    看到微软官方的 Holographic Remoting Player https://developer.microsoft.com/en-us/windows/holographic/hologr ...

  8. IIS部署Remoting总结

    1.在IIS里新建一个网站,命名为test,路径指向 e:\test: 2.在 e:\test下创建目录bin: 3.把Remoting远程对象的Project设置为类库,编译为DLL文件,然后复制到 ...

  9. .NET Remoting 体系结构 之 在 ASP.NET 中驻留远程服务器

    迄今为止,所有服务器示例都是运行在自驻留(self-hosted)的.NET 服务器上.自驻留的服务器必 须手动启动..NET Remoting 服务器也可以在许多其他的应用程序类型中启动.在 Win ...

  10. 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 ...

随机推荐

  1. Spring官网阅读(十)Spring中Bean的生命周期(下)

    文章目录 生命周期概念补充 实例化 createBean流程分析 doCreateBean流程分析 第一步:factoryBeanInstanceCache什么时候不为空? 第二步:创建对象(crea ...

  2. struts2入门教学

    我的博客地址:https://blog.csdn.net/qq_41907991 首先介绍一下struts2使用的基本步骤: 1.导入相关的 jar 文件 2.需要在 web.xml 文件中配置一个 ...

  3. Openwrt:添加"自定义软件包.ipk"

    我们已经尝试做的一件事情,是让移植软件到OpenWrt的操作变得非常容易.如果打开OpenWrt里的一个软件包的目录(OpenWrt/Package/* 或 OpenWrt/feeds/package ...

  4. .NET Attribute在数据校验上的应用

    Attribute(特性)的概念不在此赘述了,相信有点.NET基础的开发人员都明白,用过Attribute的人也不在少数,毕竟很多框架都提供自定义的属性,类似于Newtonsoft.JSON中Json ...

  5. Autojs - 用 JavaScript 实现自己的安卓手机自动化工具脚本

    我是风筝,公众号「古时的风筝」,一个不只有技术的技术公众号,一个在程序圈混迹多年,主业 Java,另外 Python.React 也玩儿的 6 的斜杠开发者. Spring Cloud 系列文章已经完 ...

  6. python解析谷歌在线表格链接,转化为数组形式,图片转化为链接

    在线表格已成为趋势,传统的表格导入导出已经不能满足现在企业的发展. 必须支持在线表格的导入,只需要输入在线表格的链接,就能将数据读取出来. 而且相比相传表格,能够将图片信息一块读取.在线表格中的图片也 ...

  7. java ->Iterator (迭代)

    Iterator迭代器概述 java中提供了很多个集合,它们在存储元素时,采用的存储方式不同.我们要取出这些集合中的元素,可通过一种通用的获取方式来完成. Collection集合元素的通用获取方式: ...

  8. 这么简单的ES索引生命周期管理,不了解一下吗~

    对于日志或指标(metric)类时序性强的ES索引,因为数据量大,并且写入和查询大多都是近期时间内的数据.我们可以采用hot-warm-cold架构将索引数据切分成hot/warm/cold的索引.h ...

  9. MyBatis缓存机制(一级缓存,二级缓存)

    一,MyBatis一级缓存(本地缓存) My Batis 一级缓存存在于 SqlSession 的生命周期中,是SqlSession级别的缓存.在操作数据库时需要构造SqlSession对象,在对象中 ...

  10. Django视图函数之三种响应模式

    视图函数响应处理: from django.shortcuts import render,HttpResponse,redirect (1)render 处理模板文件,可以渲染模板,第一个参数必须为 ...