增加WCF服务中的超时值


133

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


目前尚不清楚,但是我想您是在隐式询问的是,是否可以在服务器端进行配置以使处理时间超过一分钟的所有呼叫超时。这是不可能的
gravidThoughts

Answers:


196

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

对于客户端,您需要调整绑定元素的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>
....

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


如何在端点标记内使用“ bindingname”来映射绑定?
Blankman

这是完全错误receiveTimeout。服务器端控制基于会话的绑定的空闲状态确定。例如,服务器不会将此设置用于基本HTTP绑定。您必须为WCF滚动自己的服务器端处理超时
gravidThoughts,

45

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

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


该工具是避免出错的好方法,例如,错误地将元素包装起来,拼写错误等。
markaaronky

另请参阅此处以了解其他打开日志文件的工具:stackoverflow.com/a/34283667/187650
juFo


8

您可以选择两种方式:

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>

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

在绑定上配置超时值

WSHttpBinding类


0

除了绑定超时(以Timespan秒为单位)之外,您可能还需要这样做。以秒为单位。

<system.web>
    <httpRuntime executionTimeout="600"/><!-- = 10 minutes -->
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.