Beispiel #1
0
        private static async Task <SubscriberHost> SetupSubscriberHostAsync(CancellationTokenSource lifetimeCts)
        {
            using var securityDaemonClient = new SecurityDaemonClient();

            // get server certificate to configure with
            Console.WriteLine($"Configure server certificate");
            (X509Certificate2 serverCert, X509Certificate2[] certChain) =
                await securityDaemonClient.GetServerCertificateAsync().ConfigureAwait(false);

            CertificateHelper.ImportCertificate(serverCert);
            CertificateHelper.ImportIntermediateCAs(serverCert);
            CertificateHelper.ImportIntermediateCAs(certChain);

            // Configure client trust bundle
            Console.WriteLine($"Configure client trust bundle");
            var trustBundle = await securityDaemonClient.GetTrustBundleAsync();

            CertificateHelper.ImportIntermediateCAs(trustBundle);

            // start subscriber webhost
            SubscriberHost host = new SubscriberHost(serverCert, lifetimeCts);
            await host.StartAsync().ConfigureAwait(false);

            return(host);
        }
Beispiel #2
0
        public static async Task Main()
        {
            var resetEvent = new ManualResetEventSlim();

            // signals to long running components when to power down (either due to a Ctrl+C, or Ctrl-Break, or SIGTERM, or SIGKILL)
            CancellationTokenSource lifetimeCts = SetupGracefulShutdown(resetEvent);
            SubscriberHost          host        = SetupSubscriberHostAsync(lifetimeCts).GetAwaiter().GetResult();

            IConfigurationSection hostConfigurationSection = GetHostConfigurationSection();

            if (ShouldAutoCreateSubscription())
            {
                GridConfiguration   gridConfig = GetGridConfiguration();
                EventGridEdgeClient egClient   = GetEventGridClientAsync(gridConfig).GetAwaiter().GetResult();

                // certificate issued by IoT Edge takes a while to be current so will wait for a bit
                Thread.Sleep(120 * 1000);

                // wait for topic to exist
                await WaitUntilEventGridModuleIsUpAndTopicExistsAsync(egClient, gridConfig.Topic.Name).ConfigureAwait(false);

                // register subscription
                await RegisterSubscriptionAsync(egClient, gridConfig).ConfigureAwait(false);
            }


            // wait until shutdown
            await host.WaitForShutdownAsync().ConfigureAwait(false);

            // signal to gracefully shutdown
            resetEvent.Set();
        }