升级到.NET 4.0后WCF回调异常

11

我有一个使用DualHttpBindings的WCF服务,其中包含回调。该服务在找到搜索结果时向客户端(长时间运行的搜索)推送搜索结果的datatable。

在.NET 3.5中,这个功能运行良好。自从我升级到.NET 4.0以后,它出现了System.Runtime.FatalException错误,并且实际上导致IIS工作进程关闭。我都不知道如何开始修复。欢迎任何建议。

以下是事件日志的信息:


发生未处理的异常,进程已终止。

应用程序ID: / LM / W3SVC / 2 / ROOT / CP

进程ID: 5284

> 异常: System.Runtime.FatalException

> 消息:对象引用未设置对象实例。

StackTrace: at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext) at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext) at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result) at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously) at System.Runtime.InputQueue1.AsyncQueueReader.Set(Item item) at System.Runtime.InputQueue1.Dispatch() at System.ServiceModel.Channels.ReliableDuplexSessionChannel.ProcessDuplexMessage(WsrmMessageInfo info) at System.ServiceModel.Channels.ReliableDuplexSessionChannel.HandleReceiveComplete(IAsyncResult result) at

System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnReceiveCompletedStatic(IAsyncResult result) 在 System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) 内部处理未捕获的异常,在 System.ServiceModel.Channels.ReliableChannelBinder1.InputAsyncResult1.OnInputComplete(IAsyncResult result) 内完成输入操作,然后在 System.Runtime.InputQueue1.AsyncQueueReader.Set(Item item) 之内设置Item项。接着在 System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) 内进行调度,最后在 System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) 中执行IO完成回调。
InnerException: System.NullReferenceException
消息:对象引用未设置为对象的实例。
StackTrace: 在 System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext) 中进入线程上下文,在 System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) 处于私有状态时调用,然后在 System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) 或 System.Web.AspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state) 调用回调函数。最后在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 中处理消息。
1个回答

15

好的 - 我找到了答案。非常奇怪,但是只需要在WCF回调包装类上放置以下属性:

[CallbackBehavior(UseSynchronizationContext=false)]

感谢Cauldwell.net的回答:http://www.cauldwell.net/patrick/blog/CategoryView,category,CodeGen.aspx

来自cauldwell.net:

问题在于,ASP.NET(默认情况下)使用了一个名为SynchronizationContext的小东西。据我所知(老实说,我还没有深入研究这个),它的工作之一是确保任何回调都在UI线程上运行,从而避免了在WinForms中需要调用Control.Invoke的需求。在我的情况下,该附加锁定给某些内容带来了困扰,因此它试图在不存在的线程上清理内容,导致了NullReferenceException。


成功了!虽然我的情况与文章有些不同,因为异常是在回调方法被调用之前抛出的。 - Jan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接