zoukankan      html  css  js  c++  java
  • Remoting方法重载遇到的一个问题

    一.没有重载的情况

         假设Remoting的接口是IComputer:

        public interface IComputer
        {
            
    int Add<TEntity>(TEntity c);       
        }

         在Remoting Server上的实现非常简单:

        public class Computer IComputer
        {        
            public int Add<TEntity>(TEntity c)
            {
                return 0;
            }  
            
        }

         然后,通过Spring.net分别在配置Server和Client的Remoting通道,接着Client作如下调用:

         IComputer remoteComupter = (IComputer)Program.SpringContext.GetObject("remotingComputer");
         
    int res2 = remoteComupter.Add<GameRecordDetail>(new GameRecordDetail());

         这个调用是成功的,没有任何问题,返回值为0。

    二.添加一个重载

         我们为IComputer接口添加一个重载方法,如下:

        public interface IComputer
        {
            int Add<TEntity>(TEntity c);
            int Add<TEntity>(IList<TEntity> list);
        }

         服务端对重载方法的实现仍然是直接返回0。这时在Client再次调用前面的方法,会抛出System.NullReferenceException。异常的堆栈位置是:      

     Server stack trace: 
           在 System.Runtime.Remoting.Messaging.MethodCall.ResolveMethod(Boolean bThrowIfNotResolved)
           在 System.Runtime.Remoting.Messaging.MethodCall..ctor(Object handlerObject, BinaryMethodCallMessage smuggledMsg)
           在 System.Runtime.Serialization.Formatters.Binary.BinaryMethodCall.ReadArray(Object[] callA, Object handlerObject)
           在 System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
           在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
           在 System.Runtime.Remoting.Channels.CoreChannel.DeserializeBinaryRequestMessage(String objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel securityLevel)
           在 System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage
    & responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)
        Exception rethrown at [
    0]: 
           在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
           在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData
    & msgData, Int32 type)
           在 RemotingLib.IComputer.Add[TEntity](TEntity c)

    三.通过修改方法名消除重载

         现将后面添加的方法重命名为Add2,Client再次调用前面的方法,一切又恢复正常。

    四.仍然使用重载,但去掉泛型参数

         现在我们恢复重载,但将泛型参数去掉,如下所示:

        public interface IComputer
        {
            
    int Add(GameRecordDetail c);
            
    int Add(IList<GameRecordDetail> list);
        }

         服务端对两个方法的实现仍然是直接返回0。Client再次调用前面的方法,一切是正常的。

    五.测试的结果

         根据上述的试验,我们可以总结出,出现上述奇怪现象的条件有以下几点:

    1.通过Remoting调用的目标方法必须是泛型的。

    2.通过Remoting调用的目标方法必须有重载。

         如果满足这两个条件,那么调用目标方法时就会抛出上面的System.NullReferenceException。那么,为什么会这样了?答案探索中......

         针对这个问题,目前我暂用的解决方案是,通过重命名方法名以消除重载。 

  • 相关阅读:
    CROC 2016
    CROC 2016
    CROC 2016
    IndiaHacks 2016
    IndiaHacks 2016
    @JsonProperty的使用
    JDK8新特性:函数式接口@FunctionalInterface的使用说明
    cannot nest '/dubboService/src/main/resources' inside '/dubboService/src/main' .To enable the nesting exclude '/resources' from '/dubboService/src/main'
    【转】关于BeanUtils.copyProperties的用法和优缺点
    JAXB--@XmlElementWrapper注解(二)
  • 原文地址:https://www.cnblogs.com/luluping/p/1858454.html
Copyright © 2011-2022 走看看