C# WCF Duplex:ASP.NET 4.0 Webforms客户端中接收回调的FatalException

C# WCF Duplex:ASP.NET 4.0 Webforms客户端中接收回调的FatalException,c#,asp.net,wcf,webforms,wcf-binding,C#,Asp.net,Wcf,Webforms,Wcf Binding,我在使用双工服务和使用该服务的asp.net 4.0 webforms应用程序时遇到了一个非常奇怪的问题 在我的开发机器中,代码工作正常,没有错误,但在我的应用程序测试服务器中测试相同的代码时,我得到System.Runtime.FatalException消息:当我向客户端发送回调时,对象引用未设置为对象的实例 我的开发机器是:Windows8.1,IIS8.5 测试服务器是Azure上的虚拟机:Windows server 2008数据中心,IIS 7.5 服务和客户端应用程序都是:.NET

我在使用双工服务和使用该服务的asp.net 4.0 webforms应用程序时遇到了一个非常奇怪的问题

在我的开发机器中,代码工作正常,没有错误,但在我的应用程序测试服务器中测试相同的代码时,我得到System.Runtime.FatalException消息:当我向客户端发送回调时,对象引用未设置为对象的实例

我的开发机器是:Windows8.1,IIS8.5

测试服务器是Azure上的虚拟机:Windows server 2008数据中心,IIS 7.5

服务和客户端应用程序都是:.NET Framework 4.0

值得一提的是,非常奇怪的是,我尝试在VM上远程调试应用程序,启用远程调试后,服务行为正常,但一旦卸载调试器,错误再次出现

因此,代码是:

服务

[ServiceContract(CallbackContract = typeof(IProcessCallback))]
public interface IProcess
{
    [OperationContract(IsOneWay = true)]
    void Process(ProcessRequest request);
}

public interface IProcessCallback
{
    [OperationContract(IsOneWay = true)]
    void NotifyOk(string id, string message);

    [OperationContract(IsOneWay = true)]
    void NotifyError(string id, string message);
}
服务实现

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Processor : IProcess
{
    public void Process(ProcessRequest request)
    {
        try
        {
            //Some logic here...

            var callbackChannel = GetCallback();
            callbackChannel.NotifyOk(id, msg);
        }
        catch (Exception ex)
        {
            //Some error handling logic here...

            var callbackChannel = GetCallback();
            callbackChannel.NotifyError(id, msg);
        }
    }

    private static IProcessCallback GetCallback()
    {
        return OperationContext.Current.GetCallbackChannel<IProcessCallback>();
    }
}
如果启用了远程调试,则此代码在开发计算机上和测试服务器上都可以正常工作。

我在web.config(WCF项目和ASP.NET项目)中启用了WCF跟踪,并使用工具“SvcTraceViewer.exe”检查日志文件。我发现ASP.NET项目在尝试处理回调时出错:

标记为异常的活动的XML信息(在.svclog文件上)


131075
3.
0
2.
服务器WS2008R2
http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx
正在引发异常。
/LM/W3SVC/1/ROOT/MyApplication.Web-1-130815389967873315
System.Runtime.FatalException,System.Runtime.DurableInstance,版本=4.0.0.0,区域性=neutral,PublicKeyToken=31bf3856ad364e35
对象引用未设置为对象的实例。
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&;rpc)
位于System.ServiceModel.Dispatcher.MessageRpc.Process(布尔isOperationContextSet)
位于System.ServiceModel.Dispatcher.ChannelHandler.DispatchedReleasePump(RequestContext请求、布尔cleanThread、OperationContext currentOperationContext)
位于System.ServiceModel.Dispatcher.ChannelHandler.HandlerRequest(RequestContext请求,OperationContext currentOperationContext)
位于System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult结果)
在System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)中
在System.ServiceModel.Diagnostics.Traceutibility.c__DisplayClass4.CallbackGeneratorb__2(异步回调,IAsyncResult结果)
在System.Runtime.AsyncResult.Complete(布尔同步完成)
在System.Runtime.InputQueue`1.AsyncQueueReader.Set(项)上
在System.Runtime.InputQueue`1.Dispatch()中
位于System.ServiceModel.Channels.ReliableDuplexSessionChannel.ProcessDuplexMessage(WsrmMessageInfo信息)
位于System.ServiceModel.Channels.ReliableDuplexSessionChannel.HandlerReceiveComplete(IAsyncResult结果)
位于System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnReceiveCompletedStatic(IAsyncResult结果)
在System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)中
在System.Runtime.AsyncResult.Complete(布尔同步完成)
在System.ServiceModel.Channels.ReliableChannelBinder`1.InputAsyncResult`1.OnInputComplete(IAsyncResult结果)
在System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)中
在System.ServiceModel.Diagnostics.Traceutibility.c__DisplayClass4.CallbackGeneratorb__2(异步回调,IAsyncResult结果)
在System.Runtime.AsyncResult.Complete(布尔同步完成)
在System.Runtime.InputQueue`1.AsyncQueueReader.Set(项)上
在System.Runtime.InputQueue`1.Dispatch()中
在System.Runtime.ActionItem.DefaultActionItem.Invoke()中
在System.Runtime.ActionItem.CallbackHelper.InvokeWithoutContext(对象状态)中
在System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32错误,UInt32字节读取,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
System.Runtime.FatalException:对象引用未设置为对象的实例。--System.NullReferenceException:对象引用未设置为对象的实例。
在System.Web.HttpApplication.ThreadContext.Enter(布尔setImpersonationContext)
位于System.Web.HttpApplication.OnThreadEnterPrivate(布尔setImpersonationContext)
位于System.Web.AspNetSynchronizationContext.CallbackPossiblyUnderlock(SendOrPostCallback回调,对象状态)
位于System.Web.AspNetSynchronizationContext.CallCallCallback(SendOrPostCallback回调,对象状态)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&;rpc)
---内部异常堆栈跟踪的结束---
System.NullReferenceException,mscorlib,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
对象引用未设置为对象的实例。
在System.Web.HttpApplication.ThreadContext.Enter(布尔setImpersonationContext)
位于System.Web.HttpApplication.OnThreadEnterPrivate(布尔setImpersonationContext)
位于System.Web.AspNetSynchronizationContext.CallbackPossiblyUnderlock(SendOrPostCallback回调,对象状态)
位于System.Web.AspNetSynchronizationContext.CallCallCallback(SendOrPostCallback回调,对象状态)
位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&;rpc)
System.NullReferenceException:对象引用未设置为对象的实例。
在System.Web.HttpApplication.ThreadContext.Enter(布尔setImpersonationContext)
位于System.Web.HttpApplication.OnThreadEnterPrivate(布尔setImpersonationContext)
在系统中。
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false, AutomaticSessionShutdown = false)]
public partial class LoadPage : System.Web.UI.Page, IProcessCallback
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnProcess_OnClick(object sender, EventArgs e)
    {
        try
        {
            var instanceContext = new InstanceContext(this);
            var proxyDuplex = new ProcessorClient(instanceContext);

            proxyDuplex.Process(new ProcessRequest());
        }
        catch (Exception ex)
        {
            //Some exception handling code here...
        }
    }

    public void NotifyOk(string id, string msg)
    {
        //Some logic...
    }

    public void NotifyError(string id, string msg)
    {
        //Some logic...
    }
}
<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131075</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2015-07-16T16:51:44.9708260Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{06c345d5-3374-4692-98c6-0256c236f70d}" />
<Execution ProcessName="w3wp" ProcessID="4432" ThreadID="39" />
<Channel />
<Computer>SERVERWS2008R2</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier>
<Description>Throwing an exception.</Description>
<AppDomain>/LM/W3SVC/1/ROOT/MyApplication.Web-1-130815389967873315</AppDomain>
<Exception>
<ExceptionType>System.Runtime.FatalException, System.Runtime.DurableInstancing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</ExceptionType>
<Message>Object reference not set to an instance of an object.</Message>
<StackTrace>
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; 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.ServiceModel.Diagnostics.TraceUtility.&lt;&gt;c__DisplayClass4.&lt;CallbackGenerator&gt;b__2(AsyncCallback callback, IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.InputQueue`1.AsyncQueueReader.Set(Item item)
at System.Runtime.InputQueue`1.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)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.ServiceModel.Channels.ReliableChannelBinder`1.InputAsyncResult`1.OnInputComplete(IAsyncResult result)
at System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.ServiceModel.Diagnostics.TraceUtility.&lt;&gt;c__DisplayClass4.&lt;CallbackGenerator&gt;b__2(AsyncCallback callback, IAsyncResult result)
at System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)
at System.Runtime.InputQueue`1.AsyncQueueReader.Set(Item item)
at System.Runtime.InputQueue`1.Dispatch()
at System.Runtime.ActionItem.DefaultActionItem.Invoke()
at System.Runtime.ActionItem.CallbackHelper.InvokeWithoutContext(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.Runtime.FatalException: Object reference not set to an instance of an object. ---&gt; System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext)
   at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
   at System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
   at System.Web.AspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
   --- End of inner exception stack trace ---</ExceptionString>
<InnerException>
<ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Object reference not set to an instance of an object.</Message>
<StackTrace>
at System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext)
at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
at System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
at System.Web.AspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)
</StackTrace>
<ExceptionString>System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Web.HttpApplication.ThreadContext.Enter(Boolean setImpersonationContext)
   at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext)
   at System.Web.AspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state)
   at System.Web.AspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc)</ExceptionString>
</InnerException>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
var instanceContext = new InstanceContext(this);
var proxyDuplex = new ProcessorClient(instanceContext);
var instanceContext = new InstanceContext(new LoadPage());
var proxyDuplex = new ProcessorClient(instanceContext);