Question

I have a named pipe wcf service that I need to programmatically set the ChannelInitializationTimeout property for. I am currently not using any configuration files. Here is my current client code:

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

        IStringReverser pipeProxy = pipeFactory.CreateChannel();

Following is my server code:

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();
        }

The service is functioning correctly, but I am trying to figure out how to programmatically change the ChannelInitializationTimeout property as part of another issue I am trying to solve.

Any ideas on how to do this strictly in code? Thanks in advance.

Was it helpful?

Solution

Make a custom binding which either exposes the ChannelInitializationTimeout as a binding property, or sets it when it is instantiated, for instance like this:

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);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top