MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

'idle.ClientInbox' threw an exception of type 'System.ObjectDisposedException'

Open SumitDobhal opened this issue 8 years ago • 6 comments

I implemented the MailKit SDK successfully in a console application! But now I am trying to implement it in a windows service but facing one issue. When I am implementing 'idle.Client.Idle(timeout.Token, idle.CancellationToken); ' in the IdleLoop method I am getting the exception Cannot access a disposed object. Object name: 'ImapClient'. And also now I am unable to access Client.Inbox , it gives me below exception idle.Client.Inbox' threw an exception of type 'System.ObjectDisposedException' | MailKit.IMailFolder {System.ObjectDisposedException}

The below is the stack trace of the error Cannot access a disposed object. Object name: 'ImapClient'..

at MailKit.Net.Imap.ImapClient.CheckDisposed()
 at MailKit.Net.Imap.ImapClient.<IdleAsync>d__90.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
 at MailKit.Net.Imap.ImapClient.Idle(CancellationToken doneToken, CancellationToken cancellationToken)
 at MailIt.ImapNotification.IdleLoop(Object state)
 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
 at System.Threading.ThreadHelper.ThreadStart(Object obj)

SumitDobhal avatar May 25 '18 08:05 SumitDobhal

Sounds like you've disposed the ImapClient object.

Are you creating the ImapClient in a using statement like this?

using (var client = new ImapClient ()) {
}

If so, the client object gets disposed at the end of that using block.

jstedfast avatar May 25 '18 14:05 jstedfast

Hi @jstedfast ! I just encountered same behavior, while not using using statement` Here is my pseudocode - I disconnect in desctructor:

namespace teststudio
{
    class EmailManager
    {
        protected static MailKit.IMailFolder inbox;
        protected static ImapClient client;

        public EmailManager(string baseUrl)
        {
            client = new ImapClient();
            client.Connect(host, port, ssl);
	    client.Authenticate(username, password);
            inbox = client.Inbox;
            inbox.Open(FolderAccess.ReadOnly);
        }

        ~EmailManager()
        {
            client.Disconnect(true);
        }

        public int getEmailCount()
        {
            return inbox.Count;
        }
    }
}

namespace mystart
{
    using teststudio;
    class Program
    {
        static void Main(string[] args)
        {
            EmailManager emailM = new EmailManager("https://imap.com");
            Console.WriteLine(emailM.getEmailCount().ToString());
            Console.ReadLine();
        }
    }
}

JGladykowski-modus avatar Oct 23 '18 19:10 JGladykowski-modus

You are trying to use the ImapClient to connect to a https URL. That won't work.

jstedfast avatar Oct 23 '18 19:10 jstedfast

Wow, thx for such fast reply. It all connects properly (this is just snippet of pseudocode). Only thing failing is at the end, when destructor is called. Then I get Cannot access a disposed object. error.

JGladykowski-modus avatar Oct 23 '18 19:10 JGladykowski-modus

You'll have to give me a real test case, then. But I guarantee that you are disposing it (or the GC is disposing it because the object went out of scope).

jstedfast avatar Oct 23 '18 19:10 jstedfast

Ha! This is so interesting. Digged little bit more. It only happens with this https://github.com/djfarrelly/MailDev (SSL disabled, port 143). When using outlook.office.com (SSL, 993) no error. Magic! Sorry, I cannot give you anything, as this happens on internal server behind VPN :(

More funny is, that Visual Studio GC clears the connection, without explicit closing. But not Test Studio... And error does not exists on outlook.office in Visual Studio , but exists in Test Studio. .NET version??

JGladykowski-modus avatar Oct 23 '18 19:10 JGladykowski-modus