设置WCF RoutingService的操作超时时间

3

我在设置RoutingService的OperationTimeout上遇到了困难。

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

我尝试在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生成的代理可能不关心这个设置,这是可能的吗?
有什么建议吗?

在您的帖子中不需要包含签名 - 您的用户卡片会自动添加。阅读FAQ以获取更多详细信息。 - Artemix
2个回答

1
在 web.config 中设置下面的 SendTimeout。
<binding name="myBindingName" sendTimeout="00:10:00"
                 closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" 
                 maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647"
                 crossDomainScriptAccessEnabled="true" />

我对这篇文章进行了负评,因为虽然它能够工作,但非常不精确。请参考https://dev59.com/Om435IYBdhLWcg3wohpT#5310933,了解客户端使用的超时详细信息:在那种情况下,唯一需要更改的超时是sendTimeout。 - Eric Boumendil

1
我找到了解决方法。
你只需要在路由器客户端终结点的绑定上设置SendTimeout。在创建代理时,路由器将在其通道上设置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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接