Skip to content
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

feat(device): add support for DisposeAsync #1977

Merged
merged 1 commit into from
May 24, 2021
Merged
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
42 changes: 42 additions & 0 deletions iothub/device/src/DeviceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -16,6 +17,9 @@ namespace Microsoft.Azure.Devices.Client
/// </summary>
/// <threadsafety static="true" instance="true" />
public class DeviceClient : IDisposable
#if !NET451 && !NET472 && !NETSTANDARD2_0
, IAsyncDisposable
#endif
{
/// <summary>
/// Default operation timeout.
Expand Down Expand Up @@ -613,12 +617,50 @@ public void SetConnectionStatusChangesHandler(ConnectionStatusChangesHandler sta
/// <summary>
/// Releases the unmanaged resources used by the DeviceClient and optionally disposes of the managed resources.
/// </summary>
/// <remarks>
/// The method <see cref="CloseAsync()"/> should be called before disposing.
/// </remarks>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
#if !NET451 && !NET472 && !NETSTANDARD2_0
// IAsyncDisposable is available in .NET Standard 2.1 and above

/// <summary>
/// Disposes the client in an async way. See <see cref="IAsyncDisposable"/> for more information.
/// </summary>
/// <remarks>
/// Includes a call to <see cref="CloseAsync()"/>.
/// </remarks>
/// <example>
/// <code>
/// await using var client = DeviceClient.CreateFromConnectionString(...);
drwill-ms marked this conversation as resolved.
Show resolved Hide resolved
/// </code>
/// or
/// <code>
/// var client = DeviceClient.CreateFromConnectionStirng(...);
/// try
/// {
/// // do work
/// }
/// finally
/// {
/// await client.DisposeAsync();
/// }
/// </code>
/// </example>
[SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "SuppressFinalize is called by Dispose(), which this method calls.")]
public async ValueTask DisposeAsync()
{
await CloseAsync().ConfigureAwait(false);
Dispose();
}

#endif

/// <summary>
/// Releases the unmanaged resources used by the DeviceClient and allows for any derived class to override and
/// provide custom implementation.
Expand Down
39 changes: 39 additions & 0 deletions iothub/device/src/ModuleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Net.Http;
Expand All @@ -22,6 +23,9 @@ namespace Microsoft.Azure.Devices.Client
/// Contains methods that a module can use to send messages to and receive from the service and interact with module twins.
/// </summary>
public class ModuleClient : IDisposable
#if !NET451 && !NET472 && !NETSTANDARD2_0
, IAsyncDisposable
#endif
{
private const string ModuleMethodUriFormat = "/twins/{0}/modules/{1}/methods?" + ClientApiVersionHelper.ApiVersionQueryStringLatest;
private const string DeviceMethodUriFormat = "/twins/{0}/methods?" + ClientApiVersionHelper.ApiVersionQueryStringLatest;
Expand Down Expand Up @@ -439,6 +443,41 @@ public void Dispose()
GC.SuppressFinalize(this);
}

#if !NET451 && !NET472 && !NETSTANDARD2_0
// IAsyncDisposable is available in .NET Standard 2.1 and above

/// <summary>
/// Disposes the client in an async way. See <see cref="IAsyncDisposable"/> for more information.
/// </summary>
/// <remarks>
/// Includes a call to <see cref="CloseAsync()"/>.
/// </remarks>
/// <example>
/// <code>
/// await using var client = ModuleClient.CreateFromConnectionString(...);
/// </code>
/// or
/// <code>
/// var client = ModuleClient.CreateFromConnectionStirng(...);
/// try
/// {
/// // do work
/// }
/// finally
/// {
/// await client.DisposeAsync();
/// }
/// </code>
/// </example>
[SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "SuppressFinalize is called by Dispose(), which this method calls.")]
public async ValueTask DisposeAsync()
{
await CloseAsync().ConfigureAwait(false);
Dispose();
}

#endif

/// <summary>
/// Releases the unmanaged resources used by the ModuleClient and allows for any derived class to override and
/// provide custom implementation.
Expand Down
3 changes: 1 addition & 2 deletions iothub/service/src/Common/Security/SharedAccessSignature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Azure.Devices.Common.Data;

namespace Microsoft.Azure.Devices.Common.Security
{
Expand Down Expand Up @@ -73,7 +72,7 @@ private SharedAccessSignature(
public string Signature { get; private set; }

/// <summary>
/// Parses a shared access signature string representation into a <see cref="SharedAccessSignature"/>./>
/// Parses a shared access signature string representation into a <see cref="SharedAccessSignature"/>.
/// </summary>
/// <param name="iotHubName">The IoT Hub name.</param>
/// <param name="rawToken">The string representation of the SAS token to parse.</param>
Expand Down