简体   繁体   English

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

[英]C# 'Cannot access a disposed object. Object name: 'SslStream'.'

The exception in my questions title is being thrown on the line return allMessages; 我的问题标题中的异常被抛出return allMessages; in the code below. 在下面的代码中。 I don't understand it because that line of code is still within the using (var sslStream...) statement, so by the time it is executed the SslStream object should not be disposed.. 我不明白这是因为该行代码仍在using(var sslStream ...)语句内,因此在执行该代码时,不应处置SslStream对象。

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;
        }
    }
}

UPDATES: 更新:

Put a breakpoint on return allMessages; 在return allMessages上设置一个断点; and hovered over the sslStream object, it is not disposed. 并悬停在sslStream对象上,则不会对其进行处理。

When I remove the using statement and just declare var sslStream it works but I don't think this would scale well without it.. 当我删除using语句并只声明var sslStream时,它可以工作,但是我认为如果没有它,它将无法很好地扩展。

因此,我实际上没有任何解释,但是重新启动计算机可以解决问题,这样做之后代码可以正常运行...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#XNA无法访问已处置的对象。 对象名称:“ Texture2D”。 但是我没有处理它 - c# XNA Cannot access a disposed object. Object name: 'Texture2D'. But im not disposing it 无法访问已处置的 object。 Sockets C# - Cannot access a disposed object. Sockets C# AutoMapper IValueResolver:无法访问已处理的对象。 对象名称:&#39;IServiceProvider&#39; - AutoMapper IValueResolver: Cannot access a disposed object. Object name: 'IServiceProvider' ObjectDisposedException:无法访问已处置的对象。 对象名称:“调度程序” - ObjectDisposedException: Cannot access a disposed object. Object name: 'Dispatcher' HangFire“无法访问已处置的对象。对象名称:'SqlDelegatedTransaction'” - HangFire "Cannot access a disposed object. Object name: 'SqlDelegatedTransaction'" 无法访问已处置的对象。 对象名称:&#39;tlsxyz&#39; - Cannot access a disposed object. Object name: 'tlsxyz' 无法访问已处置的对象。 对象名称:“ NumericUpDown” - Cannot access a disposed object. Object name: 'NumericUpDown' 无法访问已处置的对象。\\ r \\ n对象名称:“ ApplicationUserManager” - Cannot access a disposed object.\r\nObject name: 'ApplicationUserManager' 尝试打开C#表单时显示“无法访问已处置的对象。”错误消息 - “Cannot access a disposed object.” error message shown while trying to open a C# form 无法访问已处置的对象。 与XPO - Cannot access a disposed object. with XPO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM