首次访问时使用ClientBase <t>的WCF速度很慢

时间:2016-03-10 17:01:02

标签: c# wcf

我有一个WCF客户端/服务器,客户端代理正在使用ClientBase<T>。访问客户端代理时,对任何服务的第一次调用大约需要8秒。后续调用(在15秒内超时)需要约0.5秒。我找到了以下文章:

http://blogs.msdn.com/b/wenlong/archive/2010/02/11/why-does-wcf-become-slow-after-being-idle-for-15-seconds.aspx

首先有一条评论建议在.net 4.5中修复此问题(我使用4.5.1但我仍然面临这个问题)。我已经尝试了建议的解决方法,但仍然有这个问题。

我的客户代理代码:

public class MyClient : ClientBase<IMyBrowser>, IMyBrowser
{
    private MyClient(Binding binding, EndpointAddress remoteAddress) : base(binding, remoteAddress) { }

    public string GetCustomerCommissionGroup(string customerId)
    {
        string _result = null;
        try
        {
            _result = Channel.GetCustomerCommissionGroup(customerId);
        }
        catch (FaultException<MyFault> _fault)
        {
          // do something
        }
        return _result;
    }
}

客户端配置:

<system.serviceModel>

<bindings>
  <netTcpBinding>
    <binding name="netTcpNone">
      <security mode="None"/>
    </binding>
  </netTcpBinding>
</bindings>

<services>

  <service name="MyService" behaviorConfiguration="StandardBehaviour">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpNone" contract="MyBrowser">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:23456/MyService.svc"/>
      </baseAddresses>
    </host>
  </service>

</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="StandardBehaviour">
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceMetadata httpGetEnabled="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

</system.serviceModel>

我在Windows服务应用程序中托管这个,使用Net.Tcp并且没有安全措施。

编辑:

http://blogs.msdn.com/b/wenlong/archive/2007/10/26/best-practice-always-open-wcf-client-proxy-explicitly-when-it-is-shared.aspx

认为这是解决方案,但遗憾的是,长期启动仍然存在

EDIT2:

我决定修改我的解决方案,以便在WCF上使用WebAPI。我目前在启动时仍然存在延迟(约2秒)但是我的日志记录变得更好,而且与WCF相比实际上更简单。

1 个答案:

答案 0 :(得分:0)

我发现一篇文章称NetBios是潜在的原因。我在托管服务器上禁用了此服务以及WINS和LMHosts查找,超时从~8秒减少到~3.5。赢了!

http://www.codeproject.com/Tips/114132/WCF-First-Call-Slow

相关问题