forked from Azure/azure-iot-sdk-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(all): Add support for .NET 5.0 * refactor(all): Update APIs to follow .NET 5.0 patterns * refactor(e2e-tests): Update E2E tests to follow .NET 5.0 patterns * refactor(all): Simplify disposal of IDisposable X509Certificate * fix(vsts): Update the location for log analytics workspace resource * refactor(e2e-tests): Dispose IDisposable types * fix(e2e-tests): Dispose IDisposable types created - hmac
- Loading branch information
1 parent
688e6f0
commit 4b6752e
Showing
91 changed files
with
967 additions
and
708 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Devices.Shared | ||
{ | ||
/// <summary> | ||
/// Extensions added to simplify the usage of <see cref="HttpContent"/> APIs based on the .NET implementation used. | ||
/// </summary> | ||
internal static class HttpContentExtensions | ||
{ | ||
internal static async Task CopyToStreamAsync(this HttpContent content, Stream stream, CancellationToken cancellationToken) | ||
{ | ||
#if NET5_0 | ||
await content.CopyToAsync(stream, cancellationToken).ConfigureAwait(false); | ||
#else | ||
// .NET implementations < .NET 5.0 do not support CancellationTokens for HttpContent APIs, so we will discard it. | ||
_ = cancellationToken; | ||
|
||
await content.CopyToAsync(stream).ConfigureAwait(false); | ||
#endif | ||
} | ||
|
||
internal static Task<Stream> ReadHttpContentAsStream(this HttpContent httpContent, CancellationToken cancellationToken) | ||
{ | ||
#if NET5_0 | ||
return httpContent.ReadAsStreamAsync(cancellationToken); | ||
#else | ||
// .NET implementations < .NET 5.0 do not support CancellationTokens for HttpContent APIs, so we will discard it. | ||
_ = cancellationToken; | ||
|
||
return httpContent.ReadAsStreamAsync(); | ||
#endif | ||
} | ||
|
||
internal static Task<byte[]> ReadHttpContentAsByteArrayAsync(this HttpContent content, CancellationToken cancellationToken) | ||
{ | ||
#if NET5_0 | ||
return content.ReadAsByteArrayAsync(cancellationToken); | ||
#else | ||
// .NET implementations < .NET 5.0 do not support CancellationTokens for HttpContent APIs, so we will discard it. | ||
_ = cancellationToken; | ||
|
||
return content.ReadAsByteArrayAsync(); | ||
#endif | ||
} | ||
|
||
internal static Task<string> ReadHttpContentAsStringAsync(this HttpContent content, CancellationToken cancellationToken) | ||
{ | ||
#if NET5_0 | ||
return content.ReadAsStringAsync(cancellationToken); | ||
#else | ||
// .NET implementations < .NET 5.0 do not support CancellationTokens for HttpContent APIs, so we will discard it. | ||
_ = cancellationToken; | ||
|
||
return content.ReadAsStringAsync(); | ||
#endif | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Net.Http; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
|
||
#if NET451 | ||
using System.Net.Http.Formatting; | ||
#endif | ||
|
||
namespace Microsoft.Azure.Devices.Shared | ||
{ | ||
/// <summary> | ||
/// A helper class to simplify operations with Http messages based on the .NET implementation used. | ||
/// </summary> | ||
internal static class HttpMessageHelper | ||
{ | ||
#if NET451 | ||
private static readonly JsonMediaTypeFormatter s_jsonFormatter = new JsonMediaTypeFormatter(); | ||
#else | ||
private const string ApplicationJson = "application/json"; | ||
#endif | ||
|
||
internal static void SetHttpRequestMessageContent<T>(HttpRequestMessage requestMessage, T entity) | ||
{ | ||
#if NET451 | ||
requestMessage.Content = new ObjectContent<T>(entity, s_jsonFormatter); | ||
#else | ||
string str = JsonConvert.SerializeObject(entity); | ||
requestMessage.Content = new StringContent(str, Encoding.UTF8, ApplicationJson); | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Devices.Shared | ||
{ | ||
/// <summary> | ||
/// Extensions added to simplify the usage of <see cref="Stream"/> APIs based on the .NET implementation used. | ||
/// </summary> | ||
internal static class StreamExtensions | ||
{ | ||
internal static async Task WriteToStreamAsync(this Stream stream, byte[] requestBytes, CancellationToken cancellationToken) | ||
{ | ||
#if NET451 || NET472 || NETSTANDARD2_0 | ||
await stream.WriteAsync(requestBytes, 0, requestBytes.Length, cancellationToken).ConfigureAwait(false); | ||
#else | ||
await stream.WriteAsync(requestBytes, cancellationToken).ConfigureAwait(false); | ||
#endif | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Devices.Shared | ||
{ | ||
/// <summary> | ||
/// A <see cref="TaskCompletionSource{boolean}"/> implementation that returns a <see cref="Task"/> when completed. | ||
/// </summary> | ||
/// <remarks> | ||
/// Represents the producer side of a <see cref="Task"/> unbound to a delegate, providing access to the consumer side through the <see cref="Task"/> property. | ||
/// This is used for .NET implementations lower than .NET 5.0, which lack a native implementation of the non-generic TaskCompletionSource. | ||
/// </remarks> | ||
internal sealed class TaskCompletionSource : TaskCompletionSource<bool> | ||
{ | ||
public TaskCompletionSource() | ||
{ | ||
} | ||
|
||
public bool TrySetResult() => TrySetResult(true); | ||
|
||
public void SetResult() => SetResult(true); | ||
|
||
public override string ToString() => $"TaskCompletionSource[status: {Task.Status}]"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.