-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement DisconnectAsync method in SocketTaskExtensions #1608
Comments
Triage: We already have |
Duplicate of #33417. Not really (using Github keyword) -- just consolidating socket API proposals to streamline for focused API review. |
I'm going to reopen this so we can do API review specifically on this (as opposed to all the APIs in #33417). Also we should add a CancellationToken. Updated proposal: public static class SocketTaskExtensions
{
// Existing APIs:
// public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state);
// public void EndDisconnect (IAsyncResult asyncResult);
public static Task DisconnectAsync(this Socket that, bool reuseSocket, CancellationToken cancellationToken);
} |
namespace System.Net.Sockets
{
public static class SocketTaskExtensions
{
// Existing APIs:
// public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state);
// public void EndDisconnect (IAsyncResult asyncResult);
public static ValueTask DisconnectAsync(this Socket socket, bool reuseSocket, CancellationToken cancellationToken=default);
}
} |
Added #43901 |
Note, per approval of #43901, this should be added as an instance method and not added to SocketTaskExtensions. namespace System.Net.Sockets
{
public partial class Socket
{
public ValueTask DisconnectAsync(bool reuseSocket, CancellationToken cancellationToken=default);
}
} |
I was using the
Socket
extension methods provided by theSocketTaskExtensions
class inSystem.Net.Sockets
and noticed that almost all the*Async
methods fromSocket
that are IO bound operations seemed to have theirTask
returning counterpart inSocketTaskExtensions
except forDisconnectAsync
.Now, I've asked around online and people seemed to have mixed feelings about the issue. Some people didn't seem to understand why one would want to wait for the IO to complete when disconnecting a
Socket
. It's true that in most cases it's not really useful (and as I understand it, it's probably the reason it's not yet implemented in the framework) but even if use cases are limited, I would make the argument that for the sake of consistency, it'd be better that theSocketTaskExtensions
class provides aDisconnectAsync
method.Implementation
My proposition is to add a
DisconnectAsync
method to theSocketTaskExtensions
class.The task is intentionally not returning a
bool
as its result (like theSocket.DisconnectAsync()
is returning abool
to indicate whether the operation completed synchronously or not) because the goal of this extension method is to abstract that away.Usage
A developer could use the API in the following way
Additional context
Just to add some additional context, what led to me making this PR is this Stack Overflow question.
I would be happy to contribute an implementation for this feature!
The text was updated successfully, but these errors were encountered: