Example #1
0
        public static T UseHttpApi <T>(this T kernel, StartupOptions startupOptions, HttpProbingSettings httpProbingSettings)
            where T : Kernel
        {
            var initApiCommand = new Command("#!enable-http")
            {
                IsHidden = true,
                Handler  = CommandHandler.Create((KernelInvocationContext context) =>
                {
                    if (context.Command is SubmitCode submitCode)
                    {
                        var probingUrls = httpProbingSettings != null
                            ? httpProbingSettings.AddressList
                            : new[]
                        {
                            new Uri($"http://localhost:{startupOptions.HttpPort}")
                        };
                        var html =
                            HttpApiBootstrapper.GetHtmlInjection(probingUrls, startupOptions.HttpPort?.ToString() ?? Guid.NewGuid().ToString("N"));
                        context.Display(html, "text/html");
                        context.Complete(submitCode);
                    }
                })
            };

            kernel.AddDirective(initApiCommand);

            return(kernel);
        }
Example #2
0
        public static IWebHostBuilder ConstructWebHostBuilder(
            StartupOptions options,
            IServiceCollection serviceCollection)
        {
            using var _ = Log.OnEnterAndExit();

            // FIX: (ConstructWebHostBuilder) dispose me
            var disposables = new CompositeDisposable
            {
                StartToolLogging(options)
            };

            HttpProbingSettings probingSettings = null;

            if (options.EnableHttpApi)
            {
                var httpPort = GetFreePort(options);
                options.HttpPort = httpPort;
                probingSettings  = HttpProbingSettings.Create(httpPort.PortNumber);
            }

            var webHost = new WebHostBuilder()
                          .UseKestrel()
                          .UseDotNetInteractiveHttpApi(options.EnableHttpApi, options.HttpPort, probingSettings, serviceCollection)
                          .UseStartup <Startup>();

            if (options.EnableHttpApi && probingSettings != null)
            {
                webHost = webHost.UseUrls(probingSettings.AddressList.Select(a => a.AbsoluteUri).ToArray());
            }

            return(webHost);
Example #3
0
        public static IWebHostBuilder ConstructWebHostBuilder(
            StartupOptions options,
            IServiceCollection serviceCollection)
        {
            // FIX: (ConstructWebHostBuilder) dispose me
            var disposables = new CompositeDisposable
            {
                StartToolLogging(options)
            };

            var httpPort = options.HttpPort ??= GetFreePort(options.HttpPortRange);

            var probingSettings = HttpProbingSettings.Create(httpPort);

            var webHost = new WebHostBuilder()
                          .UseKestrel()
                          .ConfigureServices(c =>
            {
                c.AddSingleton(probingSettings);
                c.AddSingleton(options);
                foreach (var serviceDescriptor in serviceCollection)
                {
                    c.Add(serviceDescriptor);
                }
            })
                          .UseStartup <Startup>()
                          .UseUrls(probingSettings.AddressList.Select(a => a.AbsoluteUri).ToArray());

            return(webHost);

            int GetFreePort(PortRange httpPortRange = null)
            {
                var currentPort = httpPortRange?.Start ?? 0;
                var endPort     = httpPortRange?.End ?? 0;

                for (; currentPort <= endPort; currentPort++)
                {
                    try
                    {
                        var l = new TcpListener(IPAddress.Loopback, currentPort);
                        l.Start();
                        var port = ((IPEndPoint)l.LocalEndpoint).Port;
                        l.Stop();
                        return(port);
                    }
                    catch (SocketException)
                    {
                    }
                }

                throw new InvalidOperationException("Cannot find a port");
            }
        }
Example #4
0
        public static IWebHostBuilder ConstructWebHostBuilder(
            StartupOptions options,
            IServiceCollection serviceCollection)
        {
            using var _ = Log.OnEnterAndExit();

            // FIX: (ConstructWebHostBuilder) dispose me
            var disposables = new CompositeDisposable
            {
                StartToolLogging(options)
            };

            HttpProbingSettings probingSettings = null;

            if (options.EnableHttpApi)
            {
                var httpPort = GetFreePort(options);
                probingSettings = HttpProbingSettings.Create(httpPort.PortNumber);
            }

            var webHost = new WebHostBuilder()
                          .UseKestrel()
                          .ConfigureServices(c =>
            {
                if (options.EnableHttpApi && probingSettings != null)
                {
                    c.AddSingleton(probingSettings);
                }
                c.AddSingleton(options);
                foreach (var serviceDescriptor in serviceCollection)
                {
                    c.Add(serviceDescriptor);
                }
            })
                          .UseStartup <Startup>();

            if (options.EnableHttpApi && probingSettings != null)
            {
                webHost = webHost.UseUrls(probingSettings.AddressList.Select(a => a.AbsoluteUri).ToArray());
            }

            return(webHost);
Example #5
0
        public static IWebHostBuilder ConstructWebHostBuilder(
            StartupOptions options,
            IServiceCollection serviceCollection)
        {
            // FIX: (ConstructWebHostBuilder) dispose me
            var disposables = new CompositeDisposable
            {
                StartToolLogging(options)
            };

            var httpPort = options.HttpPort ??= GetFreePort();

            var probingSettings = HttpProbingSettings.Create(httpPort);

            var webHost = new WebHostBuilder()
                          .UseKestrel()
                          .ConfigureServices(c =>
            {
                c.AddSingleton(probingSettings);
                c.AddSingleton(options);
                foreach (var serviceDescriptor in serviceCollection)
                {
                    c.Add(serviceDescriptor);
                }
            })
                          .UseStartup <Startup>()
                          .UseUrls(probingSettings.AddressList.Select(a => a.AbsoluteUri).ToArray());

            return(webHost);

            int GetFreePort()
            {
                var l = new TcpListener(IPAddress.Loopback, 0);

                l.Start();
                var port = ((IPEndPoint)l.LocalEndpoint).Port;

                l.Stop();
                return(port);
            }
        }