我有一个包含 WCF NetTcp 主机的 Windows 服务。从客户端开始,当我开始通过 tcp 调用服务时,它们一开始运行良好,但几分钟后它们都开始出现可怕的 WCF 超时错误,这实际上与超时无关:
发送到 net.tcp://myserver:8080/ListingService 的此请求操作在配置的超时 (00:01:00) 内未收到回复。
我从该站点上的其他帖子中看到,很多时候这与最大消息大小有关,但我已经将这些设置为无济于事。
这是我的 Windows 服务代码:
public partial class Service : ServiceBase
{
internal static ServiceHost myHost = null;
public Service()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
System.Net.ServicePointManager.DefaultConnectionLimit = 10000;
//create host.
var path = ConfigurationManager.AppSettings["ServiceHostAddress"].ToString();
myHost = new ServiceHost(typeof(ListingService));
//add endpoint.
myHost.AddServiceEndpoint(typeof(IListingService), GetBinding(), path);
//add behaviors.
AddBehaviors();
//open host.
myHost.Open();
}
private void AddBehaviors()
{
//service metadata behavior.
var smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
myHost.Description.Behaviors.Add(smb);
//service throttling behavior.
var behavior = new ServiceThrottlingBehavior()
{
MaxConcurrentCalls = 10000,
MaxConcurrentInstances = 10000,
MaxConcurrentSessions = 10000
};
myHost.Description.Behaviors.Add(behavior);
//service debug behavior.
var serviceDebugBehavior = myHost.Description.Behaviors.Find<ServiceDebugBehavior>();
serviceDebugBehavior.IncludeExceptionDetailInFaults = true;
}
private Binding GetBinding()
{
var queueBinding = new NetTcpBinding(SecurityMode.None);
queueBinding.MaxConnections = 10000;
queueBinding.MaxBufferSize = 2048000;
queueBinding.MaxReceivedMessageSize = 2048000;
return queueBinding;
}
protected override void OnStop()
{
if (myHost != null)
{
myHost.Close();
myHost = null;
}
}
}
这是客户端配置,以防有任何不同:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding" transferMode="Buffered" hostNameComparisonMode="StrongWildcard"
closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:01:00" sendTimeout="00:01:00"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"><!-- transactionFlow="true"-->
<security mode="None"/>
<reliableSession enabled="false"/>
<readerQuotas maxArrayLength="2147483647"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint
address="net.tcp://myserver:8080/ListingService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
contract="ListingServiceProxy.IListingService" name="NetTcpBinding" />
</client>
</system.serviceModel>
我确保关闭我的客户端连接,这是代码:
public static void Using<T>(this T client, Action<T> work)
where T : ICommunicationObject
{
try
{
work(client);
client.Close();
}
catch (CommunicationException)
{
client.Abort();
throw;
}
catch (TimeoutException)
{
client.Abort();
throw;
}
catch
{
client.Abort();
throw;
}
}
new ListingServiceClient().Using(client =>
{
client.SaveListing(listing);
});