[英]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.