2
votes

AddDeviceAsync method of registry throw exception ArgumentNull. Parameter, registryManager and output parameter notjing is null but it still throw exception.

Exception : {"Message":"ErrorCode:ArgumentNull;BadRequest","ExceptionMessage":"Tracking ID:adf7e83e7db046969086702500cbe73b-G:2-TimeStamp:03/21/2018 14:09:50"} Method: ProjectXXX.NTB.FN.DeviceRegistration.IdentityCreationService.CreateDeviceIdentity() Description: Unexpected exception

Is it issue related to Microsoft.Azure.Devices library ??

Code

RegistryManager registryManager = null;

void Initialize()
{
    registryManager = RegistryManager.CreateFromConnectionString(AppSetting.IoTHubConnectionString);
}

public async Task<string> CreateDeviceIdentity(string deviceId, string deviceKey)
{
    Device device = new Device(deviceId);
    Device newdevice =  new Device();

    Exception exception = null;
    bool bExceptionHasOccured = false;

    string token = string.Empty;
    string primarySasToken = Base64Encode(deviceKey);
    string secondarySasToken = Base64Encode($"{deviceId}-{deviceId}");
    device.Authentication = new AuthenticationMechanism
    {
        SymmetricKey = new SymmetricKey
        {
            PrimaryKey = primarySasToken,
            SecondaryKey = secondarySasToken
        }
    };
    try
    {
        newdevice = await registryManager.AddDeviceAsync(device);
        break;
    }
    catch (DeviceAlreadyExistsException)
    {
        token = GetDeviceToken(deviceId);
        break;
    }
    catch (IotHubThrottledException e)
    {

    }
    catch (SocketException e)
    {

    }
    catch (Exception e)
    {

    }

    token = newdevice.Authentication.SymmetricKey.PrimaryKey;
    return token;
}

Stack Trace System.ArgumentException: at Microsoft.Azure.Devices.HttpClientHelper+d__36.MoveNext (Microsoft.Azure.Devices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at Microsoft.Azure.Devices.HttpClientHelper+d__121.MoveNext (Microsoft.Azure.Devices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) at ProjectXXX.NTB.FN.DeviceRegistration.IdentityCreationService+d__6.MoveNext (ProjectXXX.NTB.FN.DeviceRegistration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullProjectXXX.NTB.FN.DeviceRegistration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\ProjectXXX\ProjectXXX.NTB\ProjectXXX.NTB\DeviceRegistrationLib\ProjectXXX.NTB.FN.DeviceRegistration\IdentityCreationService.csProjectXXX.NTB.FN.DeviceRegistration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 122)

3

3 Answers

2
votes

It worked when I downgraded Microsoft.Azure.Devices from 1.5.1 to 1.3.2

1
votes

A valid SymmetricKey is must be Base64 encoded string and between 16 and 64 bytes in length.

I've no idea of what kind of your deviceKey. I use a GUID and test with the following code piece and the device can be successfully created.

public static async Task<string> CreateDeviceIdentity(string deviceId)
    {
        Device device = new Device(deviceId);
        Device newdevice = new Device();

        string token = string.Empty;

        var primaryKey = Guid.NewGuid();
        var secondaryKey = Guid.NewGuid();

        byte[] bytes = Encoding.UTF8.GetBytes(primaryKey.ToString());
        string base64PrimaryKey = Convert.ToBase64String(bytes);

        bytes = Encoding.UTF8.GetBytes(secondaryKey.ToString());
        string base64SecondaryKey = Convert.ToBase64String(bytes);

        try
        {

            device.Authentication = new AuthenticationMechanism
            {
                SymmetricKey = new SymmetricKey
                {
                    PrimaryKey = base64PrimaryKey,
                    SecondaryKey = base64SecondaryKey
                }
            };

            newdevice = await registryManager.AddDeviceAsync(device);
        }
        catch (Exception ex)
        {

        }

        token = newdevice.Authentication.SymmetricKey.PrimaryKey;
        return token;
    }
1
votes

by downgrading the Microsoft.Azure.Devices from 1.5.1 to 1.3.2 it should work