如何在Castle Windsor WcfClient上设置Timeout属性

时间:2017-11-28 09:49:23

标签: c# .net wcf castle-windsor wcf-binding

我有一个Windows服务,我可以像这样调用Wcf客户端端点。

        Console.WriteLine("Invoking start...");
        using (var container = new WindsorContainer())
        {
            container.AddFacility<WcfFacility>();
            container.Register(
                Component.For<IShoppingService>()                        
                    .AsWcfClient(new DefaultClientModel(
                        WcfEndpoint
                            .ForContract<IShoppingService>()
                            .BoundTo(new NetTcpBinding(SecurityMode.None))
                            .At("net.tcp://localhost:12123/shoppingService"))));
            container.Resolve<IShoppingService>().Debug();
        }
        Console.WriteLine("Invoking end...");

我想以编程方式将超时的超时时间增加到10分钟以进行调试。如何在客户端设置超时属性,就像我通常在app.config文件中的绑定部分那样设置,如此

  <system.serviceModel>
<bindings>
  <netTcpBinding>
    <binding name="longTimeOutLargeTcpBuffer" maxBufferSize="20000000" maxReceivedMessageSize="20000000" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00">
      <readerQuotas maxArrayLength="20000000" maxBytesPerRead="20000000" maxStringContentLength="10000000" />
      <security mode="None" />
    </binding>        
  </netTcpBinding>
</bindings>

干杯

1 个答案:

答案 0 :(得分:1)

您可以直接在绑定上设置属性:

    var timeout = new TimeSpan(0, 0, 10, 0);
    var binding = new NetTcpBinding(SecurityMode.None)
                      {
                          CloseTimeout = timeout,
                          ReceiveTimeout = timeout,
                          SendTimeout = timeout,
                          OpenTimeout = timeout
                      };

然后:

    .BoundTo(binding)