据lovecherry的一步一步学Remoting序列文章学习.net Remoting日记(2)

  今天学习了服务器端激活和客户端激活的区别!可还是出现了一点点的差错,经过对比得到正确的调用方法,整理如下:

1.服务器端激活,分为两种方式Singleton和SingleCall方式

  Server端App.config设置,在这里wellknown指向了类库RemoteObject的Myobject类,设置的Uri地址为RemoteObject.MyObject;采用的模式是Singleton模式。

且通道协议是Tcp协议,端口号为9999。

服务器端app.config
<configuration>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject"
mode
="Singleton" />
</service>
<channels>
<channel ref="tcp" port="9999"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>


  Client端App.config设置,在这里设置了连接关键字ServiceURL,这个连接关键字的值tcp://localhost:9999/RemoteObject.MyObject,也就是采用tcp协议访问本地的9999端口号的Uri为RemoteObject.MyObject的服务器端。

<configuration>
<appSettings>
<add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/>
</appSettings>
</configuration>

  客户端调用RemoteObject.MyObject类的实现代码如下,Activator.GetObject方法创建当前运行的远程对象、由服务器激活的已知对象或 XML Web services 的代理。您可以指定连接介质(即通道)。

代码
RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),
System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
 RemoteObject.MBV mbv = app.GetMBV();

2.客户端激活。在客户端请求的时候就激活了对象。

  Server端App.config设置如下,设置

代码
<configuration>
<system.runtime.remoting>
<application name="RemoteServer">
<service>
<activated type="RemoteObject.MyObject,RemoteObject"/>
</service>
<channels>
<channel ref="tcp" port="9999"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>

  Client端App.config设置如下,在这里Value值为tcp://localhost:9999/RemoteServer,这里的Uri指向的是Server端App.config文件中Application节的名字。

<configuration>
<appSettings>
<add key="ServiceURL" value="tcp://localhost:9999/RemoteServer"/>
</appSettings>
</configuration>

  

  Client调用远程对象代码如下,在这里使用的是Activator.CreateInstance方法,该通过调用与指定参数匹配程度最高的构造函数来创建在程序集中定义的类型的实例。如果没有指定任何参数,则将调用不带任何参数的构造函数(即默认构造函数)。

代码
RemoteObject.MyObject app=(RemoteObject.MyObject)Activator.CreateInstance(typeof(RemoteObject.MyObject),
new object[]{10},
new object[]{new System.Runtime.Remoting.Activation.UrlAttribute(System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"])});

  以上内容皆为学习了lovecherry的一步一步学Remoting之一:从简单开始等文章之后得出的经验,本人水平有限,如果有概念不清,或者理解有误的地方欢迎大家指点,让我不断进步!

原文地址:https://www.cnblogs.com/chengxingliang/p/1761019.html