【问题标题】:WCF host 15 seconds delayWCF 主机延迟 15 秒
【发布时间】:2011-12-28 13:58:58
【问题描述】:

我正在使用 .NET 3.5 和 WCF 来开发服务器-客户端应用程序。绑定=基本的Http。 我正在 Windows 2003 server sp2 中工作和部署该服务。

服务器中的服务由控制台应用程序自托管,在我的计算机中一切正常。问题是,当我在它应该运行的计算机中部署服务器时,打开 serviceHost 实例需要 15 秒,而它应该是毫秒。我可以忍受这一点,但当这个实例收到来自客户端的第一个请求时,它需要 15 秒才能响应,对于每个新客户端都是如此。在第一次请求之后,只需几毫秒即可响应以下内容。

我的电脑没有这个问题,我已经尝试过很多其他的,它也可以正常工作。我无法格式化我正在部署的服务器,因此我需要一些关于该特定计算机或配置中可能出现的问题的建议。 我想在该机器上托管的任何服务都会重复此行为,甚至是“WCF 服务库”模板中的基本示例,所以为了简单起见,我正在处理它解决这个问题。 这是我在主机应用程序中使用的 app.config。其余代码正是上面提到的模板之一。 请记住,服务运行良好,延迟会导致服务无法使用。

提前致谢!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary7.Service1" behaviorConfiguration="WcfServiceLibrary7.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary7/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary7.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary7.Service1Behavior">
          <!-- 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>

</configuration>

客户端 App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        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>
        </bindings>
        <client>
            <endpoint address="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary7/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

【问题讨论】:

  • 当您进入 15 秒等待的 7 秒时,暂停正在运行的进程并查看进程调用堆栈在哪里等待。由于这是在不是您的开发机器的 PC 上运行(刚刚看到),您可以改为使用任务管理器进行进程转储,然后在您的 PC 上打开生成的 dmp 文件以查看进程在做什么
  • Drats,刚刚看到您在 net3.5 上,这意味着您无法在 Visual Studio 中查看 dmp 文件。尽管如此,您可以使用 windbg 检查调用堆栈或在.net4 中一次性运行它以进行调试
  • 我正在使用服务器中的诊断和进程堆栈来交易活动。如果我在另一台计算机上安装该服务,它运行得很好。

标签: .net wcf web-services


【解决方案1】:

我会首先在客户端使用 fiddler 来查看发生了什么。如果需要,如果存在非 http 较低级别的网络问题,您也可以使用 netmon。

在服务器端,您可以分析、跟踪或记录,以查看在这 15 秒的请求期间,服务器代码花费了多少时间。

这些至少会告诉你从哪里开始。

如果是针对每个客户端的新请求,您应该查看身份验证、DNS 名称解析和其他网络配置。另一个很好的实验是在对用户发出 15 秒的请求后,回收服务器应用程序并再次发出请求。又是 15 秒?如果不是,则可能是网络,如果是,则 app/config 中的某些内容。

【讨论】:

  • 我认为这与网络有关,因为 app.config 中的任何配置都是如此。最大的问题是什么?
【解决方案2】:

第一次加载 ServiceHost 总是需要很长时间。一些原因:

  • 加载程序集
  • 打开端口
  • JITting 代码
  • 做各种反射操作

因此,如果您的机器规格不是很好,这可能会更长。我认为与您的操作方式无关。


更新

查看客户端配置后,似乎使用clientCredentialType="Windows" 的安全性消息将调用可能超时的域控制器。

【讨论】:

  • 是的,但这应该是一次性的问题——如果它发生在每个服务请求上,那么很可能是其他问题。
  • 每一个连接到服务的新客户端都会发生这种情况,并且只发生在该特定计算机的第一次调用中。在另一台计算机上,该服务运行良好。这就是杀死我的原因。
  • 那么每个新客户?当新客户端连接并且其他一些客户端连接时也是如此吗?它的空闲时间设置是多少?
  • 时间总是正好 15 秒,在客户端和服务器中创建新的服务主机时。我确定这是某种超时,但除了 WCF 中的诊断之外,我不知道如何跟踪它。
  • 不,我的意思是把客户端绑定配置也放在那里。
【解决方案3】:

预感 - 仅此而已 - 在某处解析某个网络名称时存在 DNS 超时。有没有

ping localhost

还给你十五秒的延迟?

同样,也许有什么东西正试图对传入的主机名进行反向 DNS 查找。您的远程主机是否具有可由服务器解析的名称?

当您在端口 8732 上打开连接时,您可能还想检查是否有防火墙或病毒检查程序以某种奇怪的方式拦截 HTTP 请求。

【讨论】:

  • 我 ping localhost,它在不到 1 毫秒的时间内响应。我现在会检查杀毒软件
  • 杀毒软件关闭,防火墙关闭,还有什么?
  • 我确实说过这只是一种预感!
【解决方案4】:

当我发现它没有安装 .NET 3.5 的 Service Pack 1 时,问题就解决了。在电脑上我遇到了问题。安装后,一切都开始正常运行,没有延迟。

感谢大家的建议! 最好的祝福 纳乔

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 2015-07-12
    • 2012-08-03
    相关资源
    最近更新 更多