Remoting方法重載遇到的一個問題

來源:互聯網
上載者:User

     在通過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。那麼,為什麼會這樣了?答案探索中......

     針對這個問題,目前我暫用的解決方案是,通過重新命名方法名以消除重載。 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.