增加WCF服务中的超时值

时间:2009-01-08 14:12:40

标签: wcf

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

5 个答案:

答案 0 :(得分:187)

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

对于客户端,您需要调整绑定元素的 sendTimeout 属性。对于服务,您需要调整绑定元素的 receiveTimeout 属性。

<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>
....

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

答案 1 :(得分:43)

在Visual Studio 2008中的“工具”菜单下(如果安装了正确的WCF,则为2005),有一个名为“WCF服务配置编辑器”的选项。

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

答案 2 :(得分:8)

不同的超时意味着不同的事情。当你在客户端上工作时......你可能主要关注SendTimeout - 请查看此参考资料 - 精彩且相关的解释: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/84551e45-19a2-4d0d-bcc0-516a4041943d/

答案 3 :(得分:5)

您可以选择两种方式:

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)通过Web服务器中的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>

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

Configuring Timeout Values on a Binding

Class WSHttpBinding

答案 4 :(得分:0)

除了绑定超时(在Timespan s中),您可能还需要它。这是在几秒钟内。

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