.NET Remoting Basic(4)-客户端调用方式
2010-08-26 22:47 Clingingboy 阅读(433) 评论(0) 编辑 收藏 举报分同步,异步和单向方法(即无返回值方法)
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(); }
采用委托的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(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2009-08-26 Spring.NET学习笔记(6)-基础AOP