Skip to content

TcpListener should implement IDisposable #63114

@geoffkizer

Description

@geoffkizer

EDITED 3/14/2022 by @stephentoub:

namespace System.Net.Sockets
{
    public class TcpListener
+        : IDisposable
    {
+         void IDisposable.Dispose() => Stop();
    }
}

TcpListener is essentially a convenience wrapper around a Socket in listen mode. Sockets are disposable, but TcpListener is not. Instead, you are expected to call the Stop() method.

Implementing IDisposable enables "using" syntax and matches common library practice.

Consider that the sample code for TcpListener explicitly uses a try/finally to ensure that Stop() is called properly: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.tcplistener?view=net-6.0

See also https://stackoverflow.com/questions/33667149/why-tcplistener-does-not-implement-idisposable

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions