System.ArgumentException:'应指定SharedAccessKeyName'

时间:2019-07-12 06:29:58

标签: c# azure-iot-hub azure-iot-dps

在创建预配客户端时,我从Azure IoT配给服务中收到此错误。

System.ArgumentException: 'Should specify SharedAccessKeyName'

我从门户网站复制了连接字符串,这可能是什么问题?

using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Provisioning.Service;

namespace EnrollmentApp
{
    class Program
    {
        private static string ProvisioningConnectionString = "HostName=happybeerhub-us.azure-devices.net;DeviceId=test-device-01;SharedAccessKey=tawpddfqUt3EHZg9a5tUzQ5fjros7zMhKsZbmuXzwXE=";
        private static string EnrollmentGroupId = "test";
        private static string X509RootCertPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"key.pfx");        

        static void Main(string[] args)
        {
            RunSample().GetAwaiter().GetResult();
            Console.WriteLine("\nHit <Enter> to exit ...");
            Console.ReadLine();
        }

        public static async Task RunSample()
        {
            Console.WriteLine("Starting sample...");

            using (ProvisioningServiceClient provisioningServiceClient =
                    ProvisioningServiceClient.CreateFromConnectionString(ProvisioningConnectionString))
            {
                #region Create a new enrollmentGroup config
                Console.WriteLine("\nCreating a new enrollmentGroup...");
                var certificate = new X509Certificate2(X509RootCertPath);
                Attestation attestation = X509Attestation.CreateFromRootCertificates(certificate);
                EnrollmentGroup enrollmentGroup =
                        new EnrollmentGroup(
                                EnrollmentGroupId,
                                attestation)
                        {
                            ProvisioningStatus = ProvisioningStatus.Enabled
                        };
                Console.WriteLine(enrollmentGroup);
                #endregion

                #region Create the enrollmentGroup
                Console.WriteLine("\nAdding new enrollmentGroup...");
                EnrollmentGroup enrollmentGroupResult =
                    await provisioningServiceClient.CreateOrUpdateEnrollmentGroupAsync(enrollmentGroup).ConfigureAwait(false);
                Console.WriteLine("\nEnrollmentGroup created with success.");
                Console.WriteLine(enrollmentGroupResult);
                #endregion

            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

好吧,您错过了SharedAccessKeyName,因此验证尝试对其进行检查,并且在您遇到的情况下为null会导致异常。

如您在ServiceConnectionStringBuilder.cs中所见,正确的连接字符串格式为

/// A valid connection string shall be in the following format:
/// <code>
/// HostName=[ServiceName];SharedAccessKeyName=[keyName];SharedAccessKey=[Key]
/// </code>

还在第128行中,您可以看到抛出异常的检查:

        if (string.IsNullOrWhiteSpace(SharedAccessKeyName))
        {
            throw new ArgumentException("Should specify SharedAccessKeyName");
        }

现在,您可能会认为代码未使用ServiceConnectionStringBuilder.cs,但是调用ProvisioningServiceClient.CreateFromConnectionString创建了一个新的ProvisioningServiceClient实例,该实例又调用了ServiceConnectionStringBuilder.Parse()。 有关更多信息,请参见第82、113和123行中的ProvisioningServiceClient.cs

解决方案:

要解决此问题,您必须提供一个SharedAccessKeyName。您还应该考虑为此使用IotHubConnectionStringBuilder,它在构建连接字符串时已经检查了您的属性。

发问者根据此博客文章here指出密钥名称是deviceId。因此,解决方案是使用SharedAccessKeyname=xx而不是DeviceId=xx