.net c#WebChannelFactory keepAliveEnabled = false with webHttpBehavior

时间:2017-02-28 15:37:53

标签: keep-alive custom-binding webchannelfactory

我创建了WebChannelFactory。我需要设置KeepAlive = false。 我发现只有一个解决方案使用CustomBinding。并将属性keepAliveEnabled设置为false。

我也为我的工厂使用自定义行为。

代码:

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)
    { }
}

在当前情况下,我收到错误“没有与Message MessageVersion绑定”。

0 个答案:

没有答案