wcf channelfactory 和 opentimeout [英] wcf channelfactory and opentimeout

查看:18
本文介绍了wcf channelfactory 和 opentimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在客户端中,我正在尝试连接到 WCF,将 OpenTimeout 属性更改为 5 秒,但它不起作用......这是我创建频道的方式:

In a client, I'm trying to connect to a WCF changing OpenTimeout property to 5 seconds but it's not working.... here is how I'm creating the channel:

NetTcpBinding bind = new NetTcpBinding(SecurityMode.None);
bind.OpenTimeout = new TimeSpan(0, 0, 5);
var channel = new ChannelFactory<IService>(bind, new EndpointAddress(myAddr)); 
channel.CreateChannel();

此后,我正在调用该方法,但如果服务器已关闭,则需要 21 秒而不是我在 OpenTimeout 上更改的 5 秒,我是否遗漏了什么?

After this, I'm calling the method but if the server is out, it takes 21 seconds and not the 5 that I changed on OpenTimeout, Am I missing something?

谢谢

推荐答案

我用下一个方法解决了这个问题.看来是可行的.

I resolved this problem in the next way. It seem to be works.

    protected TServiceContract CreateChannel()
    {
        TServiceContract channel = factory.CreateChannel();

        var ar = ((IChannel)channel).BeginOpen( null, null );

        if( !ar.AsyncWaitHandle.WaitOne( factory.Endpoint.Binding.OpenTimeout, true ) )
        {
            throw new TimeoutException( "Service is not available" );
        }

        ((IChannel)channel).EndOpen( ar );

        return channel;
    }

这篇关于wcf channelfactory 和 opentimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆