public ConsulGrpcHost(IGrpcHost host, string consulAddress = "localhost", int consulPort = 8500, string datacenter = "dc1") { _host = host; _consulAddress = consulAddress; _consulPort = consulPort; _datacenter = datacenter; }
private static async Task RunAsync(this IGrpcHost host, CancellationToken token = default(CancellationToken)) { if (token.CanBeCanceled) { await host.RunAsync(token, null); return; } var done = new ManualResetEventSlim(false); using (var cts = new CancellationTokenSource()) { AttachCtrlcSigtermShutdown(cts, done, "Application is shutting down..."); await host.RunAsync(cts.Token, "Application started. Press Ctrl+C to shut down."); done.Set(); } }
private static async Task WaitForTokenShutdownAsync(this IGrpcHost host, CancellationToken token) { var applicationLifetime = host.Services.GetService <IApplicationLifetime>(); token.Register(state => { ((IApplicationLifetime)state).StopApplication(); }, applicationLifetime); var waitForStop = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously); applicationLifetime.ApplicationStopping.Register(obj => { var tcs = (TaskCompletionSource <object>)obj; tcs.TrySetResult(null); }, waitForStop); await waitForStop.Task; // ReSharper disable once MethodSupportsCancellation await host.StopAsync(); }
private static async Task RunAsync(this IGrpcHost host, CancellationToken token, string shutdownMessage) { using (host) { host.Start(); var hostingEnvironment = host.Services.GetService <IHostingEnvironment>(); Console.WriteLine($"Hosting environment: {hostingEnvironment.EnvironmentName}"); Console.WriteLine($"Content root path: {hostingEnvironment.ContentRootPath}"); foreach (var port in host.Server.Ports) { Console.WriteLine($"Now listening on: {port.Host}:{port.Port}"); } if (!string.IsNullOrEmpty(shutdownMessage)) { Console.WriteLine(shutdownMessage); } await host.WaitForTokenShutdownAsync(token); } }
public ConsulGrpcHost(IGrpcHost host, string consulAddress) : this(host, consulAddress, 8500) { }
public static void Run(this IGrpcHost host) { host.RunAsync().GetAwaiter().GetResult(); }