I have a SocketState object which I am using to wrap a buffer and a socket together and pass it along for use with the Begin/End async socket methods. In the destructor, I have this:
~SocketState()
{
if (!_socket.Connected) return;
_socket.Shutdown(SocketShutdown.Both);
_socket.Close();
}
When it gets to Close(), I get an ObjectDisposed exception. If I comment out the call Shutdown(), I do not get the error when it gets to the Close() method. What am I doing wrong?
Edit:
I know that the IDisposable solution seems to be how my code should be laid out, but that doesn't actually solve my problem. It's not like the destructor is getting called twice, so why would calling dispose() instead of using the destructor help me? I still get the same exception when calling these 2 functions in succession.
I have looked at the source for a similar server, and all they do is wrap these 2 statements in a try block and swallow the exception. I'll do that if I have to because it seems harmless (we're closing it anyway), but I would like to avoid it if possible.