C# 使用ClientBase的WCF<;T>;第一次访问时速度慢
我有一个WCF客户端/服务器,客户端代理正在使用C# 使用ClientBase的WCF<;T>;第一次访问时速度慢,c#,wcf,C#,Wcf,我有一个WCF客户端/服务器,客户端代理正在使用ClientBase。当访问客户端代理时,对任何服务的第一次调用需要约8秒钟。后续呼叫(在15秒内超时)需要约0.5秒。我发现了以下文章: 首先有一条评论建议这在.NET4.5中是固定的(我使用的是4.5.1,但我仍然面临这个问题)。我已尝试将建议的解决方法落实到位,但仍然存在此问题 我的客户端代理代码: public class MyClient : ClientBase<IMyBrowser>, IMyBrowser {
ClientBase
。当访问客户端代理时,对任何服务的第一次调用需要约8秒钟。后续呼叫(在15秒内超时)需要约0.5秒。我发现了以下文章:
首先有一条评论建议这在.NET4.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;
}
}
公共类MyClient:ClientBase,IMyBrowser
{
私有MyClient(绑定绑定,EndpointAddress remoteAddress):基(绑定,remoteAddress){}
公共字符串GetCustomerCommissionGroup(字符串customerId)
{
字符串_result=null;
尝试
{
_结果=Channel.GetCustomerCommissionGroup(customerId);
}
捕获(故障异常_故障)
{
//做点什么
}
返回结果;
}
}
客户端配置:
<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>
本以为这就是解决办法,但遗憾的是,长期的创业期仍然存在
编辑2:
我决定修改我的解决方案,在WCF上使用WebAPI。我目前在启动时仍有延迟(~2秒),但与WCF相比,我的日志记录更好,控制更简单。我发现一篇文章提到NetBios是一个潜在原因。我禁用了此服务,并在我的托管服务器上查找WINS和LMHosts,超时时间从~8秒减少到~3.5秒。胜利