Remoting方法重载遇到的一个问题

本文探讨了通过Remoting调用重载泛型方法时遇到的问题,包括正常调用、添加重载后的异常及解决办法,并分析了出现此问题的条件。

在通过Remoting调用重载的泛型方法时,遇到了一个奇怪的问题,现使用一个例子一步步来说明如下。

一.没有重载的情况

假设Remoting的接口是IComputer:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicinterfaceIComputer
{
intAdd<TEntity>(TEntityc);
}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> publicclassComputer:IComputer
{
publicintAdd<TEntity>(TEntityc)
{
return0;
}

}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->IComputerremoteComupter=(IComputer)Program.SpringContext.GetObject("remotingComputer");
intres2=remoteComupter.Add<GameRecordDetail>(newGameRecordDetail());

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

二.添加一个重载

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicinterfaceIComputer
{
intAdd<TEntity>(TEntityc);
intAdd<TEntity>(IList<TEntity>list);
}

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->Serverstacktrace:
在System.Runtime.Remoting.Messaging.MethodCall.ResolveMethod(BooleanbThrowIfNotResolved)
在System.Runtime.Remoting.Messaging.MethodCall..ctor(ObjecthandlerObject,BinaryMethodCallMessagesmuggledMsg)
在System.Runtime.Serialization.Formatters.Binary.BinaryMethodCall.ReadArray(Object[]callA,ObjecthandlerObject)
在System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandlerhandler,__BinaryParserserParser,BooleanfCheck,BooleanisCrossAppDomain,IMethodCallMessagemethodCallMessage)
在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(StreamserializationStream,HeaderHandlerhandler,BooleanfCheck,BooleanisCrossAppDomain,IMethodCallMessagemethodCallMessage)
在System.Runtime.Remoting.Channels.CoreChannel.DeserializeBinaryRequestMessage(StringobjectUri,StreaminputStream,BooleanbStrictBinding,TypeFilterLevelsecurityLevel)
在System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStacksinkStack,IMessagerequestMsg,ITransportHeadersrequestHeaders,StreamrequestStream,IMessage
&responseMsg,ITransportHeaders&responseHeaders,Stream&responseStream)
Exceptionrethrownat[
0]:
在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessagereqMsg,IMessageretMsg)
在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData
&msgData,Int32type)
在RemotingLib.IComputer.Add[TEntity](TEntityc)

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

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

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

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicinterfaceIComputer
{
intAdd(GameRecordDetailc);
intAdd(IList<GameRecordDetail>list);
}

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

五.测试的结果

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

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值