wcf - 无法理解 WCF 错误消息,需要帮助

标签 wcf exception

我有一个简单的 Web 服务来允许应用程序查询我的 CMDB。我遇到问题的函数在一个小的结果集上工作但在一个更大的结果集上失败,这表明它是 WCF 服务配置中的某些东西阻止它成功。

我有一个简单的 WinForms 测试应用程序,其中包含对 Web 服务的服务引用和一个调用相关函数的函数。

较小的结果集返回 ~120KB 的 xml,失败的较大结果集是 ~2MB。我曾尝试增加 maxReceivedMessageSize 和 maxStringContentLength 的大小,但没有成功。

有没有我错过的配置?如果这是问题所在,我会期待更详细的错误消息。

提前致谢,

缺口

返回的错误是:

System.ServiceModel.CommunicationException: The underlying connection was closed: The connection was closed unexpectedly. --->
System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at TestRig.CmdbDataService.ICmdbDataService.GetMonitors(String client)
   at TestRig.CmdbDataService.CmdbDataServiceClient.GetMonitors(String client) in C:\Documents and Settings\nfoster\My Documents\Visual Studio Projects\Virtual Operations Manuals\Trunk\src\TestRig\Service References\CmdbDataService\Reference.vb:line 1480
   at TestRig.Form1.btnGetServers_Click(Object sender, EventArgs e) in C:\Apps\Virtual Operations Manuals\Trunk\src\TestRig\Form1.vb:line 8

应用程序中的调用函数是:
Private Sub btnGetMonitors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetMonitors.Click
  txtResults.Text = String.Empty
  Dim proxy As CmdbDataService.CmdbDataServiceClient = Nothing
  Try
    proxy = New CmdbDataService.CmdbDataServiceClient("WSHttpBinding_ICmdbDataService")
    Dim monitors As TestRig.CmdbDataService.ConfigurationItems = proxy.GetMonitors(txtClientName.Text)
    proxy.Close()
    For Each monitor In monitors
      txtResults.Text &= monitor.Name & " (" & monitor.TypeName & ")" & vbCrLf
    Next
    txtResults.Text &= monitors.Count & " monitors returned"
  Catch ex As Exception
    If Not IsNothing(proxy) AndAlso proxy.State <> ServiceModel.CommunicationState.Closed Then proxy.Abort()
    txtResults.Text = ex.ToString
  Finally
    proxy = Nothing
  End Try
End Sub

在测试设备端,app.config 包含以下 serviceModel:
  <system.serviceModel>
    <diagnostics>
      <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true" />
    </diagnostics>
    <behaviors />
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_Default" closeTimeout="00:05:00"
          openTimeout="00:05:00" receiveTimeout="00:10:00" sendTimeout="00:05:00"
          maxBufferPoolSize="104857600" maxReceivedMessageSize="104857600">
          <readerQuotas maxDepth="104857600" maxStringContentLength="104857600"
            maxArrayLength="104857600" maxBytesPerRead="104857600" maxNameTableCharCount="104857600" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/CmdbServices/DataService.svc/soap12"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_Default"
        contract="CmdbDataService.ICmdbDataService" name="WSHttpBinding_ICmdbDataService">
        <identity>
          <userPrincipalName value="MyMachine\ASPNET" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

在服务端,web.config 是:
  <system.serviceModel>
    <diagnostics performanceCounters="Default">
      <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
       logMessagesAtTransportLevel="true" />
    </diagnostics>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="MyCorp.Cmdb.Services.DataService.CmdbDataService">
        <endpoint address="soap12" binding="wsHttpBinding" contract="MyCorp.Cmdb.Services.DataService.ICmdbDataService" />
        <endpoint address="soap11" binding="basicHttpBinding" contract="MyCorp.Cmdb.Services.DataService.ICmdbDataService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

最佳答案

我的一个同事刚刚给我指了一个 this blog post真正的罪魁祸首是端点行为中的 maxItemsInObjectGraph 属性。

增加这些已经解决了问题,我一定刚刚超过了 65536 的默认阈值:D

很高兴看到错误消息为您指明了正确的方向:(

干杯....

另外:您可能会收到此错误,因为您的 Web 方法之一正在使用不是 [DataContract] 的类。类(class)。

关于wcf - 无法理解 WCF 错误消息,需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/714488/

相关文章:

c# - 始终在 WCF 服务中使用 Json 返回 XML 字符串

php - 执行自定义错误处理程序后执行默认异常处理程序

C++:如何处理从 std:exception 派生的异常,具体取决于它们的类型?

c++ - 在共享库中增加异常

.net - 如何查明我的 WCF 服务器何时退出(并关闭它的 TCP 连接)

wcf - 防止在 WCF 中的用户之间共享登录凭据

.net - 如何在 WCF 中为 HTTP 连接设置保持事件间隔

java - 此 WSDL 是否缺少方法?

java - 为什么在 .NET 中不检查异常?

c# - 捕获异步方法中的异常