如何使用Remoting实现双工(转自Artech)

Remoting是NET平台下比较成熟高效的分布式技术,我们习惯采用传统的远程调用的方式使用Remoting。在客户端所在的Application Domain,我们通过Proxy(Transparent Proxy)远程地跨Application Domain调用一个方法。当来自Client端的调用请求通过Proxy到达Server端所在的Application Domain后,Remoting Infrastructure在Server 端激活(Activate)相应的远程对象(一个继承子System.MarshalByRefObject类对象)——这里仅仅以服务端激活对象(Server Activated Object——SAO),然后再Server端执行相应的操作后把Result传递给Proxy,并最终到达Client。这是一种典型的Request/Response的调用方式。

我之所以一直比较推崇在.NET平台下使用Remoting而非XML Web Service是因为我觉得.NET Remoting是一种比较成熟的分布式技术。它自身提供了XML Web Service很多不具备的特性,其中对双向通信的支持就是一个很好的体现。

相对于典型的Request/Response的消息交换模式(Message Exchange Pattern——MEP),双向通信实质上是采用的Duplex的MEP。也就是说,Server端在执行操作的时候,可以回调(Callback)Client端的操作(这个操作时再Client端的Application Domain中执行的)。

现在我们来看如何一步一步实现在Remoting环境下的双向通信。在下面的Sample中,我们的逻辑是:调用一个数学计算的远程调用,除了传递相应的操作数之外,我们还传递一个对象,这个对象可以在Server端中回调 (Callback) 把运算结果在Client端显示出来。
可以通过下面的URL下载源代码:
https://files.cnblogs.com/artech/Artech.DuplexRemoting.zip

Step1:构建整个Solution的整体构架。

  • Artech.DuplexRemoting.Contract:Class Library Project,定义远程对象(Remote Object)和Callback对象的Contract(Interface)。实际上,站在Server端的角度上看,Callback的操作是在Client端的Application Domain中执行的,所以从本质上讲, Callback对象是Server端的远程对象。

之所以定义这样一个Contract Project,其目的主要有以下几点:

1. 如果没有把远程对象的Interface,对已某一个需要调用这个远程对象的Client来说,它必须引用远程对象本身。从安全的角度考虑,Server向Client过多暴露了操作的实现逻辑。如果我们把远程操作的Contract提取出来,Client只要引用这个Interface就可以了。

2. 一般来说,远程对象的Contract相对时静态的(static),而业务逻辑的实现则是经常 变化的。因为Client只需要了解的是远程对象的Contract,所在无论Server端对远程对象的实现作了多大的变动,对不回对Client产生任何影响。

  • Artech.DuplexRemoting.Remoting:Class Library Project,定义远程对象本身。由于远程对象必须实现上边定义的Contract。所以需要引用Artech.DuplexRemoting.Contract。

  • Artech.DuplexRemoting.Hosting:Console Application Project,以Self-Host的方式Host Remoting。引用Artech.DuplexRemoting.Remoting。

  • Artech.DuplexRemoting.Client:Console Application Project,引用Artech.DuplexRemoting.Contract。

Step 2 在Artech.DuplexRemoting.Contract中定义Contract

IDuplexCalculator.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Artech.DuplexRemoting.Contract
{
public interface IDuplexCalculator
{
void Add(double x, double y, ICalculatorCallback callback);
}
}

ICalculatorCallback.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Artech.DuplexRemoting.Contract
{
public interface ICalculatorCallback
{
void ShowResult(double x, double y, double result);
}
}

Step 3 在Artech.DuplexRemoting.Remoting定义远程对象

DuplexCalculatorRemoting.cs

using System;
using System.Collections.Generic;
using System.Text;

using Artech.DuplexRemoting.Contract;

namespace Artech.DuplexRemoting.Remoting
{
public class DuplexCalculatorRemoting:MarshalByRefObject, IDuplexCalculator
{
IDuplexCalculator Members#region IDuplexCalculator Members

public void Add(double x, double y, ICalculatorCallback callback)
{
Console.WriteLine("Invoke the method Add({0},{1}).",x,y);
double result = x + y;
callback.ShowResult(x,y,result);
}

#endregion
}
}

Step 4 在Artech.DuplexRemoting.Hosting Host远程对象

App.config

<configuration>
<system.runtime.remoting>
<application name="Calculator">
<service>
<wellknown mode="SingleCall"
type="Artech.DuplexRemoting.Remoting.DuplexCalculatorRemoting,Artech.DuplexRemoting.Remoting"
objectUri="DuplexCalculator.soap" />
</service>

<channels>
<channel ref="http" port="8080">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Artech.DuplexRemoting.Hosting
{
class Program
{
static void Main(string[] args)
{
System.Runtime.Remoting.RemotingConfiguration.Configure("Artech.DuplexRemoting.Hosting.exe.config",false);
Console.WriteLine("Calculator service has begun to listen ");
Console.Read();
}
}
}

这里需要特别注意的有以下两点:

1. 在定义Channel是需要指定一个双向Channel(Bi-Directional Channel)。系统给我们定义一一系列的System-Defined Channel用于调用远程对象。其中有一些只能提供单向的通信——比如只支持Client到Server的通信,而另一些可以提供双向的通信——比如TCP Channel 和Http Channel.

2. 在ServerProvider Section,我们必须设置typeFilterLevel为Full。出于安全的考量,Remoting提供了两个反序列化级别(Level)——Low & Full。Low是默认的,如果把typeFilterLevel设为Low,Remoting之会反序列化Remoting基本功能相关的对象。而设为Full则意味着Remoting会反序列化所有类型。如果你想知道那些类型是在Low Level下被限制,请参考http://msdn2.microsoft.com/en-us/library/5dxse167.aspx

之所以要把typeFilterLevel为Full,是因为我们的远程调用里包含一Callback对象,它实际上是一个继承System.MarshalByRefObject类对象(这个的对象将在Artech.DuplexRemoting.Client中定义)。而这个对象是不会再Low Level下被自动反序列化。

<channels>
<channel ref="http" port="8080">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>

public interface IDuplexCalculator
{
void Add(double x, double y, ICalculatorCallback callback);
}

Step 4 Artech.DuplexRemoting.Client定义Callback对象和调用远程对象

CalculatorCallbackHandler.cs

using System;
using System.Collections.Generic;
using System.Text;
using Artech.DuplexRemoting.Contract;

namespace Artech.DuplexRemoting.Client
{
public class CalculatorCallbackHandler:MarshalByRefObject, ICalculatorCallback
{

ICalculatorCallback Members#region ICalculatorCallback Members

public void ShowResult(double x, double y, double result)
{
Console.WriteLine("x + y = {2} where x = {0} and y = {1}", x, y, result);
}

#endregion
}
}

App.config

<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="http" port="0">
<clientProviders>
<formatter ref="binary" />
</clientProviders>
<serverProviders>
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>
</configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using Artech.DuplexRemoting.Contract;

namespace Artech.DuplexRemoting.Client
{
class Program
{
static void Main(string[] args)
{
System.Runtime.Remoting.RemotingConfiguration.Configure("Artech.DuplexRemoting.Client.exe.config", false);

InvocateDuplexCalculator("http://localhost:8080/Calculator/DuplexCalculator.soap");
}

static void InvocateDuplexCalculator(string remoteAddress)
{
IDuplexCalculator proxy = (IDuplexCalculator)Activator.GetObject(typeof(IDuplexCalculator), remoteAddress);
proxy.Add(1,2,new CalculatorCallbackHandler());
Console.Read();
}
}
}

这里有两点需特别注意的:
1. 由于Server端时跨Application Domain远程地调用运行Client Application Domain中的Callback对象(Callback的执行实际是在Client而不在Server),所以Callback对象应该是一个MarshalByRefObject对象。
2. 上面我们以经提及,对于Server端了来说Callback对象实际上是一个远程对象(在Callback过程中Client端转变成Server端,而Server端转变成Client端)。Server端需要注册一些Channel用于Client访问寄宿在Server端的远程对象,同理,Server需要Callback一个寄宿在Client端Application Domain中的Callback对象,Client端需要注册相应的Channel
3. 和Server端一样,我们必须设置typeFilterLevel为Full。

到现在为止我们已经完成了所有的Program,我们来运行一下。

1. 运行Artech.DuplexRemoting.Hosting

2. 运行Artech.DuplexRemoting.Client

将远程对象HostIIS

我们知道,Remoting有两种Host方式Self Host和IIS Host,上面我们把Remoting Host到一个Console Application中; 现在我们把试着把它Host到IIS中。实际上我们要做的工作很简单。

1. 在IIS Manager中添加一个虚拟目录对应Artech.DuplexRemoting.Remoting文件夹, 假设此虚拟目录的Alias为Artech.DuplexRemoting

2. 在Artech.DuplexRemoting.Remoting根目录下中(也就是在http://localhost/Artech.DuplexRemoting根目录下)添加一个Web.config,并添加类似于Artech.DuplexRemoting.Hosting/App.Config中 的Remoting Configuration。

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<system.runtime.remoting>
<application>
<service>
<wellknown mode="SingleCall"
type="Artech.DuplexRemoting.Remoting.DuplexCalculatorRemoting,Artech.DuplexRemoting.Remoting"
objectUri="DuplexCalculator.soap" />
</service>

<channels>
<channel ref="http">
<serverProviders>
<provider ref="wsdl" />
<formatter ref="binary" typeFilterLevel="Full" />
</serverProviders>
<clientProviders>
<formatter ref="binary" />
</clientProviders>
</channel>
</channels>
</application>
</system.runtime.remoting>

<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.Transactions.Bridge, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.IdentityModel.Selectors, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
</assemblies>
</compilation>
</system.web>
</configuration>

这样我们可以不需要Hosting,就可以运行Client了。

原文地址:https://www.cnblogs.com/Skypurple/p/1845766.html