Skip to content

Remove WebRequest from SoundPlayer #65854

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

Closed
wants to merge 2 commits into from
Closed

Remove WebRequest from SoundPlayer #65854

wants to merge 2 commits into from

Conversation

KieranFoot
Copy link

I have removed the two instances of WebRequest and instead use HttpClient.

I could maybe refine it to only allocate a HttpClient if required.

Fix #41516

Replaced both usages of `WebRequest` and replaced with `HttpClient` instead.
Replaced the reference to `System.Net.Requests` with a reference to `System.Net.Http`.
@ghost ghost added area-System.Net community-contribution Indicates that the PR has been added by a community member labels Feb 24, 2022
@ghost
Copy link

ghost commented Feb 24, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

I have removed the two instances of WebRequest and instead use HttpClient.

I could maybe refine it to only allocate a HttpClient if required.

Fix #41516

Author: KieranFoot
Assignees: -
Labels:

area-System.Net

Milestone: -

@dnfadmin
Copy link

dnfadmin commented Feb 24, 2022

CLA assistant check
All CLA requirements met.

Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @KieranFoot

#pragma warning disable SYSLIB0014 // WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.
WebRequest webRequest = WebRequest.Create(_uri);
#pragma warning restore SYSLIB0014
webRequest.Timeout = LoadTimeout;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like we are loosing LoadTimeout without replacement. We may consider something like:

           using CancellationTokenSource cts = new CancellationTokenSource();
           cts.CancelAfter(LoadTimeout);

and pass it to GetStreamAsync


// now get the stream
_stream = webResponse.GetResponseStream();
_stream = _httpClient.GetStreamAsync(_uri).Result;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_stream = _httpClient.GetStreamAsync(_uri).Result;
_stream = _httpClient.GetStreamAsync(_uri).GetAwaiter().GetResult();

Since this function is synchronous, I'm wondering if we should use HttpClient.Send instead to avoid sync-over-async
Any thought on that @ManickaP @stephentoub ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HttpWebRequest is using the sync APIs already so we should likely do so here as well, or this change would actually regress perf.

@Tornhoof
Copy link
Contributor

Be aware that initiating and using a new HttpClient per instance of the Soundplayer is not the suggested way to use it.
See https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests for more details.
Unfortunately that concept is coupled closely to dependency injection.
I'm not aware of an easy way to use the factory without it.

@stephentoub
Copy link
Member

I appreciate the intent of the PR, but I think it's actually taking steps backwards in a few ways:

  • With how the WebRequest is being used in this PR, it'll end up using a shared HttpClient instance, whereas this PR causes it to use one per request (or per sound player)
  • The sync WebRequest methods are implemented on top of the sync HttpClient methods, but this is using the async HttpClient methods and then blocking waiting for them to complete.

It might make sense to wait for #65380 before trying to proceed here further. That would take care of the 1st issue, and the second issue can be addressed with just a bit more code (or we could also decide to add more sync helpers to HttpClient, e.g. a GetStream in addition to GetStreamAsync).

@KieranFoot
Copy link
Author

@stephentoub I am working on refining the use of HttpClient to use the sync methods where applicable.

As for your first point, I can see where HttpWebRequest has a cache of HttpClient, maybe that could be implemented at the HttpClient level.

@karelz karelz added this to the 7.0.0 milestone Mar 15, 2022
@MihaZupan
Copy link
Member

@KieranFoot are you still working on this?

@KieranFoot
Copy link
Author

KieranFoot commented Mar 15, 2022

@MihaZupan I did do some more work that I can add to the PR, however I think there was a call to wait for the issue #65380

I'll push what I have and hand over to you :)

@MihaZupan
Copy link
Member

We are looking at potentially implementing the HttpClient.Shared with sensible defaults for a static instance. To avoid re-implementing that here, I recommend we close this PR until that API is available.

@MihaZupan
Copy link
Member

Closing as per above until the API is available.

@MihaZupan MihaZupan closed this Mar 17, 2022
@ghost ghost locked as resolved and limited conversation to collaborators May 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove WebRequest from SoundPlayer
7 participants