我在创建预配客户端时从 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
}
}
}
}
好吧,您缺少SharedAccessKeyName
因此验证会尝试检查它,并且由于它在您的情况下为空,因此您会得到异常。
正如您在 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,它已经在构建连接字符串时检查了您的属性。
提问者指出,根据这篇博客文章,键名是deviceId。因此,解决方案是使用SharedAccessKeyname=xx
而不是DeviceId=xx
.