代码之家  ›  专栏  ›  技术社区  ›  JohnWick

C#'无法访问已释放的对象。对象名称:“SslStream”。'

c#
  •  0
  • JohnWick  · 技术社区  · 6 年前

    public static List<Message> FetchAllMessages(string hostname, int port, string username, string password, string proxyIp, int proxyPort)
    {
        // The client disconnects from the server when being disposed
        using (Pop3Client client = new Pop3Client())
        {
            var proxyClient = new HttpProxyClient(proxyIp, proxyPort);
            using (var sslStream = new SslStream(proxyClient.CreateConnection(hostname, port).GetStream()))
            {
                sslStream.AuthenticateAsClient(hostname);
                client.Connect(sslStream);
                // Authenticate ourselves towards the server
                client.Authenticate(username, password);
                // Get the number of messages in the inbox
                int messageCount = client.GetMessageCount();
    
                // We want to download all messages
                List<Message> allMessages = new List<Message>(messageCount);
    
                // Messages are numbered in the interval: [1, messageCount]
                // Ergo: message numbers are 1-based.
                // Most servers give the latest message the highest number
                for (int i = messageCount; i > 0; i--)
                {
                    allMessages.Add(client.GetMessage(i));
                }
                                // Now return the fetched messages
                return allMessages;
            }
        }
    }
    

    更新:

    在returnallmessages上放置一个断点;如果将其悬停在sslStream对象上,则不会释放它。

    1 回复  |  直到 6 年前
        1
  •  3
  •   JohnWick    6 年前

    所以,我真的没有一个解释,为什么,但重新启动我的电脑解决了问题,代码运行良好后,这样做。。。