Esempio n. 1
0
        /// <inheritdoc />
        public async Task <bool> StartAsync(CancellationToken cancellationToken = default)
        {
            var started = false;

            if (_host == null)
            {
                using (await _control.LockAsync().ConfigureAwait(false))
                {
                    if (_host == null)
                    {
                        _host = GetHostBuilder().Build();

                        await _host.StartAsync(cancellationToken).ConfigureAwait(true);

                        Task = _host.WaitForShutdownAsync(cancellationToken).ContinueWith(t =>
                        {
                            _completionSource.SetResult(t.IsCompletedSuccessfully);

                            return(Task.IsCompleted);
                        });

                        started = true;
                    }
                }
            }

            return(started);
        }
Esempio n. 2
0
 public void Stop()
 {
     _server?.StopAsync().GetAwaiter().GetResult();
     _server?.WaitForShutdownAsync().GetAwaiter().GetResult();
     _server?.Dispose();
     _server = null;
 }
Esempio n. 3
0
        public static async Task <int> Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration)
                         .CreateLogger();

            try
            {
                Log.Information("Starting web host");
                IWebHost host = BuildWebHost(args);
                host.Start();
                await host.WaitForShutdownAsync();

                return(0);
            }
            catch (Exception e)
            {
                Log.Fatal(e, "{Class} -> {Method} -> Host terminated unexpectedly", nameof(Program), nameof(Main));

                return(1);
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }
Esempio n. 4
0
        private static void Start(bool isReload = false)
        {
            PluginLoader.LoadSettings();

            _host = WebHost
                    .CreateDefaultBuilder()
                    .UseKestrel()
                    .UseUrls("http://*:5006")
                    .ConfigureLogging((hostingContext, logging) =>
            {
                logging.ClearProviders();
                logging.AddConsole();
            })
                    .UseStartup <Startup>()
                    .Build();

            Task.Run(async() =>
            {
                await _host.StartAsync();
                await EventManager.CallAsync(new ServerStartedEvent());
                if (isReload)
                {
                    await EventManager.CallAsync(new ServerReloadedEvent());
                }
                await _host.WaitForShutdownAsync();
            });
        }
Esempio n. 5
0
        public Task StopAsync()
        {
            _cts.Cancel();

            IsStarted = false;
#if NETSTANDARD1_3
            return(Task.FromResult(true));
#else
            return(_host.WaitForShutdownAsync());
#endif
        }
Esempio n. 6
0
 public static Task StartListenerAsync(this IWebHost host, CancellationToken ct)
 {
     return(Task.Run(() =>
     {
         using (host)
         {
             host.StartAsync(ct);
             host.WaitForShutdownAsync(ct).GetAwaiter().GetResult();
         }
     }, ct));
 }
 public Task StopAsync(CancellationToken cancellationToken)
 {
     try
     {
         process.Kill();
     }
     catch
     {
     }
     webHost.WaitForShutdownAsync();
     return(Task.CompletedTask);
 }
Esempio n. 8
0
        public async Task RunAsync(CancellationToken cancellationToken = default)
        {
            using (_webHost)
            {
                await _webHost.RunAsync(cancellationToken);

                var serverAddresses = _webHost.ServerFeatures.Get <IServerAddressesFeature>()?.Addresses;
                if (serverAddresses != null)
                {
                    foreach (var address in serverAddresses)
                    {
                        Logger.LogInformation($"Now listening on: {address}");
                    }
                }
                await _webHost.WaitForShutdownAsync(cancellationToken);

                Logger.LogInformation("Stopping WebHookServer");
            }
        }
Esempio n. 9
0
                private static TaskCompletionSource <int> CreateTcs(CancellationTokenSource stopHostCts, IWebHost host, IConsole console, int runtimeInMinutes)
                {
                    var tcs = new TaskCompletionSource <int>();

                    if (runtimeInMinutes > 0)
                    {
                        // on runtime expiry, signal stopHostCts and tcs
                        _ = Task.Delay(TimeSpan.FromMinutes(runtimeInMinutes)).ContinueWith(
                            t =>
                        {
                            EGBenchLogger.WriteLine(console, $"--runtime-in-minutes ({runtimeInMinutes}) Minutes have passed, shutting down publishers.");
                            try
                            {
                                stopHostCts.Cancel();
                                tcs.TrySetResult(0);
                            }
                            catch (Exception ex)
                            {
                                tcs.TrySetException(ex);
                            }
                        },
                            TaskScheduler.Default);
                    }

                    // on host shutdown, signal tcs
                    _ = host.WaitForShutdownAsync().ContinueWith(
                        t =>
                    {
                        if (t.IsFaulted)
                        {
                            tcs.TrySetException(t.Exception);
                        }
                        else
                        {
                            tcs.TrySetResult(0);
                        }
                    },
                        TaskScheduler.Default);

                    return(tcs);
                }
Esempio n. 10
0
 /// <summary>
 /// Block the calling thread until shutdown is triggered via Ctrl+C or SIGTERM.
 /// </summary>
 /// <param name="host">The running <see cref="IWebHost"/>.</param>
 public static void WaitForShutdown(this IWebHost host)
 {
     host.WaitForShutdownAsync().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Block the calling thread until shutdown is triggered via Ctrl+C or SIGTERM.
 /// </summary>
 /// <param name="host">The running <see cref="T:Microsoft.AspNetCore.Hosting.IWebHost" />.</param>
 /// <param name="runSuccessCallback">启动成功的回调函数(Run the successful callback)</param>
 public static void WaitForShutdown(this IWebHost host, Action runSuccessCallback = null)
 {
     host.WaitForShutdownAsync(new CancellationToken(), runSuccessCallback).GetAwaiter().GetResult();
 }
Esempio n. 12
0
 public Task WaitForShutdownAsync() => _host.WaitForShutdownAsync();
Esempio n. 13
0
 /// <summary>
 /// 暂停
 /// </summary>
 /// <param name="hostControl"></param>
 /// <returns></returns>
 public bool Pause(HostControl hostControl)
 {
     //scheduler.PauseAll();
     webHost.WaitForShutdownAsync();
     return(true);
 }