Skip to content

Commit

Permalink
feat(device): add support for DisposeAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
David R. Williamson committed May 21, 2021
1 parent 8eefef4 commit b50d5eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
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);
}

#if !NET451 && !NET472 && !NETSTANDARD2_0
// IAsyncDisposable is available in .NET 5.0 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(...);
/// </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
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

0 comments on commit b50d5eb

Please sign in to comment.