remoting 编程

博客记录了编程中序列化和远程调用的相关问题及解决办法。包括文件流操作实现对象的序列化与反序列化,解决‘由于安全限制,无法访问对象’的错误,以及处理数据库连接类在web service中无法序列化的问题,还介绍了调试remoting的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在尝试使用remoting进行分布式应用开发,出现一些小错误,让我走了不少的弯路,现记录如下。
1、 发现服务器激活对象支持不带参数的构造函数,所以改为客户端激活对象的方式;
2、客户端激活对象由于是知名对象,可以采用http://localhost/*.soap?wsdl的方式查看远程对象是否已经配置,可是客户端激活对象不支持这种方式查看;
3、不知是调用的远程remoting对象还是本地对象,通过如下代码检查对象是否是代理对象:
if (RemotingServices.IsTransparentProxy(obj))
   {
    Console.WriteLine("transparent proxy");
    RealProxy proxy = RemotingServices.GetRealProxy(obj);
    Console.WriteLine(proxy.ToString());
   }
4、序列化对象同时标明Serializable属性和从System.MarshalByRefObject继承,出现错误;
5、客户端组件和远程组件(通过IIS承载,位于站点的bin目录下)的版本不一致,出现错误;
6、在传递序列化对象时,出现“由于安全限制,无法访问对象”错误,我一直在查找是否权限的问题;为了判断传递的对象是否可以序列化,使用如下代码检验,发现本地序列化组件没有问题;
//反序列化对象
  public static Object DeSer()
  {
   object info;
   IFormatter formatter = new BinaryFormatter();

   Stream stream = new FileStream("sam.dat",
    FileMode.Open , FileAccess.Read , FileShare.None);
   info = formatter.Deserialize(stream);

   stream.Close();

   return info;

  }

  //序列化对象
  public static void Ser(Object info)
  {
   IFormatter formatter = new BinaryFormatter();

  Stream stream = new FileStream("sam.dat",
   FileMode.Create , FileAccess.Write  , FileShare.None);
  formatter.Serialize(stream,info);

  stream.Close();
  
  }
7、对于错误“由于安全限制,无法访问对象”,最终发现是由于序列化对象的组件添加了强名,引起的错误。去掉强名就不会出现这个错误。这个问题困扰我两天。
开始以为我的Remoting配置基本可以了,可最近又出现了一些问题,自己进行简单的摸索和实践,记录如下。
 8 、发现自己的数据库连接类ConnectionInfo在web service中作为参数出现无法序列化的错误,由于有public的属性是CultureInfo 的,无法实现序列化(由于CultureInfo 没有实现ISerializable接口),只好把类型改为string;既然remoting调用远程对象时不便于调试,所以可以先把需要序列化的类用Web Service调试通过再使用,也是一种调试remoting的方法;
9、前面说到出现错误“由于安全限制,无法访问对象”的错误,和强名有关,其实那不是真正的解决办法;可以通过在配置文件中设置 typeFilterLevel="Full" 来解决,客户端和服务器端的配置文件都需要设置。参见:
http://www.thinktecture.com/Resources/RemotingFAQ/Changes2003.html

You can do this either by using a configuration file like this (for server side):

<configuration>
 <system.runtime.remoting>
  <application>
   <channels>
    <channel ref="http" port="1234">		
     <serverProviders>			
      <provider ref="wsdl" />			
      <formatter ref="soap" typeFilterLevel="Full" />			
      <formatter ref="binary" typeFilterLevel="Full" />
     </serverProviders>
     <clientProviders>
      <formatter ref="binary" />
     </clientProviders>
    </channel>
   </channels>
   <service>
    <!-- ... Add your services here ... -->
   </service>
  </application>
 </system.runtime.remoting>
</configuration>

In this case, a matching client-side configuration file which allows the reception of events and callbacks, would look like this:

<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>
   <client>
     <!-- ... Add your classes here ... -->
   </client>
  </application>
 </system.runtime.remoting>
</configuration>

10、按照如上的格式进行配置文件的修改后,终于不出现“由于安全限制,无法访问对象”的错误了,可是出现
“mscorlib.dll 中出现无法处理的异常类型 System.Runtime.Serialization. SerializationException。
其他信息:BinaryFormatter 版本不兼容。收到的版本为 1008738336.1684104552”的错误,
这种错误大部分“不是”因为版本不兼容,而是因为客户端无法分析文本格式的错误响应。为了真正的错误信息,我把
格式程序从binary改为soap
11、把需要序列化的类ConnectionInfo和其他remoting远程类一样,添加到配置文件中(服务器端Web.Config中的<service> 节和客户端配置文件中),哈哈,终于通过了。(不过我还是没明白序列化的类为何也要进行配置?)
12、发现静态(static)的方法即使进行Remoting配置,调用的竟然是本的方法,看来如果需要调用远程的static方法,
只有再封装成实例方法供客户端调用了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值