Reputation: 1464
I wonder what happens when I first create a TCPClient
within a function scope, extract the NetworkStream
out of it, pass that to the c-tor of a class and return the newly created object:
{
TcpClient client = new TcpClient();
await client.ConnectAsync(host, outport).ConfigureAwait(false);
NetworkStream stream = client.GetStream();
TCPWrapperAdapterWithStream adapter = new TCPWrapperAdapterWithStream(stream, stream, 100);
return adapter;
}
What happens with client after the context is left? Will stream still be working?
Can I do it in this way or do I have to "keep" the TCPClient
object?
Upvotes: 3
Views: 112
Reputation: 43278
TCPClient starts throwing if you continue to use it. I ended up filing a bug when I discovered SSLStream threw for no good reason in Dispose because it called TCPClient.Flush() which threw when NetworkStream was dead.
(I needed to shutdown SSLStream immediately because I knew it was dead in a way that it was going to block for seconds before failing if closed the normal way so I tried this.)
On the other hand, TCPClient will attempt to dispose its NetworkStream in the finalizer. I got bit by this once ages ago.
Upvotes: 1