当前上下文中不存在名称 deviceId

The name deviceId does not exist in the current context

主程序 CSharp 文件的当前上下文中不存在名称 'DeviceId'。

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

namespace ServiceClientApplication
{
    class Program
    {
        private static string s_connectionString = Environment.GetEnvironmentVariable("IOTHUB_CONN_STRING_CSharp");

        private static TransportType s_transportType = TransportType.Amqp;

        public static int Main(string[] args)
        {

            if(args.Length < 1)
            {
                Console.WriteLine("\nUsage:\n");
                Console.WriteLine("\tServiceClientApplication <deviceID> [connectionString]\n");
                return 1;
            }
            if(string.IsNullOrEmpty(s_connectionString) && args.Length > 1)
            {
                s_connectionString = args[1];
            }

            ServiceClient serviceClient = ServiceClient.CreateFromConnectionString(s_connectionString, s_transportType);

            var sample = new ServiceClient(serviceClient);
            sample.RunSampleAsync(deviceId).GetAwaiter().GetResult();

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


        }
    }
}

ServiceClient.cs file has the following methods.

using Microsoft.Azure.Devices.Client;
using System;
using System.Text;
using System.Threading.Tasks;

namespace ServiceClientApplication
{
    public class ServiceClient
    {
        private readonly ServiceClient serviceClient;

        public ServiceClient(ServiceClient serviceClient)
        {
            serviceClient = serviceClient ?? throw new ArgumentNullException(nameof(serviceClient));
        }
        public async Task RunSampleAsync(string deviceId)
        {
            var str = "Testing eNtsa Project";
            var message = new Message(Encoding.ASCII.GetBytes(str));
            await serviceClient.SendAsync(deviceId, message).ConfigureAwait(false);
        }

        private Task SendAsync(string deviceId, object message)
        {
            throw new NotImplementedException();
        }

        internal static ServiceClient CreateFromConnectionString(string s_connectionString, TransportType s_transportType)
        {
            throw new NotImplementedException();
        }


    }
}

这里究竟遗漏了什么?方法 RunSampleAsync(string deviceId) 上的 deviceId,那么这个字符串对象如何在方法定义之外呢?我如何从该行修复此错误; sample.RunSampleAsync(deviceId).GetAwaiter().GetResult();?

问题不在于 RunSampleAsync 方法定义。 (它接收一个deviceId参数,可以在函数内部使用这个参数)

在 Main 函数中,您将 deviceId 变量传递给 RunSampleAsync。但是,您尚未在 Main(或 class、程序)中声明 deviceId。这就是错误状态 "deviceId does not exist in the current context"

的原因

我假设应该从 args 中读取 deviceId。如果是这样,在您编写 sample.RunSampleAsync(deviceId) 之前,您需要按照以下方式做一些事情:

var deviceId = args[<replace with appropriate index>];

// Now that we have declared deviceId, we can pass it into RunSampleAsysc
var sample = new ServiceClient(serviceClient);
sample.RunSampleAsync(deviceId).GetAwaiter().GetResult();