.net c# WebChannelFactory keepAliveEnabled=false with webHttpBehavior

161 Views Asked by At

I create WebChannelFactory. I need set KeepAlive=false. I found only one solution use CustomBinding. and set property keepAliveEnabled to false.

I use custom behavior for my factory also.

Code:

static CustomBinding GetBinding(MySettings serviceSettings = null)
    {
        var customBinding = new CustomBinding();
        HttpTransportBindingElement transportBindingElement = new HttpTransportBindingElement();
        transportBindingElement.KeepAliveEnabled = false;
        transportBindingElement.MaxBufferSize = 0x07000000;
        transportBindingElement.MaxReceivedMessageSize = 0x07000000;

        if (serviceSettings != null)
        {
            customBinding.SendTimeout = serviceSettings.SendTimeout;
        }

        customBinding.Elements.Add(transportBindingElement);

        return customBinding;
    }

    var customBinding = GetBinding(serviceSettings);
        WebChannelFactory<TChannel> factory = new WebChannelFactory<TChannel>(customBinding, new Uri(url));
        factory.Endpoint.Behaviors.Add(new MyWebHttpBehavior(userId));

 class MyWebHttpBehavior : IEndpointBehavior
{
    private int _userId;

    public MyWebHttpBehavior(int userId)
    {
        _userId = userId;
    }

    public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)

    { }

    public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.ClientRuntime behavior)
    {
        behavior.MessageInspectors.Add(new MyClientMessageInspector(_userId));
    }

    public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    { }

    public void Validate(ServiceEndpoint serviceEndpoint)
    { }
}

In current situation i get error "does not have a Binding with the None MessageVersion".

0

There are 0 best solutions below