-2

The name 'DeviceId' Does not exist in the current context on Main Program CSharp file.

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();
        }


    }
}

What am imissing here exactly? deviceId on method RunSampleAsync(string deviceId), so how does this string object be outside method definition? How do i fix this error from that line of ; sample.RunSampleAsync(deviceId).GetAwaiter().GetResult();?

Gcobza
  • 432
  • 1
  • 5
  • 12

1 Answers1

0

The problem is not with the the RunSampleAsync method definition. (It receives a deviceId parameter, and can use this parameter within the function)

In your Main function, you are passing in the deviceId variable into RunSampleAsync. However, you have not declared deviceId within Main (or the class, Program). This is the reason why the error states, "deviceId does not exist in the current context"

I'm assuming that the deviceId is supposed to be read from args. If so, before you write sample.RunSampleAsync(deviceId), you will need to do something along the lines of:

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();
Kei
  • 1,026
  • 8
  • 15
  • devideId takes a string argument on my ServiceClient.class on RunSampleAsync method. If i am passing it as a string, this object should be visible from the scope on my Main class object. What am i missing for passing this object on my Main class? – Gcobza Sep 09 '19 at 09:22
  • You are passing it in from Main to ServiceClient. Therefore, you can use it in ServiceClient, but you cannot use it in Main. Think of it this way. ServiceClient receives the parameter from Main. But Main needs to get it (or define it) somewhere. Main cannot access the deviceId based on it being defined in RunSampleAsync. If that were possible, it would lead to an unresolvable, circular reference (RunSampleAsync is given deviceId by Main which gets deviceId from RunSampleAsync's parameter which is given by Main which is..., etc.) – Kei Sep 09 '19 at 09:44
  • Basically, RunSampleAsync gets the deviceId from its caller (in this case, Main). But where does Main get deviceId from? – Kei Sep 09 '19 at 09:47
  • Ok i had work around to this by declaring the object on Main Class var string = args[0]; – Gcobza Sep 09 '19 at 10:11