gpt4 book ai didi

C# Wcf 连接导致 TCP/IP 端口耗尽

转载 作者:行者123 更新时间:2023-12-03 12:09:20 25 4
gpt4 key购买 nike

我正在处理一个 ASP.NET MVC 项目,发现几个未关闭的 WCF 服务连接,我认为这会导致 TCP/IP 端口耗尽。

我在资源监视器/网络/TCP 连接处检查了服务器,并且有数千个灰色连接作为“IPV6 环回”,并且在某些时候有太多连接,服务器停止在服务端口上响应。

存在一个依赖注入(inject)来处理 Controller 上的连接,并且有一个“CloseChannel”方法,但它没有被调用,我对其进行了一些更改并开始在 Controller 上的 Dipose 方法中调用它以关闭连接,但是我没有得到任何结果。环回继续出现。

我认为要做的两个解决方案是:

  • 删除依赖注入(inject)并正常创建连接
    每次使用。

    除了关闭连接之外,在服务器上进行一些更改
    在此 post 中描述

  • 怀疑:
    有比我建议的更好的选择吗?如果没有,您认为哪一个是最好的?

    谢谢你们!

    PS.:今天用于打开和关闭连接的代码:

    这在 Controller 上调用:
    IClient wcfClient = WcfChannel.CreateChannel<IClient>(connectionstr, WcfChannel.WcfBinding.NetTcpBinding);

    这是 WcfChannel:
    public static class WcfChannel
    {
    public static T CreateChannel<T>(string endpointAddress, WcfBinding wcfBinding)
    {
    Binding binding = null;

    #region ReaderQuotas
    XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas()
    {
    MaxDepth = int.MaxValue,
    MaxStringContentLength = int.MaxValue,
    MaxArrayLength = int.MaxValue,
    MaxBytesPerRead = int.MaxValue,
    MaxNameTableCharCount = int.MaxValue
    };
    #endregion

    switch (wcfBinding)
    {
    case WcfBinding.BasicHttpBinding:
    case WcfBinding.NetMsmqBinding:
    case WcfBinding.NetNamedPipeBinding:
    throw new NotImplementedException();

    case WcfBinding.NetTcpBinding:
    binding = new NetTcpBinding()
    {
    Name = "NetTcpBinding",
    MaxBufferPoolSize = long.MaxValue,
    MaxBufferSize = int.MaxValue,
    MaxReceivedMessageSize = int.MaxValue,
    ReaderQuotas = readerQuotas,
    Security = new NetTcpSecurity() { Mode = SecurityMode.None },
    CloseTimeout = new TimeSpan(0, 5, 0),
    OpenTimeout = new TimeSpan(0, 5, 0),
    ReceiveTimeout = new TimeSpan(0, 5, 0),
    SendTimeout = new TimeSpan(0, 5, 0)
    };
    break;

    case WcfBinding.NetTcpBindingStreamed:
    binding = new NetTcpBinding()
    {
    Name = "NetTcpBindingStreamed",
    TransferMode = TransferMode.Streamed,
    MaxBufferPoolSize = long.MaxValue,
    MaxBufferSize = int.MaxValue,
    MaxReceivedMessageSize = int.MaxValue,
    ReaderQuotas = readerQuotas,
    Security = new NetTcpSecurity() { Mode = SecurityMode.None },
    CloseTimeout = new TimeSpan(0, 5, 0),
    OpenTimeout = new TimeSpan(0, 5, 0),
    ReceiveTimeout = new TimeSpan(0, 5, 0),
    SendTimeout = new TimeSpan(0, 5, 0)
    };
    break;
    case WcfBinding.WS2007HttpBinding:
    case WcfBinding.WSHttpBinding:
    throw new NotImplementedException();
    default:
    throw new NotImplementedException();
    }

    EndpointAddress endpoint = new EndpointAddress(endpointAddress);

    var channelFactory = new ChannelFactory<T>(binding, endpoint);
    T channelObj = channelFactory.CreateChannel();

    return channelObj;
    }

    public static void CloseChannel(this object obj)
    {
    if (obj != null)
    {
    try
    {
    (obj as IClientChannel).Close();
    }
    catch (CommunicationException)
    {
    if (obj.GetType().GetMethod("Abort") != null)
    {
    (obj as IClientChannel).Abort();
    }
    }
    catch (TimeoutException)
    {
    if (obj.GetType().GetMethod("Abort") != null)
    {
    (obj as IClientChannel).Abort();
    }
    }
    catch (Exception)
    {
    //many connections doesn't have and Abort or close
    }


    if (obj.GetType().GetMethod("Dispose") != null)
    {
    (obj as IDisposable).Dispose();
    }

    obj = null;
    }
    }

    public enum WcfBinding
    {
    BasicHttpBinding,
    NetMsmqBinding,
    NetNamedPipeBinding,
    NetTcpBinding,
    NetTcpBindingStreamed,
    WS2007HttpBinding,
    WSHttpBinding
    }
    }

    最佳答案

    我怀疑您的问题是由未管理 WCF session 这一事实产生的。 Net TCP Binding 是基于 session 的绑定(bind),需要对 session 进行管理。与 session 由服务器启动和管理的 ASP.NET 相比,在 WCF 中, session 由客户端启动和管理。
    您可以使用 ServiceContract/SessionMode、OperationContract/IsInitiating/IsTerminating 注释属性来管理 session 。 (此处的文档:https://docs.microsoft.com/en-us/dotnet/framework/wcf/using-sessions)

    在客户端,您需要在结束 session 后调用 CloseChannel 方法。此外,您需要验证所有异常的 channel 状态并调用 abort 方法(有关此处使用 Net TCP Binding 客户端的一些注意事项:https://blogs.msdn.microsoft.com/rodneyviana/2016/02/29/considerations-for-nettcpbindingnetnamedpipebinding-you-may-not-be-aware/)。此外,在服务器端,作为一种最佳实践,人们可能希望启用服务限制以限制 session /服务实例 (https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-servicethrottlingbehavior-to-control-wcf-service-performance)。

    关于C# Wcf 连接导致 TCP/IP 端口耗尽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45245377/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com