【问题标题】:WCF Service with multiple threads Issue具有多个线程的 WCF 服务问题
【发布时间】:2016-06-23 04:20:16
【问题描述】:

我有一个托管在 IIS 6.0/win 2003 机器下的 wcf 服务。服务有这样的方法。

    List<CustomObject> ProcessImageData(List<CustomObject> lstData)
    {
          lstSharedProcessedImages = lstData;

         for (int idx = 0; idx < lstData.Count; )
         {
            CurrentThreadRoom = MAX_THREAD_COUNT - ThreadRunningCount;
            for (int ctr = 0; ctr < CurrentThreadRoom; ctr++)
            {
                runBrowserThread(lstData[idx].Url, lstData[idx].Path);
                idx++;
                if (lstData.Count == idx)
                    break;
            }
            while (ThreadRunningCount >= MAX_THREAD_COUNT)
            {
                Thread.Sleep(SleepTimeMiliseconds);
            }
        }
        while (ThreadRunningCount > 0)
        {
            Thread.Sleep(SleepTimeMiliseconds);
        }
        return lstSharedProcessedImages; 
    }

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>

客户端的 app.config 文件

    <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>

只有一个客户端在循环中调用 wcf 服务的方法并等待一分钟,以便每次迭代接收结果。 wcf 服务产生 10 个线程(全部独立且不相互依赖)来完成每个请求。这些线程中的每一个都创建一个 WebBrowser 实例来完成其工作。处理请求并将响应发送回客户端通常需要 6-7 秒。大约 80% 的调用是成功的。

现在的问题是,

  1. 在 Web 服务器上,当客户端调用服务方法时,我们看到线程数出现了巨大的峰值(从 13k 增加到 18k 左右)。该服务应该创建 10 个线程/请求,并且没有特定的代码可以在完成后终止线程。假设一旦给定的工作完成,线程就会自行死亡(也释放内存等)。

  2. 由于某种原因,大量线程保留在内存中,导致服务器无响应。也不知道为什么#of 线程会出现如此巨大的峰值。

  3. 然后对服务器的所有后续调用都会失败。客户端不断调用新请求并继续出现以下异常。

"这也可能是由于服务器中止了 HTTP 请求上下文(可能是由于服务关闭)。", "请求通道在 00:00:59.9749975 之后等待回复时超时。"等等等等……

当内存被回收并且线程数再次下降到 13k 或其他东西时,服务再次恢复正常,处理请求,线程数激增,新的异常和恶性循环继续。

这里出了什么问题??假设内存中的#of 线程是罪魁祸首,我该如何强制清理服务器?线程清理似乎无论如何都在发生,但为时已晚,因为那时有太多异常。

你们有什么建议?非常感谢任何输入!!!

【问题讨论】:

  • 您向我们展示了一个伪代码签名,甚至不是真正的签名,但我们需要的是实际代码。
  • 对不起。代码现在可供审查。
  • 谢谢,ProcessImageData 的代码在哪里?
  • 完成。请见上文。
  • 那是你的实际代码吗?还是它“像”你的实际代码?

标签: c# multithreading performance wcf


【解决方案1】:

很难说,因为您没有共享任何代码或配置。人们可能会怀疑所有的胎面都没有完成,因此线程的数量增加了例如每次调用 10 个,如果没有完成。

您可以使用分析器或 PerfMon 来监控线程数,或向您的服务添加跟踪调用和跟踪侦听器以了解更多信息。

【讨论】:

  • 对不起。代码可供审查。
猜你喜欢
  • 1970-01-01
  • 2016-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多