• .NET Remoting Basic(4)客户端调用方式


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

    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秒的时间

    image_2

    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();
         }    
     }


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

    image_4

    4.单向访问

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

    public interface IMyRemoteObject
     {
     
         // no more oneway attribute [OneWay()]
         [OneWay()]
         void SetValue(int newval);
         int GetValue();
         String GetName();
     }
  • 相关阅读:
    数据库的数据同步
    IDEA连接数据库自动生成实体类
    异常日记(一)
    高16位,低16位是什么
    springboot整合rabbitmq
    springBoot使用教程
    分布式框架Dubbo入门
    Spring注解驱动开发04(给容器中注册组件的方式)
    Spring注解驱动开发03(按照条件注册bean)
    Spring注解驱动开发02(作用域和懒加载)
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1809573.html
Copyright © 2020-2023  润新知