【Remoting-5代码实现】
服务端
class RemotingServiceHelper
{
private static string m_protocolType;
private static string urlString = "{0}://{1}:{2}/{3}";
/// <summary>
/// 注册Ichannel通道
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool RegisterChannel(ChannelType type)
{
try
{
IChannel channel;
IDictionary tables=new Hashtable();
tables["port"] = 9011;
switch (type)
{
case ChannelType.HttpChannel:
tables["name"] = "HttpChannel";
channel=new HttpChannel(tables,new SoapClientFormatterSinkProvider(),
new SoapServerFormatterSinkProvider());
m_protocolType = "http";
break;
case ChannelType.TcpChannel:
tables["name"] = "TcpChannel";
channel=new TcpChannel(tables,new BinaryClientFormatterSinkProvider(),
new BinaryServerFormatterSinkProvider() );
m_protocolType = "tcp";
break;
case ChannelType.IpcChannl:
tables["name"] = "IpcChannel";
channel=new IpcChannel(tables,new BinaryClientFormatterSinkProvider(),
new BinaryServerFormatterSinkProvider());
m_protocolType = "ipc";
break;
default:
channel = null;
return false; }
ChannelServices.RegisterChannel(channel,false);
Console.WriteLine("服务注册成功!端口号为:{0},通道类型为{1}", tables["port"],type);
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
// channel = null;
return false;
}
} public static bool ActivatedServiceObject(ActivatedType type)
{
RemotingConfiguration.ApplicationName = "RemotingService";
string url=String.Empty;
string objUri = type.ToString();
switch (type)
{
case ActivatedType.Client:
Console.WriteLine("激活方式:客户端激活");
//在服务端上,指定使用客户端激活方式激活远程服务对象
RemotingConfiguration.RegisterActivatedServiceType(typeof(ServiceObj));
url = string.Format(urlString, m_protocolType, IPAddress.Loopback, 9011,
RemotingConfiguration.ApplicationName);
break;
case ActivatedType.ServiceSingleCall:
Console.WriteLine("激活方式:服务端ServiceSingleCall激活");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceObj),"ServiceSingleCall",WellKnownObjectMode.SingleCall);
url = string.Format(urlString+"/{4}", m_protocolType, IPAddress.Loopback, 9011,
RemotingConfiguration.ApplicationName, objUri);
break;
case ActivatedType.ServiceSingleton:
Console.WriteLine("激活方式:服务端ServiceSingleton激活");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceObj), "ServiceSingleton", WellKnownObjectMode.Singleton);
url = string.Format(urlString + "/{4}", m_protocolType, IPAddress.Loopback, 9011,
RemotingConfiguration.ApplicationName, objUri);
break;
default :
return false;
}
Console.WriteLine("远程对象的Url地址为:{0}",url);
return true;
}
} enum ChannelType
{
TcpChannel,
HttpChannel,
IpcChannl,
} enum ActivatedType
{
Client,
ServiceSingleton,
ServiceSingleCall,
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("注册Ichannel通道");
if (RemotingServiceHelper.RegisterChannel(ChannelType.HttpChannel))
{
// Console.WriteLine("通道注册成功,通道类型为{0}",ChannelType.TcpChannel);
if (RemotingServiceHelper.ActivatedServiceObject(ActivatedType.ServiceSingleton))
{
Console.WriteLine("远程服务对象激活成功!");
Console.ReadKey();
}
}
}
}
客户端:
【注意对服务程序集的引用或是分离】
class ClientProxy
{
private string urlString = "{0}://{1}:{2}/{3}";
private ServiceObj _serviceObj; private string m_protocol;
private string m_appName;
private ActivatedType m_activatedType;
public ClientProxy(string protocol, string appName, ActivatedType activatedType)
{
m_protocol = protocol;
m_appName = appName;
m_activatedType = activatedType;
} public ServiceObj ServiceObj
{
get
{
if (ActivatorServiceObj())
{
return _serviceObj;
}
else
{
Console.WriteLine("激活远程对象失败");
return null;
} }
} private bool ActivatorServiceObj()
{
string url = GetUrlString();
// string url = "";
try
{
// _serviceObj = (ServiceObj)Activator.GetObject(typeof(ServiceObj), url);
switch (m_activatedType)
{
case ActivatedType.Client:
//先激活远程对象,再调用构造函数创建对象
RemotingConfiguration.RegisterActivatedClientType(typeof(ServiceObj), url);
_serviceObj = new ServiceObj();
break;
case ActivatedType.ServiceSingleCall:
_serviceObj = (ServiceObj)RemotingServices.Connect(typeof(ServiceObj), url);
break;
case ActivatedType.ServiceSingleton:
_serviceObj = (ServiceObj)Activator.GetObject(typeof(ServiceObj), url);
break;
}
return true;
}
catch (Exception e)
{
Console.WriteLine(e);
return false;
}
} private string GetUrlString()
{
string url = string.Empty;
switch (m_activatedType)
{
case ActivatedType.Client:
url = string.Format(urlString, m_protocol, IPAddress.Loopback, 9011,
m_appName);
break;
case ActivatedType.ServiceSingleCall:
url = string.Format(urlString + "/{4}", m_protocol, IPAddress.Loopback, 9011,
m_appName, "ServiceSingleCall");
break;
case ActivatedType.ServiceSingleton:
url = string.Format(urlString + "/{4}", m_protocol, IPAddress.Loopback, 9011,
m_appName, "ServiceSingleton");
break;
}
return url;
} } enum ChannelType
{
TcpChannel,
HttpChannel,
IpcChannl,
} enum ActivatedType
{
Client,
ServiceSingleton,
ServiceSingleCall,
}
class Program
{
static void Main(string[] args)
{
ClientProxy proxy = new ClientProxy("http", "RemotingService", ActivatedType.ServiceSingleton);
ServiceObj obj = proxy.ServiceObj;
if (obj != null)
{
obj.PrintAppDomain();
obj.PrintAppDomain();
obj.ShowCount("张三");
Console.WriteLine(obj.GetSum(10, 12));
Console.WriteLine(obj.GetStrInfo("张三"));
Console.WriteLine("count:{0}", obj.Count);
}
Console.WriteLine();
ClientProxy proxy2 = new ClientProxy("http", "RemotingService", ActivatedType.ServiceSingleton);
ServiceObj obj2 = proxy2.ServiceObj;
if (obj2 != null)
{
obj2.PrintAppDomain();
obj2.PrintAppDomain();
obj2.ShowCount("张三");
Console.WriteLine(obj.GetSum(332, 12));
Console.WriteLine(obj.GetStrInfo("李四"));
Console.WriteLine("count:{0}", obj.Count);
} Console.ReadKey();
}
}
远程服务对象
public class ServiceObj:MarshalByRefObject,IServiceObj
{
private int count = 0;
public ServiceObj()
{
Console.WriteLine("服务对象的构造函数");
Console.WriteLine();
} public int Count
{
get { return count; }
} public void ShowCount(string name)
{
count++;
Console.WriteLine("{0},count的值为{1}",name,Count);
} public void PrintAppDomain()
{
// AppDomain currentDomain = AppDomain.CurrentDomain;
AppDomain currentDomain = Thread.GetDomain();//获取当前线程所在的应用程序域
Console.WriteLine(currentDomain.FriendlyName);
} public int GetSum(int a, int b)
{
Console.WriteLine("A+B={0}",a+b);
return a + b;
} public string GetStrInfo(string arg)
{
Console.WriteLine(arg);
return string.Format("返回一个字符串加参数{0}", arg);
} }
【Remoting-5代码实现】的更多相关文章
- 分布式应用处理方式 - Remoting
分布式应用程序 所谓分布式计算是一门计算机科学,它研究如何把一个需要非常巨大的计算能力才能解决的问题分成许多小的部分,然后把这些部分分配给许多计算机进行处理,最后把这些计算结果综合起来得到最终的结果. ...
- .net core 2.0学习笔记(六):Remoting核心类库RealProxy迁移
在学习.net core的过程中,我们已经明确被告知,Remoting将不会被支持.官方的解释是,.net framework 类型包含了太多的Runtime的内容,是一个非常重量级的服务实现,已被确 ...
- .net remoting和wcf自托管——一个bug引发的警示
一.解决问题,需要深入,并从细节入手,多从代码找原因,不能认为代码是死的,不会出错: 之前代码都运行良好,突然某一天,在我电脑上出问题了.出了问题,那就应该找出原因.其实这个问题,本身并不难,好歹给你 ...
- .net基本面试题
OOP: Object Oriented Programming: 面向对象编程技术的关键性观念是它将数据及对数据的操作行为放在一起,作为一个相互依存.不可分割的整体——对象.对于相同类型的对象进行分 ...
- 智能提示含查询多列(html+JS+handler+ HttpRemoting)二、Remoting代码
/// <summary> /// 智能查询类型 /// </summary> public enum QueryType : byte { /// <summary&g ...
- 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 ...
- .Net中Remoting通信机制
Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式. 从微软的产品角度 ...
- 3小时搞定一个简单的MIS系统案例Northwind,有视频、有源代码下载、有真相
一.瞎扯框架.架构 楼主自从1998年从C语言.MASM.Foxbase开始学计算机开始接触这个行当16年以来,2001年干第一份与程序.软件.然后是各种屌的东西开始,差不多干了13年了,这13年来, ...
- .NET Remoting 体系结构 之 在 ASP.NET 中驻留远程服务器
迄今为止,所有服务器示例都是运行在自驻留(self-hosted)的.NET 服务器上.自驻留的服务器必 须手动启动..NET Remoting 服务器也可以在许多其他的应用程序类型中启动.在 Win ...
随机推荐
- SQL Server -查看数据库扩展属性
1.fn_listextendedproperty 函数可以基于对象类型显示单个数据库对象或数据库中所有对象的扩展属性.例如,可以返回表或表中所有列的扩展属性. A.下面的示例显示了数据库本身设置的所 ...
- hdu1175连连看
Problem Description “连连看”相信很多人都玩过.没玩过也没关系,下面我给大家介绍一下游戏规则:在一个棋盘中,放了很多的棋子.如果某两个相同的棋子,可以通过一条线连起来(这条线不能经 ...
- node.js如何使用回调
Node.js到处使用回调,尤其在有I/O(输入/输出)操作的地方. 下面是在一个Node.js中使用filesystem模块中从磁盘上读入文件内容示例一: var fs = require('fs' ...
- Android中的消息机制:Handler消息传递机制
参考<疯狂android讲义>第2版3.5 P214 一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为 ...
- ajax 传值 中文乱码问题
使用encodeURI编码内容 var Path = encodeURI("中文.xls"); url: "ashx/Data.ashx?Path =" + P ...
- linux下tar用法
以下是linux下tar的用法,转一下,以便方便自己看(这里没把rar,zip类的转过来,一般rar,zip在linux下基本没人用,基本上是zip,unzip,rar,unrar,这些命令,并且ra ...
- 安装好ubuntu之后要干的几件事
安装完ubuntu之后啊,系统除了自带了firefox,libre office等能用,要应付日常需求还差了些.然后我根据最近我的需求写了个清单.完成这个清单就让ubuntu成了一个得心应手的好工具了 ...
- 利用cookies获取登录后的网页
众所周知,HTTP连接是无状态的,那么问题来了,怎么记录用户的登录信息呢?通常的做法是用户第一次发送HTTP请求时,在HTTP Server端生成一个SessionID,SessionID会对应每个会 ...
- php文件处理
1. 将数据写入文件步骤 1. 打开这个文件,如果不存在,则新建文件 2. 将数据写入文件 3. 关闭文件 2. 从文件中读取数据步骤 1. 打开一个文件,如果不能打开,如文件不存在,应安全退出 2. ...
- linux系统文件夹的作用 good
/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 如:环境变量 /etc/rc.d 启动的配置文件和脚本 /home用户主目录的基点,比如用户user的主目录就是/ho ...