C# System.ArgumentNullException:';值不能为null。参数名称:connectionString';

C# System.ArgumentNullException:';值不能为null。参数名称:connectionString';,c#,azure-iot-hub,azure-iot-sdk,C#,Azure Iot Hub,Azure Iot Sdk,我不明白为什么在我的程序类中会出现这个系统异常错误 launchSetting.json { "profiles": { "TwinDeviceApp": { "commandName": "Project", "environmentVariables": { "IOTHUB_DEVICE-CONN_STRING": "HostName=eNtsaIOTHubs.azure-devices.net;DeviceId=eNtsaDeviceId

我不明白为什么在我的程序类中会出现这个系统异常错误

launchSetting.json

{
  "profiles": {
    "TwinDeviceApp": {
      "commandName": "Project",
      "environmentVariables": {
        "IOTHUB_DEVICE-CONN_STRING": "HostName=eNtsaIOTHubs.azure-devices.net;DeviceId=eNtsaDeviceId;SharedAccessKey=****="
      }
    }
  }
}


Main class

using System;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Azure.Devices.Client;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwinDeviceApplication
{
    class Program
    {

        private static string s_deviceConnectionString = Environment.GetEnvironmentVariable("IOTHUB_DEVICE-CONN_STRING");
        private static TransportType s_transportType = TransportType.Amqp;
        public static int Main(string[] args)
        {
            if(string.IsNullOrEmpty(s_deviceConnectionString) && args.Length > 0)
            {
                s_deviceConnectionString = args[0];
            }
            DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(s_deviceConnectionString, s_transportType);

            if(deviceClient == null)
            {
                Console.WriteLine("Failed to create DeviceClient");
                return 1;
            }
            var sample = new TwinDeviceApp(deviceClient);
            sample.RunSampleAsync().GetAwaiter().GetResult();

            Console.WriteLine("Done.\n");
            return 0;
        }
    }
}

我的连接字符串到底缺少什么?如json模板文件所示。我确实有现有的连接字符串。OnCreateFromConnectionString方法。这个方法不是必须启动json模板吗?它看不到的是什么?请帮助我,谢谢。

你一开始可能没有争论。将其更改为“if(args.Length>0)”位于“if(string.IsNullOrEmpty(…)”语句中

编辑:对不起,我两个都换了

if(args.Length > 0)
{
    if(string.IsNullOrEmpty(s_deviceConnectionString))
    {
        s_deviceConnectionString = args[0];
    }
}

似乎
s_deviceConnectionString
不是您所期望的。调试您的代码以查看其值。我发现,代码在DeviceClient DeviceClient=DeviceClient.CreateFromConnectionString上终止(s_deviceConnectionString,s_TransportType)。在堆栈跟踪上,它的null和我的transportType是Amqp。您的初始if看起来有点可疑,如果它为null或空,并且没有参数,它仍然为null或空。我们如何知道为什么
s_deviceConnectionString
为null?显然既没有设置env设置,也没有为进程提供任何参数来生成
args[0]
也是null。在我的if语句中,如图所示,我正在检查其是否为null和分配的arg值[0]。可能出了什么问题?我仍然会遇到相同的错误,“value cannot be null”仍然没有更改,即使对两者都应用了开关,我还缺少什么?