首页 > 解决方案 > 时间:2019-05-10 标签:c#wcf web-service client https connection never close

问题描述

我正在使用远程 Web 服务的简单 c# Windows 服务客户端。Everyghing 效果很好,但是当我向 Web 服务发出数据请求然后我也关闭客户端和工厂时,https 连接不会关闭。(我可以使用“netstat -n”命令查看)。为什么?任何想法?

这是我的代码:

        BasicHttpBinding binding = new BasicHttpBinding();
        binding.CloseTimeout = new TimeSpan(0, 0, 10);
        binding.OpenTimeout = new TimeSpan(0, 0, 10);
        binding.ReceiveTimeout = new TimeSpan(0, 0, 10);
        binding.SendTimeout = new TimeSpan(0, 0, 10);

        string address = AppSettings.Instance.address;
        if (address.Contains("https"))
            binding.Security.Mode = BasicHttpSecurityMode.Transport;

        binding.MaxBufferSize = 2147483647;
        binding.MaxBufferPoolSize = 2147483647;
        binding.MaxReceivedMessageSize = 2147483647;

        XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
        readerQuotas.MaxStringContentLength = 2147483647;
        binding.ReaderQuotas = readerQuotas;

        ChannelFactory<NotifierServiceChannel> factory = new ChannelFactory<NotifierServiceChannel>(binding, address);

        factory.Closed += Factory_Closed;
        factory.Closing += Factory_Closing;
        factory.Faulted += Factory_Faulted;
        factory.Opened += Factory_Opened;
        factory.Opening += Factory_Opening;

        foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
        {
            DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
            if (dataContractBehavior != null)
                dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
        }
        factory.Open();

        using (NotifierServiceChannel client = factory.CreateChannel())
        {
            client.Closed += _client_Closed;
            client.Closing += _client_Closing;
            client.Faulted += _client_Faulted;
            client.Opened += _client_Opened;
            client.Opening += _client_Opening;
            client.UnknownMessageReceived += _client_UnknownMessageReceived;

            client.Open(new TimeSpan(0, 0, 10));

            GetIdRequest requestId = new GetIdRequest();
            GetIdResponse responseId = client.GetIdResponse(requestId);

            LogManager.LoggerV("client.State = " + client.State.ToString());

            if (client.State == CommunicationState.Faulted)
                client.Abort();
            else
                client.Close();

            LogManager.LoggerV("client.State = " + client.State.ToString());

            client.Dispose();

            LogManager.LoggerV("client.State = " + client.State.ToString());
        }

        LogManager.LoggerV("factory.State = " + factory.State.ToString());

        if (factory.State == CommunicationState.Faulted)
            factory.Abort();
        else
            factory.Close();

标签: c#web-serviceswcfhttpsbasichttpbinding

解决方案


推荐阅读