Skip to content

Commit

Permalink
(fix): Build error due to missing dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
vinagesh committed Feb 3, 2021
1 parent c418ac3 commit 7324a53
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 7 deletions.
3 changes: 3 additions & 0 deletions azureiot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "security", "security", "{96531499-C8F4-4F77-88DD-9F9585B5E565}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "e2e", "e2e", "{662CE01D-7A7F-48D8-80CE-5DC9C60677E8}"
ProjectSection(SolutionItems) = preProject
iothub\device\src\DeviceAuthenticationWithRegistrySymmetricKey.cs = iothub\device\src\DeviceAuthenticationWithRegistrySymmetricKey.cs
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "device", "device", "{A48437BA-3C5B-431E-9B2F-96C850E9E1A5}"
EndProject
Expand Down
8 changes: 5 additions & 3 deletions iothub/device/src/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static InternalClient Create(string hostname, string gatewayHostname, IAu
throw new ArgumentException("Certificate chains are only supported on Amqp_Tcp_Only and Mqtt_Tcp_Only");
}

IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod);
using IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod);

// Make sure client options is initialized with the correct transport setting.
EnsureOptionsIsSetup(connectionStringBuilder.Certificate, ref options);
Expand All @@ -100,7 +100,9 @@ public static InternalClient Create(string hostname, string gatewayHostname, IAu
throw new ArgumentException("No certificate was found. To use certificate authentication certificate must be present.");
}

#pragma warning disable CA2000 // This is returned to client so cannot be disposed here.
InternalClient dc = CreateFromConnectionString(connectionStringBuilder.ToString(), PopulateCertificateInTransportSettings(connectionStringBuilder, transportType), options);
#pragma warning restore CA2000
dc.Certificate = connectionStringBuilder.Certificate;

// Install all the intermediate certificates in the chain if specified.
Expand Down Expand Up @@ -158,7 +160,7 @@ public static InternalClient Create(string hostname, string gatewayHostname, IAu
throw new ArgumentNullException(nameof(authenticationMethod));
}

var connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod);
using IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(hostname, gatewayHostname, authenticationMethod);

// Make sure client options is initialized with the correct transport setting.
EnsureOptionsIsSetup(connectionStringBuilder.Certificate, ref options);
Expand Down Expand Up @@ -364,7 +366,7 @@ internal static InternalClient CreateFromConnectionString(
throw new ArgumentOutOfRangeException(nameof(connectionString), "Must specify at least one TransportSettings instance");
}

var builder = IotHubConnectionStringBuilder.CreateWithIAuthenticationOverride(
using var builder = IotHubConnectionStringBuilder.CreateWithIAuthenticationOverride(
connectionString,
authenticationMethod);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH
throw new ArgumentNullException(nameof(iotHubConnectionStringBuilder));
}

iotHubConnectionStringBuilder.DeviceId = this.DeviceId;
iotHubConnectionStringBuilder.SharedAccessKey = this.KeyAsBase64String;
iotHubConnectionStringBuilder.DeviceId = DeviceId;
iotHubConnectionStringBuilder.SharedAccessKey = KeyAsBase64String;
iotHubConnectionStringBuilder.SharedAccessKeyName = null;
iotHubConnectionStringBuilder.SharedAccessSignature = null;

Expand Down Expand Up @@ -106,5 +106,11 @@ private void SetDeviceId(string deviceId)

_deviceId = deviceId;
}

/// <inheritdoc />

public void Dispose()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public string PolicyName
set => SetPolicyName(value);
}

/// <inheritdoc />

public void Dispose()
{
}

/// <summary>
/// Populates an <see cref="IotHubConnectionStringBuilder"/> instance based on the properties of the current instance.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions iothub/device/src/DeviceAuthenticationWithToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public string Token
set => SetToken(value);
}

/// <inheritdoc />

public void Dispose()
{
}

/// <summary>
/// Populates an <see cref="IotHubConnectionStringBuilder"/> instance based on the properties of the current instance.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions iothub/device/src/DeviceAuthenticationWithX509Certificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public string DeviceId
/// </summary>
public X509Certificate2Collection ChainCertificates { get; }

/// <inheritdoc />

public void Dispose()
{
#if !NET451
Certificate?.Dispose();
Certificate = null;
#endif
}

/// <summary>
/// Populates an <see cref="IotHubConnectionStringBuilder"/> instance based on the properties of the current instance.
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion iothub/device/src/IAuthenticationMethod.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace Microsoft.Azure.Devices.Client
{
/// <summary>
/// Authentication interface to use for device communications.
/// </summary>
public interface IAuthenticationMethod
public interface IAuthenticationMethod : IDisposable
{
/// <summary>
/// Populates an <see cref="IotHubConnectionStringBuilder"/> instance based on the properties of the current instance.
Expand Down
15 changes: 14 additions & 1 deletion iothub/device/src/IotHubConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Azure.Devices.Client
/// <summary>
/// Builds a connection string for the IoT Hub service based on the properties populated by the user.
/// </summary>
public sealed class IotHubConnectionStringBuilder
public sealed class IotHubConnectionStringBuilder : IDisposable
{
private const char ValuePairDelimiter = ';';
private const char ValuePairSeparator = '=';
Expand Down Expand Up @@ -408,5 +408,18 @@ private static bool ParseX509(string input, bool ignoreCase, out bool usingX509C
// Always returns true, but must return a bool because it is used in a delegate that requires that return.
return true;
}

/// <summary>
/// Dispose necessary objects.
/// </summary>
public void Dispose()
{
#if !NET451
Certificate?.Dispose();
Certificate = null;
#endif
AuthenticationMethod?.Dispose();
AuthenticationMethod = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,11 @@ private void SetModuleId(string moduleId)

_moduleId = moduleId;
}

/// <inheritdoc />

public void Dispose()
{
}
}
}
6 changes: 6 additions & 0 deletions iothub/device/src/ModuleAuthenticationWithToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public string Token
set => SetToken(value);
}

/// <inheritdoc />

public void Dispose()
{
}

/// <summary>
/// Populates an <see cref="IotHubConnectionStringBuilder"/> instance based on the properties of the current instance.
/// </summary>
Expand Down

0 comments on commit 7324a53

Please sign in to comment.