65.9K
CodeProject 正在变化。 阅读更多。
Home

.NET Remoting:从版本 1.0 到 1.1 的障碍路径

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.73/5 (61投票s)

2004年3月31日

2分钟阅读

viewsIcon

229298

downloadIcon

3265

休斯顿,我们遇到问题了。人类迈出的一小步,却是开发者的一大烦恼。

引言

本文旨在帮助那些希望在 Framework 1.1* 上使用 .NET Remoting 的朋友们。本文不会教你 Remoting,主要是因为我不是这方面的专家。此外,我的 CodeProject 同事们已经发表了一些关于这个问题的有用且易于阅读的文章(请参阅下面的链接)。 附带的项目尽可能保持简单,以便你克服 Framework 1.1* 带来的变化。它处理了安全异常、序列化和委托问题。

背景

最近,我面临着通过 .NET Remoting 暴露对象的问题。 像你们大多数人一样,我从 MSDN 和 CodeProject 开始,但所有的例子都适用于 Framework 1.0。 尝试在 1.1 Framework 上运行 1.0 项目会导致大量的异常。

  • 类型 System.DelegateSerializationHolder 及其派生类型(例如 System.DelegateSerializationHolder)不允许在此安全级别下进行反序列化。
  • 由于安全限制,无法访问类型 System.Runtime.Remoting.ObjRef
  • 此远程代理没有通道接收器,这意味着服务器没有注册正在侦听的服务器通道,或者此应用程序没有合适的客户端通道来与服务器通信。

网络上充斥着开发者对相同问题的抱怨,但我没有找到一个简单、纠正且全面的示例。 所以,这就是它!

代码片段

通过配置文件激活

服务器端配置

 <system.runtime.remoting>
    <application name="ServerAssembly" >
     <service>
          <!-- type: is the full type name 
(type the class that inherit from MBR,assembly) of the 
object-->
          <!-- objectUri - alias -->
          <!-- Server tells remoting Here's a type 
Here's how and when to instantiate the type
Here's the name (end point) a client will use to contact the type
            -->            
  
        <wellknown mode="Singleton" 
            type="SharedAssembly.SharedObj, SharedAssembly" 

objectUri="ParachuteExample" />
           </service>
         <channels>
            <channel ref="tcp" port="6123">    
                <serverProviders>            
                    <formatter ref="binary" typeFilterLevel="Full" />
                </serverProviders>                
            </channel>    
         </channels>     
    </application>
  </system.runtime.remoting>

服务器端代码

RemotingConfiguration.Configure ("ServerAssembly.exe.config");

客户端配置

   <system.runtime.remoting>
      <application>
         <client>
            <wellknown 
               type="SharedAssembly.SharedObj, SharedAssembly"
               url="tcp://:6123/ParachuteExample"
            />
         </client>
       <channels>
    <channel ref="tcp" port="0">        
     <clientProviders>            
      <formatter ref="binary" />
     </clientProviders>
     <serverProviders>            
      <formatter ref="binary" typeFilterLevel="Full" />
     </serverProviders>            
    </channel>
   </channels>

      </application>

客户端代码

RemotingConfiguration.Configure ("ClientAssembly.exe.config");
SharedObj remObject = new SharedObj();

通过代码激活

服务器端

    BinaryClientFormatterSinkProvider clientProvider = null;
    BinaryServerFormatterSinkProvider serverProvider = 
       new BinaryServerFormatterSinkProvider();
    serverProvider.TypeFilterLevel = 

    System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                
    IDictionary props = new Hashtable();
    props["port"] = 6123;
    props["typeFilterLevel"] = TypeFilterLevel.Full;
    TcpChannel chan = new TcpChannel(
    props,clientProvider,serverProvider);

    ChannelServices.RegisterChannel(chan);

    RemotingConfiguration.RegisterWellKnownServiceType(typeof(SharedObj),
                    "ParachuteExample",
                    WellKnownObjectMode.Singleton);

客户端

    BinaryClientFormatterSinkProvider clientProvider = 
       new BinaryClientFormatterSinkProvider();
    BinaryServerFormatterSinkProvider serverProvider = 
       new BinaryServerFormatterSinkProvider();
    serverProvider.TypeFilterLevel = 

    System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                
    IDictionary props = new Hashtable();
    props["port"] = 0;
    string s = System.Guid.NewGuid().ToString();
    props["name"] = s;
    props["typeFilterLevel"] = TypeFilterLevel.Full;
    TcpChannel chan = new TcpChannel(
    props,clientProvider,serverProvider);

    ChannelServices.RegisterChannel(chan);


    Type typeofRI = typeof(IParachute);
    IParachute remObject = (IParachute)Activator.GetObject(    typeofRI,
                    "tcp://:6123/ParachuteExample");

使用代码

由于有些人喜欢配置文件,而另一些人则喜欢通过代码连接和创建知名对象,因此我包含了两个项目。 这两个项目,codeActivationExample.zipconfigFileExample.zip,包含相同的程序集,如下所示

  • ClientAssembly
  • ServerAssembly
  • SharedAssembly
© . All rights reserved.