【问题标题】:strange exception while consuming WCF Service (possibly character encoding exception?)使用 WCF 服务时出现奇怪的异常(可能是字符编码异常?)
【发布时间】:2012-07-03 15:54:07
【问题描述】:

我正在调用我之前创建的 WCF 服务,该服务查询语义数据库并返回 dbpedia.org 链接,这些链接带有实体的名称,因此例如您会找到“http://dbpedia.org /resource/Barack_Obama”或“http://dbpedia.org/resource/Brazil”

我面临的问题是,在调用返回提到的 URI 的方法时,该方法大部分时间都有效,但很少发生此错误:

底层连接已关闭:预期保持活动状态的连接已被服务器关闭。

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.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 ComparisonServiceInterface.Compare(String[] URIs)
   at ComparisonServiceInterfaceClient.Compare(String[] URIs)

Inner Exception:
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

Inner Exception:
An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

我想也许有些资源有不同的编码,比如“ü”字符的“http://dbpedia.org/resource/Sergio_Agüero”或其他什么?

我在发回之前尝试对 uri 进行编码,它仍然抛出相同的异常。

服务配置文件如下所示:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>  
  <system.serviceModel>
    <services>
      <service name="mergedServices.MergedService">
        <endpoint address="" binding="wsHttpBinding" contract="mergedServices.RelationGeneratorServiceInterface">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="wsHttpBinding" contract="mergedServices.keywordSearchServiceInterface">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="wsHttpBinding" contract="mergedServices.QAServiceInterface">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="wsHttpBinding" contract="mergedServices.CompareWithOnePredicateServiceInterface">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="wsHttpBinding" contract="mergedServices.ComparisonServiceInterface">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="wsHttpBinding" contract="mergedServices.ProfileConstructorInterface">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/mergedServices/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

【问题讨论】:

  • 每次请求后你都关闭chanel吗?
  • 不清楚问题是您在使用特殊字符 ("dbpedia.org/resource/Sergio_Agüero") 时总是会收到此错误,还是有时会收到此错误,以及猜测可能是特殊字符。

标签: .net wcf http character-encoding windows-services


【解决方案1】:

在故障中临时包含异常详细信息,这将允许您获得真正的错误。此外,您应该在服务中重新抛出异常之前记录它们,这样您就知道真正的问题是什么,而无需在故障中包含异常详细信息。

<serviceDebug includeExceptionDetailInFaults="True"/>

【讨论】:

  • 服务器端没有异常,传输中出现异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 2010-12-11
  • 2011-04-07
  • 1970-01-01
相关资源
最近更新 更多