增加 WCF 服务中的超时值

如何将 WCF 服务的默认超时时间增加到大于1分钟?

204611 次浏览

您指的是服务器端还是客户端?

对于客户端,需要调整绑定元素的 SendTimeout属性。对于服务,需要调整绑定元素的 超时属性。

<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="longTimeoutBinding"
receiveTimeout="00:10:00" sendTimeout="00:10:00">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>


<services>
<service name="longTimeoutService"
behaviorConfiguration="longTimeoutBehavior">
<endpoint address="net.tcp://localhost/longtimeout/"
binding="netTcpBinding" bindingConfiguration="longTimeoutBinding" />
</service>
....

当然,您必须将所需的端点映射到该特定绑定。

在 VisualStudio2008的工具菜单(或者2005,如果您安装了正确的 WCF 内容)下面有一个名为“ WCF 服务配置编辑器”的选项。

从那里您可以更改客户端和服务的绑定选项,其中一个选项将用于超时。

不同的超时意味着不同的事情。当你为客户工作的时候。.你可能大部分时间都在看 SendTimeout-检查一下这个参考文献-精彩而相关的解释: Http://social.msdn.microsoft.com/forums/en-us/wcf/thread/84551e45-19a2-4d0d-bcc0-516a4041943d/

上面写着:

Brief summary of binding timeout knobs...


Client side:


SendTimeout is used to initialize the OperationTimeout, which governs the whole interaction for sending a message (including receiving a reply message in a request-reply case).  This timeout also applies when sending reply messages from a CallbackContract method.
OpenTimeout and CloseTimeout are used when opening and closing channels (when no explicit timeout value is passed).
ReceiveTimeout is not used.


Server side:


Send, Open, and Close Timeout same as on client (for Callbacks).
ReceiveTimeout is used by ServiceFramework layer to initialize the session-idle timeout.

你可以选择两种方式:

1)通过客户端中的代码

public static void Main()
{
Uri baseAddress = new Uri("http://localhost/MyServer/MyService");


try
{
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService));


WSHttpBinding binding = new WSHttpBinding();
binding.OpenTimeout = new TimeSpan(0, 10, 0);
binding.CloseTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);


serviceHost.AddServiceEndpoint("ICalculator", binding, baseAddress);
serviceHost.Open();


// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();


}
catch (CommunicationException ex)
{
// Handle exception ...
}
}

2)在网络服务器中使用 WebConfig

<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding openTimeout="00:10:00"
closeTimeout="00:10:00"
sendTimeout="00:10:00"
receiveTimeout="00:10:00">
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>

有关详细信息,请查看官方文件

在绑定上配置超时值

类 WSHttpBinding

除了绑定超时(在 Timespans 中) ,您可能还需要这个。就在几秒钟之内。

<system.web>
<httpRuntime executionTimeout="600"/><!-- = 10 minutes -->