分同步,异步和单向方法(即无返回值方法)

1.Servcie端测试代码

class MyRemoteObject: MarshalByRefObject, IMyRemoteObject{int myvalue;public MyRemoteObject() {Console.WriteLine("MyRemoteObject.Constructor: New Object created");}public void SetValue(int newval){Console.WriteLine("MyRemoteObject.setValue(): old {0} new {1}",myvalue,newval);// we simulate a long running action Console.WriteLine("     .setValue() -> waiting 5 sec before setting value");Thread.Sleep(5000);myvalue = newval;Console.WriteLine("     .setValue() -> value is now set");}public int GetValue(){Console.WriteLine("MyRemoteObject.getValue(): current {0}",myvalue);return myvalue;}public string GetName() {Console.WriteLine("MyRemoteObject.getName(): called");// we simulate a long running action Console.WriteLine("     .getName() -> waiting 5 sec before continuing");Thread.Sleep(5000);Console.WriteLine("     .getName() -> returning name");return "John Doe";}}class ServerStartup{static void Main(string[] args){Console.WriteLine ("ServerStartup.Main(): Server started");HttpChannel chnl = new HttpChannel(1234);ChannelServices.RegisterChannel(chnl);RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemoteObject),"MyRemoteObject.soap", WellKnownObjectMode.Singleton);// the server will keep running until keypress.Console.ReadLine();}}

2.同步访问

即一般的访问方法

static void Main(string[] args){DateTime start = System.DateTime.Now;HttpChannel channel = new HttpChannel();ChannelServices.RegisterChannel(channel);IMyRemoteObject obj = (IMyRemoteObject) Activator.GetObject(typeof(IMyRemoteObject),"http://localhost:1234/MyRemoteObject.soap");Console.WriteLine("Client.Main(): Reference to rem.obj. acquired");Console.WriteLine("Client.Main(): Will set value to 42");try{obj.SetValue(42);Console.WriteLine("Client.Main(): Value successfully set.");}catch (Exception e){Console.WriteLine("Client.Main(): EXCEPTION.\n{0}", e.Message);}Console.WriteLine("Client.Main(): Will now read value");int tmp = obj.GetValue();Console.WriteLine("Client.Main(): New server side value {0}", tmp);Console.WriteLine("Client.Main(): Will call getName()");String name = obj.GetName();Console.WriteLine("Client.Main(): received name {0}",name);DateTime end = System.DateTime.Now;TimeSpan duration = end.Subtract(start);Console.WriteLine("Client.Main(): Execution took {0} seconds.",duration.Seconds);Console.ReadLine();}    

测试结果,共花了11秒的时间

3.异步访问

采用委托的BeginInvoke方法和EndInvoke方法

class Client{delegate void SetValueDelegate(int value);delegate String GetNameDelegate();static void Main(string[] args){DateTime start = System.DateTime.Now;HttpChannel channel = new HttpChannel();ChannelServices.RegisterChannel(channel);IMyRemoteObject obj = (IMyRemoteObject) Activator.GetObject(typeof(IMyRemoteObject),"http://localhost:1234/MyRemoteObject.soap");Console.WriteLine("Client.Main(): Reference to rem.obj. acquired");Console.WriteLine("Client.Main(): Will call setValue(42)");SetValueDelegate svDelegate = new SetValueDelegate(obj.SetValue);IAsyncResult svAsyncres = svDelegate.BeginInvoke(42,null,null);Console.WriteLine("Client.Main(): Invocation done");Console.WriteLine("Client.Main(): Will call getName()");GetNameDelegate gnDelegate = new GetNameDelegate(obj.GetName);IAsyncResult gnAsyncres = gnDelegate.BeginInvoke(null,null);Console.WriteLine("Client.Main(): Invocation done");Console.WriteLine("Client.Main(): EndInvoke for setValue()");svDelegate.EndInvoke(svAsyncres);Console.WriteLine("Client.Main(): EndInvoke for getName()");String name = gnDelegate.EndInvoke(gnAsyncres);Console.WriteLine("Client.Main(): received name {0}",name);Console.WriteLine("Client.Main(): Will now read value");int tmp = obj.GetValue();Console.WriteLine("Client.Main(): New server side value {0}", tmp);DateTime end = System.DateTime.Now;TimeSpan duration = end.Subtract(start);Console.WriteLine("Client.Main(): Execution took {0} seconds.",duration.Seconds);Console.ReadLine();}    }

测试结果
虽然增加了复杂度,但提高了程序相应速度

4.单向访问

当方法无返回值时,可用OneWayAttribute来标记,调用方法相同,同时支持同步与异步操作.

public interface IMyRemoteObject{// no more oneway attribute [OneWay()][OneWay()]void SetValue(int newval);int GetValue();String GetName();}

.NET Remoting Basic(4)-客户端调用方式相关推荐

  1. BizTalk Orchestration Publish Host In-Process Wcf Service without IIS 多种供客户端调用方式

    BizTalk Orchestration Publish Host In-Process Wcf Service without IIS 多种供客户端调用方式 BizTalk Server 2006 ...

  2. WebService的四种客户端调用方式(基本)

    转载:http://blog.csdn.net/csdn_gia/article/details/54863549 web服务网址:http://www.webxml.com.cn/zh_cn/web ...

  3. redis集群连接 java_Redis分布式集群和直连的Java客户端调用方式详解

    jedis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分布式 ...

  4. WebService客户端调用常见5种方式

    之前系统中使用到了webservice进行第三方通信,这里总结一下常见的5种客户端调用方式. 在此之前我们先简单搭建一个webservice服务端项目,发布一个webservice服务.我这里使用sp ...

  5. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍(转)

    [-] 一普通同步方式 二事务方式Transactions 三管道Pipelining 四管道中调用事务 五分布式直连同步调用 六分布式直连异步调用 七分布式连接池同步调用 八分布式连接池异步调用 九 ...

  6. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍--转载

    原文地址:http://www.blogways.net/blog/2013/06/02/jedis-demo.html redis是一个著名的key-value存储系统,而作为其官方推荐的java版 ...

  7. Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍

    一.普通同步方式 二.事务方式(Transactions) 三.管道(Pipelining) 四.管道中调用事务 五.分布式直连同步调用 六.分布式直连异步调用 七.分布式连接池同步调用 八.分布式连 ...

  8. 客户端调用 WCF 的几种方式

    转载网络代码.版权归原作者所有..... 客户端调用WCF的几种常用的方式:1普通调用var factory = new DataContent.ServiceReference1.CustomerS ...

  9. WebService客户端三种调用方式整理

    1 WebService基础 1.1 作用 1,       WebService是两个系统的远程调用,使两个系统进行数据交互,如应用: 天气预报服务.银行ATM取款.使用邮箱账号登录各网站等. 2, ...

  10. java redis管道_Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍

    jedis是一个著名的key-value存储系统,而作为其官方推荐的java版客户端jedis也非常强大和稳定,支持事务.管道及有jedis自身实现的分布式. 在这里对jedis关于事务.管道和分布式 ...

最新文章

  1. R语言可视化散点图(scatter plot)、并在散点图中叠加回归曲线、叠加lowess拟合曲线(linear and lowess fit lines)、使用plot、line、abline函数
  2. 调查一下 大家用vs时做网页时,都用的什么标准?
  3. cmd c语言 图形,CMD-C彩图隐写方案
  4. java开发就业困难吗_就业困难期,他们面临着幸福的烦恼
  5. 火狐与Chrome浏览器的移动端调试模式(手机页面、自适应)
  6. 利用 Finder 清理Mac旧档案,释放空间
  7. gridview求和
  8. 学习总结-《父与子的编程之旅》chapter 9
  9. 用原生javascript制作日历
  10. 用例图、类图之间的几种关系
  11. struts2拦截器添加及xss攻击的处理
  12. 电力电子课程设计:简易密码控制装置
  13. pkl形式的数据集读取和可视化
  14. 手机端和pc端浏览器兼容性问题
  15. Python格式化JSON文件
  16. 2003服务器系统QQ安装不了,qq怎么安装不了(QQ怎么都安装不上重装也不行,是哪里出了问题?)...
  17. TIA博途WINCC的触摸屏VB脚本入门学习(IF THEN ELSE判断语句)
  18. android 响铃函数,android – 铃声一遍又一遍地播放(无限循环播放)
  19. 城市大脑一网统管建设解决方案 城市大脑一网统管及领导驾驶舱系统建设解决方案
  20. MySQL命令行操作

热门文章

  1. Codeforces 853A 贪心 优先队列
  2. Spark学习笔记6:Spark调优与调试
  3. Java Synchronized的用法
  4. L - Farm Irrigation (并查集
  5. Struts2出现的问题:
  6. 常用20个正则表达式
  7. 【前端技术】一篇文章搞掂:微信小程序
  8. 记一次针对Centos的入侵分析
  9. request.getRequestDispatcher(url).forward(request, response)
  10. VS2010对Excel操作---DLL向