-1

I dont follow as to why i am getting this system exception error on my Program class

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

What exactly am i missing on my connectionString? As indicated on json-template file. I do have existing connectionString. OnCreateFromConnectionString method. Isnt this method has to launch that json-template? What is it that it doesnt see? Please assist me thanks.

Gcobza
  • 432
  • 1
  • 5
  • 12
  • Seems like `s_deviceConnectionString ` isn´t what you expect. Debug your code to see its value. – MakePeaceGreatAgain Sep 09 '19 at 13:09
  • I have, the code gets terminated on DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(s_deviceConnectionString, s_TransportType). on stack trace its null and my transportType is Amqp. – Gcobza Sep 09 '19 at 13:12
  • 1
    Your inital if seems a bit dodgy, if it's null or empty and there is no args, it will still be null or empty – Icepickle Sep 09 '19 at 13:12
  • How hould we know **why** `s_deviceConnectionString` is null? Obviously neither the env-setting is set, nor did you provide any args to the process making `args[0]` being null also. – MakePeaceGreatAgain Sep 09 '19 at 13:14
  • Well inside my if statement as shown, i doing the checking if its null and assigned arg value[0]. What could have gone wrong? – Gcobza Sep 09 '19 at 13:15
  • you are checking if it is null, but you will only assign it in case there are actually arguments supplied, when not, your program will fail again. How are you calling the program in question, and where is your settings file in relation to your executable? – Icepickle Sep 10 '19 at 09:00
  • HimBromBeere i did set up on my json file IOTHUB_CONN-STRING. that file as far i understand needs to launch if on my main class calling deviceconnectionString path.? am i wrong – Gcobza Sep 10 '19 at 09:47

1 Answers1

0

You probably have no Arguments at the beginning. Change it so "if(args.Length > 0)" is inside the "if(string.IsNullOrEmpty(...)" statement

EDIT: I'm sorry, i switched both

if(args.Length > 0)
{
    if(string.IsNullOrEmpty(s_deviceConnectionString))
    {
        s_deviceConnectionString = args[0];
    }
}
DudeWhoWantsToLearn
  • 751
  • 2
  • 10
  • 29