【问题标题】:An error occurred while receiving the HTTP response to wcfservice.svc. This could be due to the service endpoint binding not using the HTTP protocol接收对 wcfservice.svc 的 HTTP 响应时出错。这可能是由于服务端点绑定未使用 HTTP 协议
【发布时间】:2016-03-09 20:38:00
【问题描述】:

我在 IIS 6.0 下部署了一个 wcf 服务。 service 方法接受对象列表并返回相同的列表。

    List<CustomObject> ProcessImageData(List<CustomObject> lstData)

该对象可能包含大量 html 数据。该服务目前几乎不需要几秒钟即可完成请求,但我收到了大量以下 2 个异常。

“请求通道在 00:00:59.9989999 之后等待回复时超时。增加传递给 Request 调用的超时值或增加 Binding 上的 SendTimeout 值。分配给此操作的时间可能是较长超时的一部分。”

“在接收到http://MyService.svc 的 HTTP 响应时发生错误。这可能是由于服务端点绑定未使用 HTTP 协议。这也可能是由于 HTTP 请求上下文被服务器中止(可能是由于到服务关闭)。有关详细信息,请参阅服务器日志。”

这是我的配置文件的样子。

web.Config(服务)文件

    <service behaviorConfiguration="WcfServices.Service1Behavior" name="WcfServices.HtmlToImageService">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="LargeSizeMessages" contract="WcfServices.IHtmlToPngService">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
   </service>

    <behaviors>
    <serviceBehaviors>
    <behavior name="WcfServices.Service1Behavior">
      <serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000"/>
      <serviceMetadata httpGetEnabled="true"/>
      <dataContractSerializer maxItemsInObjectGraph="6553500"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
   </behaviors>
    <wsHttpBinding>

       <binding name="LargeSizeMessages"
             maxBufferPoolSize="2147483647"
             maxReceivedMessageSize="2147483647">
      <readerQuotas
       maxDepth="32"
       maxStringContentLength="2147483647"
       maxArrayLength="2147483647"
       maxBytesPerRead="4096"
       maxNameTableCharCount="16384" />
    </binding>
  </wsHttpBinding>

客户端配置文件

    <wsHttpBinding>
      <binding name="WSHttpBinding_IHtmlToImageService" closeTimeout="00:10:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
      allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
          algorithmSuite="Default" />
      </security>
    </binding>
    </wsHttpBinding>

其中一个例外建议增加当前设置为 1 分钟的 sendTimeout vakue。我增加了它,但它似乎没有改善任何东西,我仍然得到这些例外。

问题: 为什么我会收到这些异常,我可以更改什么来防止这种情况发生?

另外,这是一个记录在 IIS 服务器上的事件日志中的异常。

异常类型:System.ArgumentException 消息:参数无效。 参数名称:NULL 数据:System.Collections.ListDictionaryInternal TargetSite:无效.ctor(Int32,Int32,System.Drawing.Imaging.PixelFormat) 帮助链接:NULL 来源:System.Drawing

堆栈跟踪信息


在 System.Drawing.Bitmap..ctor(Int32 宽度, Int32 高度, PixelFormat 格式) 在 System.Drawing.Bitmap..ctor(Int32 宽度,Int32 高度) 在 C:_tfs\ImagingSystem\Development\Dev\Source\Wcf\WcfServices\HtmlToPngService\HtmlToPngService.svc.cs:line 126 中的 WcfService.CreateBitmap(WebBrowser& browser) 处

【问题讨论】:

  • 看起来服务端发生了一些灾难性的异常,导致服务中断。检查 Windows 事件日志并查看是否记录了任何异常。
  • 另一件事 - List ProcessImageData(List lstData) - WCF 将所有列表/IEnumerables 转换为数组。所以这是一个等价的服务操作契约CustomObject[] ProcessImageData(CustomObject[] lstData)
  • 正确,但它对我所面临的任何事情有何影响?

标签: c# web-services wcf


【解决方案1】:

您还需要增加 web.config(服务)中的超时值。像这样的:

<bindings>
  <wsHttpBinding>
    <binding name="WSHttpBinding_IHtmlToImageService"
             openTimeout="00:10:00" 
             closeTimeout="00:10:00" 
             sendTimeout="00:10:00" 
             receiveTimeout="00:10:00">
    </binding>
  </wsHttpBinding>
</bindings> 

我建议查看以下链接:

https://msdn.microsoft.com/en-us/library/hh924831(v=vs.110).aspx

【讨论】:

    【解决方案2】:

    在这种情况下,可能会出现一些问题。首先,WCF 在其默认配置中被构建为非常安全。这就是为什么对传入请求的大小有限制的原因。该限制默认相当小(65536 字节)。如果您的请求大于此值,则需要配置 maxReceivedMessageSize 并将其设置为您希望支持的最大字节数。在服务器上用于传入请求,在客户端上用于传入响应。这可能是导致第二个错误的原因,您应该能够在服务的日志记录中找到对此的引用。

    其次,IIS 对传入请求的大小有自己的限制。此限制的默认值为 30000000 字节(大约 30MB)。如果您的请求比这更大,您还应该增加该限制(有关更多信息,请参阅here),因为 WCF 甚至不会看到传入的请求,因为 IIS 会在它到达之前阻止它。您描述的超时错误很可能发生在这种情况下,因为当 IIS 收到超过配置限制的请求时,它认为它受到攻击,甚至不会费心发送响应。请仔细考虑这些值并承担请注意 maxReceivedMessageSize 隐式配置缓冲区 WCF 将分配以放入接收到的消息。我在生产系统中看到此值设置为 Int32.MaxValue。结果,整个服务器很容易被几个巨大的并发请求关闭,因为服务器试图分配 2GB 的 RAM 来接收每个请求。

    【讨论】:

    • 谢谢马克。抱歉,我忘记粘贴已定义为 maxReceivedMessageSize="2147483647"> 的 LargeSizeMessages 配置设置。请参考 web.config(服务)文件。请你再看一遍。谢谢!
    • 您是否阅读过有关 IIS 请求过滤的部分?如果您的请求大小超过 30 MB,WCF 甚至不会看到它们,因为它们被 IIS 阻止。您可以在 web.config &lt;system.webServer&gt; &lt;security&gt; &lt;requestFiltering&gt; &lt;requestLimits maxAllowedContentLength="40000000" /&gt; &lt;/requestFiltering&gt; &lt;/security&gt; &lt;/system.webServer&gt; 中配置传入请求的最大大小
    【解决方案3】:

    你可以先分析异常。在 Visual Studio 中进行以下异常设置并启用“通用语言运行时异常”。

    大多数情况下,您可以使用它获取异常的详细信息。 我得到了这个异常,因为我的模型中的一个字段是强制性的,我得到 null 作为响应,并且由于这个在客户端我的对象无法正确反序列化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-17
      • 2013-10-08
      • 2021-09-29
      • 2011-07-15
      • 2014-06-03
      • 2018-03-22
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多