.Net组件程序设计之远程调用(二)

时间:2022-02-16 04:32:06

.Net组件程序设计之远程调用(二)

激活模式

引用封送对象激活类型两种,

一种是客户端激活类型,一种是服务器端激活.

客户端激活对象

客户端激活方式:当客户端创建一个远程对象时,客户端得到的是一个新的实例引用,新的实例可以在内存中维持状态,并且可以使用参数化构造函数来激活远程对象。

服务器激活模式single-call

SingleCall激活方式:当客户端使用一个服务器激活方式为SingleCall的对象时,.NET为每个方法调用创建一个新对象,在方法执行完毕后,对象则被销毁,客户端虽然维持着对远程对象的引用,但是真实对象已经被销毁了。

服务器激活Singleton

Singleton激活方式:所有客户端共享一个远程对象。

下面为大家演示几段示例,就详细的清楚每一种激活方式的作用了。

服务端代码

     using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
namespace RemoteServer
/// <summary>
/// 服务端
/// </summary>
public class MyClass : MarshalByRefObject
{
public MyClass()
{
_count++;
string mes = AppDomain.CurrentDomain.FriendlyName;
Console.WriteLine(mes);
} private int _count = ; public event EventHandler NumberChanged; public void Count()
{
_count++;
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName + "_" + _count.ToString());
} public void OnNumberChanged(int num)
{
if (NumberChanged != null)
{
NumberChanged(num, null);
}
}
} public class EventMehtodClass : MarshalByRefObject
{
[OneWay]
public void OnNumberChanged(object sender, EventArgs e)
{
if (sender != null)
{
Console.WriteLine(sender.ToString());
}
}
}

2.编程式:

 宿主服务端(服务器端)
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Services;
using RemoteServer; namespace RemoteServerHost #region 编程式 宿主服务端注册 信道和对象类型
Console.WriteLine("开始Tcp注册信道");
IChannel tcpChannel = new TcpChannel();
ChannelServices.RegisterChannel(tcpChannel, false);//注册Tcp类型信道
Console.WriteLine("Tcp信道注册完成————————————————");
Console.WriteLine("开始 服务器激活类型SingleCall模式的宿主服务器端类型注册");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyClass), "RWCTmyclass",WellKnownObjectMode.SingleCall);
Console.WriteLine("类型注册完成");
#endregion Thread.Sleep(new TimeSpan(, , ));
Console.WriteLine("释放Tcp信道");
ChannelServices.UnregisterChannel(tcpChannel);

这里所用的激活方式是 服务器SingleCall激活方式,通过RemotingConfiguration.RegisterWellKnownServiceType()方法来注册类型,第二个参数为统一资源标识符(URI),很好理解就是字面的意思,远程对象就是资源,标识资源的一个符号(名称),有了它,客户端在调用远程对象的时候才知道具体在哪。因为是服务器激活方式所以URI是在包含在注册类型的方法中的,而如果是客户端激活类型注册的话,则使用RemotingConfiguration.RegisterActivatedServiceType(typeof(MyClass));

有的朋友会问了,这样统一资源标识符不是没设置吗?是的,确实没有设置,客户端激活类型注册的时候URI是这样设置

 RemotingConfiguration.ApplicationName = "RWCTmyclass";
 客户端(调用端)
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Services;
using RemoteServer; namespace RemoteClient #region 编程式本地注册
Thread.Sleep(new TimeSpan(, , ));//便于调,使当前客户端线程阻塞7秒,确保宿主服务端已经运行
string url = "tcp://localhost:8003/RWCTmyclass";
Console.WriteLine("开始客户端注册类型");
RemotingConfiguration.RegisterWellKnownClientType(typeof(MyClass), url);
#endregion //类型使用
MyClass myclass = new MyClass();
myclass.Count();
MyClass myclass1 = new MyClass();
myclass1.Count();

图 1

.Net组件程序设计之远程调用(二)

因为这里使用的是服务器SingleCall类型模式激活的,所以对象状态是不保存的,就跟上面那一小节定义的一样,只有在方法调用的时候才会被创建,方法调用完毕则会被释放销毁,但是也可以在宿主服务端使用变量来记录状态。现在可以在宿主服务端(服务器端)注册类型的时候把激活模式换成Singleton的再来看一下结果:
这是编程式的示例代码,下面给大家演示配置式的。

3.配置式:

 宿主服务端(服务器端)
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Services; using System.Threading; using RemoteServer; namespace RemoteServerHost #region 配置式宿主服务端注册 信道和对象类型
Console.WriteLine("开始注册Http信道");
RemotingConfiguration.Configure(AppDomain.CurrentDomain.FriendlyName + ".config");
Console.WriteLine("Http信道注册完成————————————————");
#endregion

这里只要使用.NET给我们提供的RemotingConfiguration类型中的Configure()方法来加载配置文件就行了,注册信道的类型,远程对象的激活方式和模式都是在配置文件中注册的,来看配置文件信息:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http" port="8004"/>
</channels>
<service>
<wellknown mode="Singleton" type="RemoteServer.MyClass,RemoteServer" objectUri="RTMyClass" />
</service>
</application>
</system.runtime.remoting>
</configuration>

这里配置文件中,是准备注册http类型的信道,并且把远程对象注册为服务器激活类型(wellKnow)Singleton激活模式,宿主服务端的就这么多,接下来看客户端的.

 客户端
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Services; using System.Threading; using RemoteServer; namespace RemoteClient #region 配置式本地注册
Thread.Sleep(new TimeSpan(, , ));
Console.WriteLine("开始客户端注册类型");
RemotingConfiguration.Configure(AppDomain.CurrentDomain.FriendlyName + ".config", false);
#endregion
//类型使用
Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
MyClass myclass = new MyClass();
myclass.Count();
MyClass myclass1 = new MyClass();
myclass1.Count();

跟宿主服务端的一样,使用Configure()方法来注册远程对象,但是本地的配置文件怎么配置呢?不着急的,一起来看:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown type="RemoteServer.MyClass,RemoteServer" url="http://localhost:8004/RTMyClass"/>
</client>
</application>
</system.runtime.remoting>
</configuration>

客户端 要注册的 远程对象类型 的 激活类型(客户端激活、服务器端激活) 是要跟宿主服务端的相对应。
这次换了Singleton模式

图2

.Net组件程序设计之远程调用(二)

4.远程回调

 宿主服务端:
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Services;
using System.Threading; using RemoteServer;
namespace RemoteServerHost #region 远程回调
Console.WriteLine("开始Tcp注册信道");
BinaryServerFormatterSinkProvider formatter = new BinaryServerFormatterSinkProvider();//二进制格式化器
formatter.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;//设置过滤类型为Full IDictionary channel = new Hashtable();
channel["name"] = "RWCTmyclass";
channel["port"] = ; IChannel tcpChannel = new TcpChannel(channel, null, formatter);
ChannelServices.RegisterChannel(tcpChannel, false);//注册Tcp类型信道
Console.WriteLine("Tcp信道注册完成————————————————");
Console.WriteLine("开始 服务器激活类型Singleleton模式的宿主服务器端类型注册");
RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyClass), "RWCTmyclass", WellKnownObjectMode.Singleton);
Console.WriteLine("类型注册完成");
#endregion Thread.Sleep(new TimeSpan(, , )); Console.WriteLine("释放Tcp信道");
ChannelServices.UnregisterChannel(tcpChannel);
 客户端:

 using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Services; using System.Threading; using RemoteServer; namespace RemoteClient #region 远程回调
Thread.Sleep(new TimeSpan(, , ));
IChannel tcp = new TcpChannel();
ChannelServices.RegisterChannel(tcp, false);
string url = "tcp://localhost:8003/RWCTmyclass";
Console.WriteLine("开始客户端注册类型");
RemotingConfiguration.RegisterWellKnownClientType(typeof(MyClass), url);
#endregion Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);
MyClass myclass = new MyClass();
myclass.Count();
EventMehtodClass methodclass = new EventMehtodClass();
myclass.NumberChanged += methodclass.OnNumberChanged;
Thread.Sleep();
myclass.OnNumberChanged();

图-3

.Net组件程序设计之远程调用(二)

在Singleton激活方式下是可以完成远程回调的,但是用Singlecall模式的话则不行,因为如果是这样的话,之前对远程对象(服务器对象)的操作都没有状态保存,上面说到过,Singlecall模式是一来一回则被释放掉了,本地客户端仅仅是保留了一个代理。可以验证一下:

修改宿主服务端的代码Singleton改为SingleCall

 RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyClass), "RWCTmyclass", WellKnownObjectMode.SingleCall);

修改客户端的代码 myclass.Count();又新加一句

             MyClass myclass = new MyClass();
myclass.Count();
myclass.Count();
EventMehtodClass methodclass = new EventMehtodClass();
myclass.NumberChanged += methodclass.OnNumberChanged;
Thread.Sleep();
myclass.OnNumberChanged();

修改后的运行结果:
图4

.Net组件程序设计之远程调用(二)

为什么没有像图1中的那样发生回调这里已经不用说明了。

作者:金源

出处:http://www.cnblogs.com/jin-yuan/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面