Azure IoThub - 类RegistryManager - 方法AddDeviceAsync(device)抛出ErrorCode:ArgumentNull; BadRequest

时间:2018-03-21 14:41:43

标签: azure azure-iot-hub

注册表抛出异常ArgumentNull的AddDeviceAsync方法。 参数,registryManager和输出参数notjing为null但它仍然抛出异常。

异常:{"消息":" ErrorCode:ArgumentNull; BadRequest"," ExceptionMessage":"跟踪ID:adf7e83e7db046969086702500cbe73b-G:2- TimeStamp:03/21/2018 14:09:50"}方法:ProjectXXX.NTB.FN.DeviceRegistration.IdentityCreationService.CreateDeviceIdentity()描述:意外异常

它是否与Microsoft.Azure.Devices库有关?

代码

HttpModule

堆栈跟踪 System.ArgumentException:    在Microsoft.Azure.Devices.HttpClientHelper + d__36.MoveNext(Microsoft.Azure.Devices,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35)    在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089)    在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089)    在Microsoft.Azure.Devices.HttpClientHelper + d__12 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; } 1.GetResult(mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089)    在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 个答案:

答案 0 :(得分:1)

有效的SymmetricKey必须是Base64编码的字符串,长度在16到64个字节之间。

我不知道你的deviceKey是什么类型的。我使用GUID并使用以下代码块进行测试,并且可以成功创建设备。

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 :(得分:1)

通过将Microsoft.Azure.Devices从1.5.1降级到1.3.2可以正常工作

答案 2 :(得分:0)

当我将Microsoft.Azure.Devices从1.5.1降级到1.3.2

时,它有效