WCF 异步调用 - 事件处理程序中的异常

Posted

技术标签:

【中文标题】WCF 异步调用 - 事件处理程序中的异常【英文标题】:WCF Asynchronous Call - Exception at eventhandler 【发布时间】:2011-12-02 16:39:07 【问题描述】:

WCF 异步调用 - 事件处理程序异常

我正在使用事件处理程序对 WCF 方法进行异步调用。我在“EventAddCallback”事件中收到错误,“e.Error”显示以下错误。有谁知道为什么?我添加了示例代码、错误信息、跟踪信息和我尝试过的选项..

System.Reflection.TargetInvocationException:操作过程中发生异常,导致结果无效。检查 InnerException 以获取异常详细信息。 ---> System.ServiceModel.CommunicationException:发生错误 在接收到https://demosite.com/ourservice.asmx 的 HTTP 响应时。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于 HTTP 请求上下文被 服务器(可能是由于服务关闭)。有关更多详细信息,请参阅服务器日志。 ---> System.Net.WebException:底层连接已关闭:接收时发生意外错误。 ---> System.IO.IOException: 无法从传输连接读取数据:现有连接被远程主机强行关闭。 ---> System.Net.Sockets.SocketException: 现有连接被远程主机强行关闭

我启用了跟踪,它显示了..

System.ServiceModel.CommunicationException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
An error occurred while receiving the HTTP response to https://demosite.com/ourservice.asmx. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.
-->System.Net.WebException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
-->The underlying connection was closed: An unexpected error occurred on a receive.
------>System.IO.IOException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
------>Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
---------->System.Net.Sockets.SocketException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
---------->An existing connection was forcibly closed by the remote host


Options I tried..
1. Increased 
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
              maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>

2. Enabled 'Keep Alive', increased buffer size(s)
<httpsTransport maxReceivedMessageSize="2147483647" keepAliveEnabled="true" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" />              

3. added endpointBehaviors

      <endpointBehaviors>
        <behavior name="demo">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>

    class Program
    
        static ManualResetEvent closeapp = new ManualResetEvent(false);
        static void Main(string[] args)
        
            wcfclient.AddCompleted += new EventHandler<AddCompletedEventArgs>(EventAddCallback);
            wcfclient.AddAsync(employees);
            closeapp.WaitOne(); 
        
        static void EventAddCallback(object sender, AddCompletedEventArgs e)
        
            try
            
                if (e.Error != null)
                
                    wcfclient.Close();
                    closeapp.Set(); 
                else
                
                   //Continue with other calls.
                
            
            catch (Exception ex) 
                throw ex;
            
        
    

【问题讨论】:

【参考方案1】:

您的绑定安全性错误。您必须切换到 http(由于服务器设置,您可能无法切换),或者您应该切换到客户端绑定配置中的传输安全性。

【讨论】:

以上是关于WCF 异步调用 - 事件处理程序中的异常的主要内容,如果未能解决你的问题,请参考以下文章

javascript中事件处理程序的异步或同步调用

中间层异步 WCF 服务调用和向 UI 层返回数据的质量设计

捕获当前线程中的所有异常

如何异步处理 WCF 的传入调用?

.NET 中的异步 WMI 事件处理

20165232 第七周学习总结