Guest User

Untitled

a guest
Feb 1st, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Hosting;
  5. using Heloworld.Services;
  6.  
  7. namespace Heloworld
  8. {
  9.     class Program
  10.     {
  11.         static async Task Main()
  12.         {
  13.             var appCts = new CancellationTokenSource();
  14.             void stopApp() => appCts.Cancel();
  15.  
  16.             await new HostBuilder()
  17.                 .ConfigureServices(services =>
  18.                 {
  19.                     services.AddScoped(typeof(IHelloWorldService), typeof(HeloWorldService));
  20.                     services.AddHostedService(x => new HelloWorldConsoleService(x.GetService<IHelloWorldService>(), stopApp));
  21.                 })
  22.                 .UseConsoleLifetime(opts => opts.SuppressStatusMessages = true)
  23.                 .Build()
  24.                 .RunAsync(appCts.Token);
  25.         }
  26.     }
  27. }
Add Comment
Please, Sign In to add comment