c# - 在 Wcf RoutingService 上设置 OperationTimeout

标签 c# .net wcf wcf-routing

我正在努力设置 RoutingService 上的 OperationTimeout|

问题是消息转发到的服务需要超过 1 分钟的时间才能做出响应。这会导致 RoutingService 出现 OperationTimeout 异常。

我尝试在 RoutingService 的客户端代理上设置 OperationTimeout 但没有成功。

我所做的是添加端点行为并在 ApplyClientBehavior 方法中添加自定义 IClientMessageInspector。

在自定义 ClientMessageInspector 中,我设置了 OperationTimeout,就像您在这段代码中看到的那样。

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        var contextChannel = channel as IContextChannel;
        contextChannel.OperationTimeout = new TimeSpan(0, 10, 0);

        return request;
    }

对我来说,似乎我在这一点上太晚了,因此 RoutingService 生成的代理不关心这个设置,这可能是吗?

有什么建议吗?

最佳答案

我找到了解决这个问题的方法。

您只需要在路由器的客户端端点的绑定(bind)上设置 SendTimeout。创建代理时,路由器将在其 channel 上设置 OperationTimeout=SendTimeout。

            // add the endpoint the router uses to receive messages
            serviceHost.AddServiceEndpoint(
                 typeof(IRequestReplyRouter),
                 new BasicHttpBinding(), 
                 "http://localhost:8000/routingservice/router");

            // create the client endpoint the router routes messages to
            var client = new ServiceEndpoint(
                                            ContractDescription.GetContract(typeof(IRequestReplyRouter)), 
                                            new NetTcpBinding(),
                                            new EndpointAddress("net.tcp://localhost:8008/MyBackendService.svc"));

            // Set SendTimeout, this will be used from the router generated proxy as OperationTimeout
            client.Binding.SendTimeout = new TimeSpan(0, 10, 0);

关于c# - 在 Wcf RoutingService 上设置 OperationTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13915623/

相关文章:

c# - 使用 ASP.NET 动态数据实体过滤表中的行

c# - 在框架中使用 Funcs 而不是实例

.net - 在哪里可以获得 .NET 中套接字中保持事件的规范?

c# - 向项目添加 WCF 服务

wcf - 如何构建 WCF 项目

c# - 带有 'same port' 的 WCF 多主机 header 导致错误

c# - 如何使用 linq 连接 2 个表但避免匿名对象

c# - asp.net DropDownList 不适用于 ASPNET 4.5

c# - DateTime.TryParseExact 为 MM/dd/yyyy hh 返回 false :mm

c# - 在 C# 中获取 'ldftn' 函数指针