.NET Remoting Basic(4)-客户端调用方式
2010-08-26 22:47
459 查看
分同步,异步和单向方法(即无返回值方法)
1.Servcie端测试代码
2.同步访问
即一般的访问方法
测试结果,共花了11秒的时间
3.异步访问
采用委托的BeginInvoke方法和EndInvoke方法
测试结果
虽然增加了复杂度,但提高了程序相应速度
4.单向访问
当方法无返回值时,可用OneWayAttribute来标记,调用方法相同,同时支持同步与异步操作.
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(); }
相关文章推荐
- 客户端调用第三方remoting 服务程序的n中方式,VB.NET 晚期邦定还真方便
- 客户端调用第三方remoting 服务程序的n中方式,VB.NET 晚期邦定还真方便
- 客户端调用第三方remoting 服务程序的n中方式,VB.NET 晚期邦定还真方便
- .NET Remoting Basic(10)-创建不同宿主的客户端与服务器端
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- .NET Remoting Basic(2)-激活方式
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- Remoting客户端和服务端两种方式的调用总结
- Ajax.net调用时客户端报错
- JAVA+AXIS客户端调用Asp.net Web Service过程中遇到的问题及解决方法
- 使用C#创建webservice及三种调用方式 (ASP.NETweb编程常用到的27个函数集)
- .NET Remoting Basic(8)-事件
- Java 调用C# webservice接口 生成java客户端 实现方式
- [Remoting] 六:异步调用(收藏转帖http://www.rainsts.net/article.asp?id=413)
- 简单CXF方式的webService客户端调用范例
- SSIS使用OleDB和Ado.Net两种方式调用 存储过程
- 以编程方式向 ASP.NET 控件添加客户端事件处理程序--TextBox.Attributes.Add()
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
- CXF的三种发布方式和客户端调用方法
- HTTP请求中的Body构建——.NET客户端调用JAVA服务进行文件上传