Add HttpClient extensions to net461

This commit is contained in:
Yoshi Askharoun
2023-05-04 00:40:30 -05:00
parent eeb046f204
commit 4aa89544af
4 changed files with 11 additions and 35 deletions
+6
View File
@@ -25,5 +25,11 @@
<DefineConstants Condition="$(TargetFramework.StartsWith('net6.0-windows8'))">$(DefineConstants);WINDOWS8</DefineConstants>
</PropertyGroup>
</When>
<When Condition=" $(TargetFramework.StartsWith('net4')) ">
<PropertyGroup>
<DefineConstants>$(DefineConstants);NETSTANDARD1_0_OR_GREATER;NETSTANDARD2_0_OR_GREATER</DefineConstants>
</PropertyGroup>
</When>
</Choose>
</Project>
+2 -13
View File
@@ -12,18 +12,14 @@ namespace Microsoft.Zune
internal static class Extensions
{
#if NETSTANDARD2_0_OR_GREATER && !NET5_0_OR_GREATER
// FIXME: Blocking the executing thread is generally a bad idea.
// Executing some of these methods has a chance of
// deadlocking the app.
public static Stream ReadAsStream(this HttpContent content)
{
return content.ReadAsStreamAsync().Result;
return Task.Run(content.ReadAsStreamAsync).Result;
}
public static HttpResponseMessage Send(this HttpClient client, HttpRequestMessage request)
{
return client.SendAsync(request).Result;
return Task.Run(() => client.SendAsync(request)).Result;
}
public static Task<Stream> GetStreamAsync(this HttpClient client, Uri uri, CancellationToken _)
@@ -31,12 +27,5 @@ namespace Microsoft.Zune
return client.GetStreamAsync(uri);
}
#endif
#if NETSTANDARD && !NETSTANDARD2_1_OR_GREATER
public static Task CopyToAsync(this Stream source, Stream destination, CancellationToken _)
{
return source.CopyToAsync(destination);
}
#endif
}
}
+2 -14
View File
@@ -51,22 +51,10 @@ namespace Microsoft.Zune.Service
Escargot.AuthenticateRequest(request);
var response =
#if NET5_0_OR_GREATER
_client.Send(request);
#else
Task.Run(() => _client.SendAsync(request)).Result;
#endif
var response = _client.Send(request);
response.EnsureSuccessStatusCode();
System.IO.Stream responseStream =
#if NET5_0_OR_GREATER
response.Content.ReadAsStream();
#else
Task.Run(response.Content.ReadAsStreamAsync).Result;
#endif
Stream responseStream = response.Content.ReadAsStream();
var responseObj = ReadSignInResponse(responseStream);
uint? errorCode = responseObj?.AccountState?.SignInErrorCode;
+1 -8
View File
@@ -2,7 +2,6 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
namespace Microsoft.Zune.Service
{
@@ -63,13 +62,7 @@ namespace Microsoft.Zune.Service
try
{
var response =
#if NET5_0_OR_GREATER
_client.Send(request);
#else
Task.Run(() => _client.SendAsync(request)).Result;
#endif
var response = _client.Send(request);
response.EnsureSuccessStatusCode();
string token = response.Headers.GetValues(HEADER_X_TOKEN).FirstOrDefault();