c# - 时间: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();
解决方案
推荐阅读
- swift - SwiftUi 按钮操作重新加载 WebView
- sql - SQL 函数因特定输入而超时(while 循环)
- angular - 找不到“en-US”的 Angular 9 语言环境数据。不会包含此语言环境的语言环境数据
- javascript - CSS 在一列中显示帖子网格,就像我添加的图像一样
- reactjs - 在每个 .php 页面中添加类似 jquery 的反应?
- angular - 即使填写了所有字段,按钮也没有启用?
- c++ - MSVC 错误:4 个重载都不能转换所有参数类型
- python-3.x - Sqlite3 查找行值的变化
- macos - 如何使用 ffmpeg 在 MacBook 上一次录制多个屏幕和音频?
- javascript - 使用 WebCrypto API 加密大文件