【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 ...
随机推荐
- XML格式导出Excel
下面介绍一种导出Excel的方法: 此方法不需要在服务器上安装Excel,采用生成xml以excel方式输出到客户端,可能需要客户机安装excel,所以也不会有乱七八糟的权限设定,和莫名其妙的版本问题 ...
- oracle 初探内存结构
数据库的存储机构 分为 逻辑存储结构 和 物理存储结构 逻辑存储结构: 数据库.表空间.段.区.块 物理存储结构: 数据库.控制文件.数据文件.初始化参数文件.OS块等. 一个区只能在 ...
- iOS 面试题 3
0.请写出代码,用blocks来取代上例中的protocol,并比较两种方法的优势.实际应用部分?请写出代码,用blocks取代协议或回调方法 声明: #import <Foundation/F ...
- unity 打包 windows 运行 紫色 粉红色
unity下建立了个小demo,在editer里面运行正常.如下 但是一旦打包发布到android或者windows下就出现了类似这种情况 这种一般是由于材质贴图的缺失,一般来说选定的默认贴图的话会打 ...
- 空合并操作符??(C#)
??二元操作符在对first??second求值时,大致会经历以下步骤: 1)对first进行求值: 2)如果结果非空,则该结果就是整个表达式的结果: 3)否则求second的值,其结果作为整个表达式 ...
- 如何利用自己的电脑做服务器发布tomcat的WEB项目供外网访问
1.首先你要确定你有一个外网ip地址.如果你分配到的是一个局域网IP地址需要经过一系列的转换为外网ip地址,然后继续下面操作. 2.拿到外网IP地址,进行tomcat的server.xml文件的配置. ...
- im2uint8函数分析
环境:Win7 64位 + Matlab R2010a 本次分析的函数为im2uint8,这个函数在图像处理中要用到,主要把图像数据类转换到uint8 uint8函数有效的输入的图像数据类为:logi ...
- MySql移植到嵌入式Linux平台
最近在做考勤机系统,硬件采用的cortex-A8,哈哈,其实是有点浪费的,2410就可以的.所以就要考虑到考勤数据的存储问题,本来是打算用sqlite数据库存储的,可是后来发现,这个数据库只是一个本地 ...
- PHP 获取客户端IP
function get_ip() { static $realIP; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_ ...
- 【2】python核心编程 第四章-python对象
1.python对象 所有的Python 对像都拥有三个特性:身份,类型和值. 身份: 每一个对象都有一个唯一的身份标识自己,任何对象的身份可以使用内建函数id()来得到. 这个值可以被认为是该对象的 ...