1

The exception in my questions title is being thrown on the line 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..

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; and hovered over the sslStream object, it is not disposed.

When I remove the using statement and just declare var sslStream it works but I don't think this would scale well without it..

10
  • There must be something about client.GetMessage(i). Try to clone it. Well, right. client holds to the message, you add message to list. This is same instance. When you dispose client, it disposes message. Try doing ToMailMessage and add to list. This should work Commented Nov 3, 2018 at 4:48
  • 1
    Check the updated comment. When I say "clone it", I mean, you need to try adding to list not the same message held by client Commented Nov 3, 2018 at 4:53
  • 1
    Try this. Call bytes = client.GetMessageAsBytes(); then list.Add(new Message(bytes));. Should do it. This is your clone mechanism. If this works, I'll post pretty solution answer. Let me know Commented Nov 3, 2018 at 4:56
  • 1
    We recently started using MySql DB among others. And we plugged-in as you said, "pretty widely used/updated open source library" , which is MySQL Data Provider for .net. into sqlServ/Oracle system that worked for 15 years. MySql had a show-stopper bug in it, which I eliminated buy building proxies around it. Then I reported 3 visual studio bugs this year as well. 2 were fixed already. So, you know, bugs are there Commented Nov 3, 2018 at 5:11
  • 1
    I would use dotPeek to reflect SslStream and po3Client and look into its Dispose Commented Nov 3, 2018 at 5:18

1 Answer 1

3

So, I don't really have an explanation as to why, but restarting my computer solved the problem, the code runs fine after doing that...

Sign up to request clarification or add additional context in comments.

1 Comment

Seriously? That is unexpected!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.