服务端

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代码实现】的更多相关文章

  1. 分布式应用处理方式 - Remoting

    分布式应用程序 所谓分布式计算是一门计算机科学,它研究如何把一个需要非常巨大的计算能力才能解决的问题分成许多小的部分,然后把这些部分分配给许多计算机进行处理,最后把这些计算结果综合起来得到最终的结果. ...

  2. .net core 2.0学习笔记(六):Remoting核心类库RealProxy迁移

    在学习.net core的过程中,我们已经明确被告知,Remoting将不会被支持.官方的解释是,.net framework 类型包含了太多的Runtime的内容,是一个非常重量级的服务实现,已被确 ...

  3. .net remoting和wcf自托管——一个bug引发的警示

    一.解决问题,需要深入,并从细节入手,多从代码找原因,不能认为代码是死的,不会出错: 之前代码都运行良好,突然某一天,在我电脑上出问题了.出了问题,那就应该找出原因.其实这个问题,本身并不难,好歹给你 ...

  4. .net基本面试题

    OOP: Object Oriented Programming: 面向对象编程技术的关键性观念是它将数据及对数据的操作行为放在一起,作为一个相互依存.不可分割的整体——对象.对于相同类型的对象进行分 ...

  5. 智能提示含查询多列(html+JS+handler+ HttpRemoting)二、Remoting代码

    /// <summary> /// 智能查询类型 /// </summary> public enum QueryType : byte { /// <summary&g ...

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

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

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

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

  8. .Net中Remoting通信机制

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

  9. 3小时搞定一个简单的MIS系统案例Northwind,有视频、有源代码下载、有真相

    一.瞎扯框架.架构 楼主自从1998年从C语言.MASM.Foxbase开始学计算机开始接触这个行当16年以来,2001年干第一份与程序.软件.然后是各种屌的东西开始,差不多干了13年了,这13年来, ...

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

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

随机推荐

  1. Oracle/Mysql/SqlServer函数区别

    mysql日期和时间格式转换 Linux scp 使用详解 Oracle/Mysql/SqlServer函数区别 2011-07-01 12:34:36|  分类: Mysql技术 |  标签:mys ...

  2. oracle用户权限的问题

    一.创建用户 create user username identified by password --username 创建的用户的名称 --password 创建的用户的密码 二.赋权限 gra ...

  3. UIDeviceOrientation UIInterfaceOrientation 区别

    UIDeviceOrientation      是机器硬件的当前旋转方向   这个你只能取值 不能设置 UIInterfaceOrientation   是你程序界面的当前旋转方向   这个可以设置 ...

  4. VS代码清理批处理

    批处理清理VS工程 del /f /q /s *.ncb del /f /q /s *.sdf del /f /q /s /A H *.suo del /f /q /s *.ipch del /f / ...

  5. zongjie

    $msg = $_GET['msg'];$startDate = $_POST['startDate'];$endDate = $_POST['endDate'];$quickdate = $_POS ...

  6. ERP行业推荐参考书籍

    1 书名:<ERP 理论.方法与实践> 作者: 周玉清等编著 出版社:电子工业出版社 简介:本书全面介绍了ERP的基本原理和处理逻辑,以大量篇幅讨论了ERP的计划功能,特别是主生产计划功能 ...

  7. BeanUtils包的学习

    BeanUtils支持八种基本数据类型(int double short char byte float boolean long)的反射,对于日期需要提前注册DateLocalConvert获取转换 ...

  8. Mysql服务启动问题

    mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) u ...

  9. GTW likes math(BC 1001)

    GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1 ...

  10. [TYVJ] P1010 笨小猴

    笨小猴 背景 Background NOIP2008复赛提高组第一题   描述 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种 ...