使用 IoT DPS 通过 CreateOrUpdateIndividualEnrollmentAsync 更新设备时出错

Error when updating device via CreateOrUpdateIndividualEnrollmentAsync with IoT DPS

当使用以下代码更新已注册 IoT DPS 的现有设备时,出现以下错误:

Executed 'MyFunction' (Failed, Id=xxx) System.Private.CoreLib: Exception while executing function: MyFunction. Microsoft.Azure.Devices.Provisioning.Service: Conflict:

{"errorCode":409201,"trackingId":"xxx","message":"Enrollment already exists.","timestampUtc":"2019-08-07T16:27:23.3403783Z"}.

Microsoft.Azure.Devices.Provisioning.Service errorCode 409201 "Enrollment already exists."
private static async Task UpdateIndividualEnrollmentSymmetricKeyAsync()
            {
    var regId = GetRegistrationId("1", APP_NAME);
    var symmKey = GenerateSymmetricalKey(regId, _PROVISIONING_SHARED_KEY);

    Attestation attestation = new SymmetricKeyAttestation(symmKey, symmKey);
    var enrollment = new IndividualEnrollment(regId, attestation);

   //There are other properties, ProvisioningStatus below is only updated.
    enrollment.ProvisioningStatus = ProvisioningStatus.Enabled;

     //error occurred
    IndividualEnrollment enrollmentResult = await _provisioningServiceClient.CreateOrUpdateIndividualEnrollmentAsync(enrollment);

    }

看看这个sample如何更新现有的个人注册:

您应该在更新之前检索现有的个人注册,然后调用 CreateOrUpdateIndividualEnrollmentAsync