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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ System.Security.Cryptography.X509Certificates.X509SelectionFlag</PackageDescript
<Reference Include="System.Diagnostics.Tools" />
<Reference Include="System.IO.FileSystem" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.Requests" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Resources.ResourceManager" />
<Reference Include="System.Runtime.CompilerServices.Unsafe" />
<Reference Include="System.Runtime.Extensions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
Expand All @@ -22,6 +22,7 @@ public class SoundPlayer : Component, ISerializable
private Uri? _uri;
private string _soundLocation = string.Empty;
private int _loadTimeout = DefaultLoadTimeout;
private HttpClient _httpClient = new HttpClient();

// used to lock all synchronous calls to the SoundPlayer object
private readonly ManualResetEvent _semaphore = new ManualResetEvent(true);
Expand Down Expand Up @@ -261,16 +262,7 @@ private void LoadSync()
// setup the http stream
if (_uri != null && !_uri.IsFile && _stream == null)
{
#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


WebResponse webResponse;
webResponse = webRequest.GetResponse();

// 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.

}

// DO NOT assert - NRE is expected for null stream
Expand Down Expand Up @@ -486,14 +478,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken)
// setup the http stream
if (_uri != null && !_uri.IsFile && _stream == null)
{
#pragma warning disable SYSLIB0014 // WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.
WebRequest webRequest = WebRequest.Create(_uri);
#pragma warning restore SYSLIB0014
using (cancellationToken.Register(r => ((WebRequest)r!).Abort(), webRequest))
{
WebResponse webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false);
_stream = webResponse.GetResponseStream();
}
_stream = _httpClient.GetStreamAsync(_uri, cancellationToken).Result;
}

_streamData = new byte[BlockSize];
Expand Down