如何以编程方式为wcf命名管道设置ChannelInitializationTimeout

时间:2013-06-04 17:00:42

标签: wcf named-pipes channel netnamedpipebinding

我有一个命名管道wcf服务,我需要以编程方式设置ChannelInitializationTimeout属性。我目前没有使用任何配置文件。这是我目前的客户代码:

ChannelFactory<IStringReverser> pipeFactory = new ChannelFactory<IStringReverser>(
                                                                    new NetNamedPipeBinding(),
                                                                    new EndpointAddress(
                                                                    "net.pipe://localhost/PipeReverse"));

        IStringReverser pipeProxy = pipeFactory.CreateChannel();

以下是我的服务器代码:

using (System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(StringReverser),
                                        new Uri[]{new Uri("net.pipe://localhost")}))
        {
            host.AddServiceEndpoint(typeof(IStringReverser),
                            new NetNamedPipeBinding(),
                            "PipeReverse");

            host.Open();

            Console.WriteLine("Service is available. " +
              "Press <ENTER> to exit.");
            Console.ReadLine();

            host.Close();
        }

该服务正常运行,但我试图找出如何以编程方式更改ChannelInitializationTimeout属性作为我试图解决的另一个问题的一部分。

关于如何在代码中严格执行此操作的任何想法?提前谢谢。

1 个答案:

答案 0 :(得分:2)

创建一个自定义绑定,它将ChannelInitializationTimeout公开为绑定属性,或者在实例化时设置它,例如:

public class CustomNamedPipeBinding : CustomBinding
{
    public CustomNamedPipeBinding(TimeSpan initTimeout) : base()
    {
        NetNamedPipeBinding standardBinding = new NetNamedPipeBinding();
        foreach (BindingElement element in standardBinding.CreateBindingElements())
        {
            NamedPipeTransportBindingElement transportElement = element as NamedPipeTransportBindingElement;
            if (null != transportElement)
            {
                transportElement.ChannelInitializationTimeout = initTimeout;
            }
            base.Elements.Add(element);
        }
    }
}