Esempio n. 1
0
        static async Task Main()
        {
            SetWindowSize();

            Uri adtInstanceUrl;

            try
            {
                // Read configuration data from the
                IConfiguration config = new ConfigurationBuilder()
                                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
                                        .Build();
                adtInstanceUrl = new Uri(config["instanceUrl"]);
            }
            catch (Exception ex) when(ex is FileNotFoundException || ex is UriFormatException)
            {
                Log.Error($"Could not read configuration. Have you configured your ADT instance URL in appsettings.json?\n\nException message: {ex.Message}");
                return;
            }

            Log.Ok("Authenticating...");
            var credential = new DefaultAzureCredential();

            client = new DigitalTwinsClient(adtInstanceUrl, credential);

            Log.Ok($"Service client created – ready to go");

            var CommandLoopInst = new CommandLoop(client);
            await CommandLoopInst.CliCommandInterpreter();
        }
Esempio n. 2
0
        static async Task Main()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                int width  = Math.Min(Console.LargestWindowWidth, 150);
                int height = Math.Min(Console.LargestWindowHeight, 40);
                Console.SetWindowSize(width, height);
            }
            try
            {
                // Read configuration data from the
                IConfiguration config = new ConfigurationBuilder()
                                        .AddJsonFile("serviceConfig.json", false, true)
                                        .Build();
                clientId       = config["clientId"];
                tenantId       = config["tenantId"];
                adtInstanceUrl = config["instanceUrl"];
            } catch (Exception e)
            {
                Log.Error($"Could not read service configuration file serviceConfig.json");
                Log.Alert($"Please copy serviceConfig.json.TEMPLATE to serviceConfig.json");
                Log.Alert($"and edit to reflect your service connection settings.");
                Log.Alert($"Make sure that 'Copy always' or 'Copy if newer' is set for serviceConfig.json in VS file properties");
                Environment.Exit(0);
            }

            Log.Ok("Authenticating...");
            try
            {
                var credential = new InteractiveBrowserCredential(tenantId, clientId);
                client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);
                // force authentication to happen here
                try
                {
                    client.GetDigitalTwin("---");
                }
                catch (RequestFailedException rex)
                {
                }
                catch (Exception e)
                {
                    Log.Error($"Authentication or client creation error: {e.Message}");
                    Log.Alert($"Have you checked that the configuration in serviceConfig.json is correct?");
                    Environment.Exit(0);
                }
            } catch (Exception e)
            {
                Log.Error($"Authentication or client creation error: {e.Message}");
                Log.Alert($"Have you checked that the configuration in serviceConfig.json is correct?");
                Environment.Exit(0);
            }

            Log.Ok($"Service client created – ready to go");

            CommandLoop CommandLoopInst = new CommandLoop(client);
            await CommandLoopInst.CliCommandInterpreter();
        }
Esempio n. 3
0
        static async Task Main()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                int width  = Math.Min(Console.LargestWindowWidth, 150);
                int height = Math.Min(Console.LargestWindowHeight, 40);
                Console.SetWindowSize(width, height);
            }
            try
            {
                // Read configuration data from the
                IConfiguration config = new ConfigurationBuilder()
                                        .AddJsonFile("serviceConfig.json", true, true)
                                        .Build();
                //clientId = config["clientId"];
                tenantId       = config["tenantId"];
                adtInstanceUrl = config["instanceUrl"];
            } catch (Exception e)
            {
                Log.Error($"Could not read service configuration file serviceConfig.json");
                Log.Alert($"Please copy serviceConfig.json.TEMPLATE to serviceConfig.json");
                Log.Alert($"and edit to reflect your service connection settings");
                Environment.Exit(0);
            }

            Log.Ok("Authenticating...");
            try
            {
                //var credential = new InteractiveBrowserCredential(tenantId, clientId);
                var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions()
                {
                    SharedTokenCacheTenantId = tenantId
                });
                client = new DigitalTwinsClient(new Uri(adtInstanceUrl), credential);


                //var tokenProvider = new AzureServiceTokenProvider("RunAs=Developer; DeveloperTool=AzureCli");
                //TokenCredentials tk = new TokenCredentials(await tokenProvider.GetAccessTokenAsync("0b07f429-9f4b-4714-9392-cc5e8e80c8b0"));
                //var t1 = await tokenProvider.GetAccessTokenAsync("0b07f429-9f4b-4714-9392-cc5e8e80c8b0");
                // force authentication to happen here
                // (for some reason this only happens on first call)
                try
                {
                    client.GetDigitalTwin("MilleniumF");
                }
                catch (RequestFailedException rex)
                {
                }
                catch (Exception e)
                {
                    Log.Error($"Authentication or client creation error: {e.Message}");
                    Environment.Exit(0);
                }
            } catch (Exception e)
            {
                Log.Error($"Authentication or client creation error: {e.Message}");
                Environment.Exit(0);
            }

            Log.Ok($"Service client created – ready to go");

            CommandLoop CommandLoopInst = new CommandLoop(client);
            await CommandLoopInst.CliCommandInterpreter();
        }