也谈 DotNet Remoting 中的事件处理

有朋友提到了在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

None.gif           Dim  chan2  As  HttpChannel
None.gif            chan2 
=   New  HttpChannel( 8086 )
None.gif
None.gif            
Dim  ic  As  IServerChannelSink  =  chan2.ChannelSinkChain
None.gif            
While   Not  (ic  Is   Nothing )
None.gif
None.gif                
If   TypeOf  (ic)  Is  SoapServerFormatterSink  Then
None.gif                    
CType (ic, SoapServerFormatterSink).TypeFilterLevel  =  Runtime.Serialization.Formatters.TypeFilterLevel.Full
None.gif                
End   If
None.gif                
If   TypeOf  (ic)  Is  BinaryServerFormatterSink  Then
None.gif                    
CType (ic, BinaryServerFormatterSink).TypeFilterLevel  =  Runtime.Serialization.Formatters.TypeFilterLevel.Full
None.gif                
End   If
None.gif                ic 
=  ic.NextChannelSink
None.gif            
End   While
None.gif

C#

None.gif             HttpChannel chan2  =   new  HttpChannel( 8086 );
None.gif
None.gif            IServerChannelSink sc
= chan2.ChannelSinkChain;
None.gif            
while (sc != null )
ExpandedBlockStart.gif            
{
InBlock.gif                
if(sc is BinaryServerFormatterSink)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    ((BinaryServerFormatterSink)sc).TypeFilterLevel
=TypeFilterLevel.Full;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if(sc is SoapServerFormatterSink)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    ((SoapServerFormatterSink)sc).TypeFilterLevel
=TypeFilterLevel.Full;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                sc
=sc.NextChannelSink;
ExpandedBlockEnd.gif            }

None.gif



当然是服务端设置一下就可以了

还有一个容易犯的错误就是:

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

你可能感兴趣的:(vb.net)