1

我创建了 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)
    { }
}

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

4

0 回答 0