C
C#9mo ago
kurumi

HttpListener implements IDisposable but there is not Dispose() method, why and how can I dispose it?

#region Assembly System.Net.HttpListener, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.2\ref\net8.0\System.Net.HttpListener.dll
#endregion
#region Assembly System.Net.HttpListener, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
// C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\8.0.2\ref\net8.0\System.Net.HttpListener.dll
#endregion
No description
No description
3 Replies
Kiel
Kiel9mo ago
you could cast it to IDisposable first and then dispose it, some classes hide their implementation of Dispose for one reason or another IE (_listener as IDisposable).Dispose() or ((IDisposable) _listener).Dispose()
Angius
Angius9mo ago
Any particular reason you're trying to dispose it manually instead of a using block/statement?
kurumi
kurumiOP9mo ago
I am using 1 HttpListener for all incoming connection, so I am trying to do same things like HttpClient re-using I mean any way I found the way to do: this class contains Close() method which is pretty-much an alias for Dispose()
private readonly HttpListener _listener;
private _isDisposed = false;


public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}

if (disposing)
{
// free managed resources
_listener.Close();
}

_isDisposed = true;
}
private readonly HttpListener _listener;
private _isDisposed = false;


public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (_isDisposed)
{
return;
}

if (disposing)
{
// free managed resources
_listener.Close();
}

_isDisposed = true;
}
Want results from more Discord servers?
Add your server