增加WCF服务中的超时值

增加WCF服务中的超时值,wcf,Wcf,如何将WCF服务的默认超时时间增加到大于1分钟?您指的是服务器端还是客户端 对于客户端,您需要调整绑定元素的sendTimeout属性。对于服务,您需要调整绑定元素的receiveTimeout属性 <system.serviceModel> <bindings> <netTcpBinding> <binding name="longTimeoutBinding" receiveTimeout="00:10:00"

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

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

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

....

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

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


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

不同的超时意味着不同的事情。当你为客户工作时。。您可能主要关注的是SendTimeout-查看此参考-精彩且相关的解释:

它说:

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 ...
    }
}
publicstaticvoidmain()
{
Uri baseAddress=新Uri(“http://localhost/MyServer/MyService");
尝试
{
ServiceHost ServiceHost=新的ServiceHost(typeof(CalculatorService));
WSHttpBinding=新的WSHttpBinding();
binding.OpenTimeout=newtimespan(0,10,0);
binding.CloseTimeout=新的时间跨度(0,10,0);
binding.SendTimeout=新的时间跨度(0,10,0);
binding.ReceiveTimeout=新的时间跨度(0,10,0);
AddServiceEndpoint(“ICalculator”,绑定,基址);
Open();
//现在可以访问该服务。
WriteLine(“服务准备就绪”);
控制台。WriteLine(“按以终止服务”);
Console.WriteLine();
Console.ReadLine();
}
捕获(通信例外)
{
//处理异常。。。
}
}
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>

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


除了绑定超时(在
Timespan
s中),您可能还需要此功能。这是以秒为单位的

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


如何在端点标记内使用“bindingname”映射绑定?服务器端的
receiveTimeout
控制基于会话的绑定空闲度的确定。例如,服务器不会将此设置用于basicHTTP绑定。从配置中可以看到,它是一个NetTcpBinding而不是BasicHttpBinding。该工具是避免错误的一个很好的方法,例如将元素包装成错误的方式、拼写等。很好!另请参见此处,了解打开日志文件的另一个工具:不清楚的是,我想您是在问,是否可以在服务器端配置超时处理任何需要一分钟以上时间的调用。