【问题标题】:Different timeouts for different requests to the same WCF service对同一 WCF 服务的不同请求的不同超时
【发布时间】:2014-12-09 04:47:03
【问题描述】:

我有一个带有 https 绑定和回调的 WCF 服务。该服务在客户端中作为服务引用被引用。超时在服务和客户端的配置文件中定义。

我需要调用服务中的一个函数,其超时值小于用于其他函数的默认值。

我的客户的代码如下所示

MyService.MyServiceClient myServiceClient = new MyService.MyServiceClient(context);
myServiceClient.GenericSendData("Save Req", serializedString, ServerIdentifier, null);

如何更改客户端/服务器中的代码,以便对特定函数的请求使用较小的超时值?我可以根据我正在调用的函数在代码中指定和更改超时时间吗?

【问题讨论】:

  • 我使用的是点网4.5
  • 您可以编辑您的问题并在那里添加标签。
  • 简而言之,我认为你不能。超时将应用于整个服务(或客户端),而不仅仅是服务中的选择功能。
  • 我可以在同一个应用程序中使用不同的超时值创建一个单独的客户端吗?
  • 是的。看看我的回答。

标签: c# wcf timeout .net-4.5


【解决方案1】:

是的。只需在您的客户端上设置SendTimeout。要为特定服务调用设置 10 秒超时,您可以使用此示例:

using(MyService.MyServiceClient myServiceClient = new MyService.MyServiceClient(context))
{
    myServiceClient.Endpoint.Binding.SendTimeout = TimeSpan.FromSeconds(10);
    myServiceClient.GenericSendData("Save Req", serializedString, ServerIdentifier, null);
}

【讨论】:

    【解决方案2】:

    关于客户端,您可以通过编程方式操作绑定并为每个服务客户端实例设置客户端超时。思路是有不同的绑定配置,然后在ServiceClient构造函数中传递需要的绑定。

    这是一个实例化BasicHttpBinding 对象的示例,为第一个服务客户端实例设置 30 秒超时,然后将下一个服务客户端实例的超时更改为 5 秒:

    var customBinding = new BasicHttpBinding()
    {
        ReceiveTimeout = TimeSpan.FromSeconds(30), 
        SendTimeout = TimeSpan.FromSeconds(30),
        CloseTimeout = TimeSpan.FromSeconds(30),
        OpenTimeout = TimeSpan.FromSeconds(30) 
    };
    
    var endpointAddress = new EndpointAddress("http://localhost:6670/Service1.svc");
    
    using (var client = new Service1Client(customBinding, endpointAddress))
    {
        var data = client.GetData(100);
    }
    
    customBinding.OpenTimeout = TimeSpan.FromSeconds(5);
    customBinding.CloseTimeout = TimeSpan.FromSeconds(5);
    customBinding.SendTimeout = TimeSpan.FromSeconds(5);
    customBinding.ReceiveTimeout = TimeSpan.FromSeconds(5);
    
    using (var client = new Service1Client(customBinding, endpointAddress))
    {
        var data = client.GetData(101);
    }
    

    或者你可以通过配置来做到这一点:

    <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1_Shorter" openTimeout="00:00:05"
         closeTimeout="00:00:05"
         sendTimeout="00:00:05"
         receiveTimeout="00:00:05"/>
        <binding name="BasicHttpBinding_IService1_Longer" openTimeout="00:00:30"
           closeTimeout="00:00:30"
           sendTimeout="00:00:30"
           receiveTimeout="00:00:30"/>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:6670/Service1.svc" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IService1_Longer" contract="ServiceReference1.IService1"
          name="BasicHttpBinding_IService1_Longer" />
      <endpoint address="http://localhost:6670/Service1.svc" binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_IService1_Shorter" contract="ServiceReference1.IService1"
          name="BasicHttpBinding_IService1_Shorter" />
    </client>
    </system.serviceModel>
    

    然后您将实例化客户端以使用不同的配置:

     using (var client = new Service1Client("BasicHttpBinding_IService1_Longer"))
     {
         var data = client.GetData(100);
     }
    
     using (var client = new Service1Client("BasicHttpBinding_IService1_Shorter"))
     {
         var data = client.GetData(101);
     }
    

    关于服务器,根据每个请求更改正在运行的 WCF 服务的超时时间是不可能的,而且绝对不是一个好主意,而且没有任何意义。一种可能性是让不同的端点覆盖不同的操作,并且每个端点可以有不同的服务器超时设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2010-11-05
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      相关资源
      最近更新 更多