Reputation: 428
Today, I am writing a framework which is using .NET Framework 4.8
. There is a HttpListener
in my code, which used to get message from the target website, and the code works fine. However, when I started the listener and stopped it immediately, it showed me the ObjectDisposedException
error. In that case, I tried to use single step debugging and the interesting thing is that nothing was wrong.
In that case, I tried to delete some code and here is the minimal reproducible code:
HttpListener httpListener = new HttpListener();
httpListener.Start();
httpListener.Stop();
When I run it, it will show me the ObjectDisposedException
error at line 3(httpListener.Stop();
), and if you stack trace the code, it shows that the error will happen in line 150 of the SafeHandle.cs
:
throw new ObjectDisposedException(nameof(SafeHandle), SR.ObjectDisposed_SafeHandleClosed);
However, I can not understand that much code, what happened, and what caused that error without taking a lot of time, so I am wondering why that will happen.
Upvotes: 1
Views: 135
Reputation: 45119
Unfortunately I don't have a real explanation, but if you take a look at the source code, you can see that the Stop() method calls CheckDisposed(), which throws the given exception, when m_State==State.Closed. The only ways to set the state explicit by a method call to this value are the methods Abort() and Dispose(). Additionally exceptions in SetupV2Config() and Start() could enforce this state, but would also throw a few lines later.
So this is not a real answer to your question. But at least it shows the points, when this error could occur and maybe someone else is able to dig deeper or better explain why this error occurs with the given information.
Upvotes: 1