服务端

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. AJAX 控件集之TextBoxWatermark(水印文本框)控件

    功能:       可以让TextBox控件初始化的时候拥有水印文字.属性:    TargetControlID :要使用具有水印效果的TextBox控件ID.    WatermarkCssCla ...

  2. [转]CENTOS6 VNCSERVER安装

    标签:vncservercentos6.0 ssh隧道 vncviewer centos 休闲 职场 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律 ...

  3. ios 获取屏幕的属性

    屏幕尺寸     CGRect screen = [UIscreen mainScreen].bounds 状态栏尺寸  CGRect rect = [[UIApplication sharedApp ...

  4. 跨平台渲染框架尝试 - GPU Buffer的管理(1)

    buffer资源 下面来谈谈buffer的管理.buffer资源从广义上就是C语言的数组.如下图所示. 图 buffer的广义模型 在渲染管线中,无论是opengl还是dx或者其他的渲染api,都会提 ...

  5. Android 开发技术流程

    1.网络连接通信 HttpClient 类通信(见<第一行代码> 郭霖2014.8月第一版P385) Android Asynchronous Http Client  (见  http: ...

  6. 1、Servlet 2、ServletConfig 3、ServletContext 4、HttpUrlConnection

    1.Servlet 2.ServletConfig 3.ServletContext 4.HttpUrlConnection 07. 五 / J2EE / 没有评论   一.第一个Servlet的编写 ...

  7. JQuery中阻止事件冒泡的两种方式及其区别

    JQuery 提供了两种方式来阻止事件冒泡. 方式一:event.stopPropagation(); $("#div1").mousedown(function(event){ ...

  8. php5.5新特性之yield理解

    今天,在阅读别人代码时,其中出现了一个陌生的关键字yield,想一探究竟,于是找到:http://php.net/manual/zh/language.generators.overview.php ...

  9. Can you find it?(hdu 2141 二分查找)

    Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others ...

  10. linux设置虚拟内存(swap)解决mysql因内存不足挂掉的故障

    mysql错误日志显示: InnoDB: mmap(137363456 bytes) failed; errno 122016-03-01 01:38:42 13064 [ERROR] InnoDB: ...