有朋友提到了在Remoting 中的事件处理的问题,我 google 了一下,发现几篇有用的文章。
大坏蛋 的 Dotnet Remoting 事件处理
http://blog.joycode.com/joe/archive/2004/11/09/38437.aspx
创建以Microsoft .NET Remoting为基础的分布式应用架构
卢彦
http://www.microsoft.com/china/community/Column/62.mspx
思路很简单,如果你去模仿写一个的话,还是很容易出错。
大概思想:
所谓事件的订阅发布模型,有一个前提就是程序必须是有状态的。这一点很重要,就应为这一点所以 http web service 无法直接实现事件模型。
对于Remoting 的事件,也必须保证远程对象是有状态的。
所以如果你用服务端激活的话,必须为 Singleton 模型。singlecall 的话,你会发现程序调试没有问题。事件处理程序言根儿就轮不到执行:)
初学者很容易碰到 一下错误
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Additional information: Type System.DelegateSerializationHolder and the types derived from it (such as System.DelegateSerializationHolder) are not permitted to be deserialized at this security level.
这个问题大坏蛋解释过了,只要在程序中配置或者硬编码设置服务端 ChannelSink 中 Formatter 信道的 TypeFilterLevel 为Full 就可以了。
比如一下代码
vb.net
chan2 = New HttpChannel( 8086 )
Dim ic As IServerChannelSink = chan2.ChannelSinkChain
While Not (ic Is Nothing )
If TypeOf (ic) Is SoapServerFormatterSink Then
CType (ic, SoapServerFormatterSink).TypeFilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Full
End If
If TypeOf (ic) Is BinaryServerFormatterSink Then
CType (ic, BinaryServerFormatterSink).TypeFilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Full
End If
ic = ic.NextChannelSink
End While
C#
IServerChannelSink sc = chan2.ChannelSinkChain;
while (sc != null )
{
if(sc is BinaryServerFormatterSink)
{
((BinaryServerFormatterSink)sc).TypeFilterLevel=TypeFilterLevel.Full;
}
if(sc is SoapServerFormatterSink)
{
((SoapServerFormatterSink)sc).TypeFilterLevel=TypeFilterLevel.Full;
}
sc=sc.NextChannelSink;
}
当然是服务端设置一下就可以了
还有一个容易犯的错误就是:
n unhandled exception of type 'System.Runtime.Remoting.RemotingException' occurred in mscorlib.dll
Additional information: This remoting proxy has no channel sink which means either the server has no registered server channels that are listening, or this application has no suitable client channel to talk to the server.
这个只要把客户端的 channel端口号改为0 ,让系统随意"折腾"即可。
Dim chan As HttpChannel
chan = New HttpChannel(0)