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

Posted

技术标签:

【中文标题】Azure IoThub - 类 RegistryManager - 方法 AddDeviceAsync(device) 抛出 ErrorCode:ArgumentNull;BadRequest【英文标题】:Azure IoThub - Class RegistryManager - Method AddDeviceAsync(device) throws ErrorCode:ArgumentNull;BadRequest 【发布时间】:2018-08-30 17:57:38 【问题描述】:

AddDeviceAsync 方法的注册表抛出异常 ArgumentNull。 参数、registryManager和输出参数notjing为null但还是抛出异常。

异常:"Message":"ErrorCode:ArgumentNull;BadRequest","ExceptionMessage":"Tracking ID:adf7e83e7db046969086702500cbe73b-G:2-TimeStamp:03/21/2018 14:09:50" 方法:ProjectXXX。 NTB.FN.DeviceRegistration.IdentityCreationService.CreateDeviceIdentity() 说明:意外异常

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

代码

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;

堆栈跟踪 System.ArgumentException: 在 Microsoft.Azure.Devices.HttpClientHelper+d__36.MoveNext(Microsoft.Azure.Devices,版本=1.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35) 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089) 在 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,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089) 在 ProjectXXX.NTB.FN.DeviceRegistration.IdentityCreationService+d__6.MoveNext(ProjectXXX.NTB.FN.DeviceRegistration,版本=1.0.0.0,文化=中性,PublicKeyToken=nullProjectXXX.NTB.FN.DeviceRegistration,版本=1.0.0.0,文化=中性,PublicKeyToken=null:D:\ProjectXXX\ProjectXXX.NTB\ProjectXXX.NTB\DeviceRegistrationLib\ProjectXXX.NTB.FN.DeviceRegistration\IdentityCreationService.csProjectXXX.NTB.FN.DeviceRegistration,版本=1.0.0.0,文化=中性, PublicKeyToken=null: 122)

【问题讨论】:

【参考方案1】:

当我将 Microsoft.Azure.Devices 从 1.5.1 降级到 1.3.2 时它起作用了

【讨论】:

【参考方案2】:

有效的 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;
    

【讨论】:

感谢您的回复。我发现此代码适用于 Microsoft.Azure.Devices 1.3.2 的解决方案【参考方案3】:

通过将 Microsoft.Azure.Devices 从 1.5.1 降级到 1.3.2 它应该可以工作

【讨论】:

以上是关于Azure IoThub - 类 RegistryManager - 方法 AddDeviceAsync(device) 抛出 ErrorCode:ArgumentNull;BadRequest的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Azure 流分析中通过 IotHub.ConnectionDeviceId 对延迟函数进行分区?

Azure IoTHub:获取连接设备的 IP 地址/位置

面对 ImportError:DLL 加载失败,同时实现 Microsoft azure iothub_service_client

azure iothub create-device-identity样例报错: unable to find valid certification path

Azure 事件中心 Event Grid(事件网格)+Azure Functions处理IOT Hub中的消息

Azure IoT Hub 十分钟入门系列 - 实现从设备上传日志文件/图片到 Azure Storage