-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
How to implement Stream nowadays? #59403
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Related: #58216 Answers based on my knowledge:
Anti-pattern should be better than not functional at all.
Use the span/memory overload as core implementation, and delegates all others to them. For compatibility, the base implementations in
The caller could call either |
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsWe currently need to implement our own Stream, using .NET 5, which wraps an asynchronous HTTP-based client API. Especially we need to:
The client API does not provide any synchronous methods. Our questions are:
Best regards,
|
You can take a look at the documentation for Stream, especially this part: https://docs.microsoft.com/en-us/dotnet/api/system.io.stream?view=net-5.0#notes-to-implementers. Regarding your scenario, seems like you need an implementation that deals with HTTP: There may exist a better starting point to do that rather than implementing your own stream from scratch e.g: https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.networkstream?view=net-5.0. cc @dotnet/ncl. |
@jozkee Are you sure that is up to date regarding async methods? Regarding those, it says:
Following this advice, if I only have an async API underlying my stream implementation, I'll end up with:
That can't be good, or can it? |
Well, it you map the perf will never be great @fschmied. The real question is what you are really trying to get. The more you implement and do without mapping the better IMHO. Seems like the link is minimalistic. Also note that the 5.0 HttpClient has synchronous functions as well: https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.send?view=net-5.0 |
@wfurt I didn't really mean performance. AFAIK sync-over-async is an anti-pattern that can cause grave problems. That's why I think the docs section mentioned above isn't really good advice, especially for the scenario described here.
That's really cool and will make some thinks easier. In this specific case, it probably won't help; from the OP:
|
@drauch I've just found that @stephentoub wrote a really nice article about subclassing Regarding the abstract, sync members:
Regarding the async ones:
(The link to implement the Begin/End methods via Tasks in the original blog is broken, here's a working one: https://devblogs.microsoft.com/pfxteam/Using-Tasks-to-implement-the-APM-Pattern/ .) |
Thank you @fschmied for digging into this. So, in short, I should probably:
Unfortunately, this still leaves me with my question of whether and how to properly implement IDisposable/IAsyncDisposable if I need to perform an async operation in the disposal of the stream. I guess I will implement IAsyncDisposable and sync-over-async in IDisposable. Best regards, |
We currently need to implement our own Stream, using .NET 5, which wraps an asynchronous HTTP-based client API. Especially we need to:
The client API does not provide any synchronous methods.
We noticed that the base class only forces us to override synchronous methods.
Our questions are:
Best regards,
D.R.
The text was updated successfully, but these errors were encountered: