c# - 如何调试此 WCF Web 服务? ("ServiceChannel is in the Faulted state")

标签 c# .net wcf web-services

我在 Web 服务中遇到一个奇怪的错误,导致 Web 服务变得不可用。它将返回一个错误

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

当我刷新应用程序池时,错误消失了。我已经添加了一些调试日志记录(使用 Log4net),但它没有显示任何有用的信息。

事件日志中也没有任何内容。

我如何成功调试这个网络服务(它已经工作了 2 年多,突然开始返回这些错误)?

这是在 C#/.NET 3.5 中。

更新:网络服务只是从 sql server 中提取数据(客户数据),将其包装在数据集中并将其发送回客户端。

*更新 2:* 我想我发现了错误:客户端没有关闭连接。这可信吗?

最佳答案

您可以使用框架提供的 System.ServiceModel.MessageLogging 功能添加服务日志记录。它将记录所有内部 WCF 异常,这些异常通常发生在比服务协定更高的级别,因此您可能永远不会通过标准调试看到它。

记录错误后,可以使用 Service Trace Viewer Tool 手动检查和分析生成的 .svclog

此功能帮助我解决了无法根据客户端或服务器可用的异常信息确定根本原因的 WCF 问题。例如,我有一个凭据问题,它只是在客户端上产生了一条类似于“连接已被远程主机关闭”的异常消息,而在服务器上根本没有异常。浏览服务器上的日志不到 3 分钟就指出了确切的问题。

将以下内容添加到您的 app.config/web.config(根据您的需要进行修改)。这仅通过配置启用,即无需编程即可进行设置。

<configuration>  
 <system.diagnostics>  
  <sources>  
    <source name="System.ServiceModel"  
            switchValue="Information, ActivityTracing"  
            propagateActivity="true" >  
      <listeners>  
        <add name="xml"/>  
      </listeners>  
    </source>  
    <source name="System.ServiceModel.MessageLogging">  
      <listeners>  
        <add name="xml"/>  
      </listeners>  
    </source>  
    <source name="myUserTraceSource"  
            switchValue="Information, ActivityTracing">  
      <listeners>  
        <add name="xml"/>  
      </listeners>  
    </source>  
  </sources>  
  <sharedListeners>  
    <add name="xml"  
         type="System.Diagnostics.XmlWriterTraceListener"  
               initializeData="C:\logs\Traces.svclog" />  
  </sharedListeners>  
 </system.diagnostics>  

 <system.serviceModel>  
  <diagnostics wmiProviderEnabled="true">  
      <messageLogging   
           logEntireMessage="true"   
           logMalformedMessages="true"  
           logMessagesAtServiceLevel="true"   
           logMessagesAtTransportLevel="true"  
           maxMessagesToLog="3000"   
       />  
  </diagnostics>  
 </system.serviceModel>  
</configuration> 

More information found here

关于c# - 如何调试此 WCF Web 服务? ("ServiceChannel is in the Faulted state"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7299116/

相关文章:

c# - 多个选项的更有效的 'if' 语句

c# - 使用 C# 包装器时出现 Matlab 异常

wcf - Azure辅助角色中的外部HTTP端点可以吗?

wcf - 将相关 token 传递给 WCF 服务?

c# - CURL 到 WCF 的更好方法

c# - 有没有一种简单的方法来获取C#(或CIL)中当前堆栈帧中的所有局部变量

c# - 为什么 NamespaceManager 在 XPath 中不使用前缀时不使用 DefaultNamespace

.net - 将 MbUnit 与 Delphi Prism 结合使用

c# - .NET TransactionScopeOption.Suppress 的 T-SQL 等价物

c# - 如何仅垂直移动无边框表格?