Library to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations.
The library is available via NuGet packages:
| NuGet | Description | Version |
|---|---|---|
| SimpleSoft.Hosting.Abstractions | interfaces and abstract implementations (IHost, IHostBuilder, ...) | |
| SimpleSoft.Hosting | library implementation that typically is only known by the main project |
Install-Package SimpleSoft.Hosting.Abstractions
Install-Package SimpleSoft.Hostingdotnet add package SimpleSoft.Hosting.Abstractions
dotnet add package SimpleSoft.Hostingpaket add package SimpleSoft.Hosting.Abstractions
paket add package SimpleSoft.HostingThis library is compatible with the following frameworks:
- SimpleSoft.Hosting.Abstractions
- .NET Standard 1.1;
- SimpleSoft.Hosting
- .NET Framework 4.5.1;
- .NET Standard 1.3;
- .NET Standard 1.5;
Documentation is available via wiki or you can check the working examples or test code.
Here is an example of a console application:
publicclassProgram{privatestaticreadonlyCancellationTokenSourceTokenSource;staticProgram(){TokenSource=newCancellationTokenSource();Console.CancelKeyPress+=(sender,eventArgs)=>{TokenSource.Cancel();eventArgs.Cancel=true;};}publicstaticvoidMain(string[]args)=>MainAsync(args,TokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();privatestaticasyncTaskMainAsync(string[]args,CancellationTokenct){varloggerFactory=newLoggerFactory().AddConsole(LogLevel.Trace,true);varlogger=loggerFactory.CreateLogger<Program>();logger.LogInformation("Application started");try{using(varhostBuilder=newHostBuilder("ASPNETCORE_ENVIRONMENT").UseLoggerFactory(loggerFactory).UseStartup<Startup>().ConfigureConfigurationBuilder(p =>p.Builder.AddCommandLine(args))){awaithostBuilder.RunHostAsync<Host>(ct);}}catch(TaskCanceledException){logger.LogWarning("Application was terminated by user request");}catch(Exceptione){logger.LogCritical(0,e,"Unexpected exception");}finally{logger.LogInformation("Application terminated. Press <enter> to exit...");Console.ReadLine();}}privateclassStartup:HostStartup{publicoverridevoidConfigureConfigurationBuilder(IConfigurationBuilderParamparam){param.Builder.SetBasePath(param.Environment.ContentRootPath).AddJsonFile("appsettings.json",true,true).AddJsonFile($"appsettings.{param.Environment.Name}.json",true,true).AddEnvironmentVariables();}publicoverrideIServiceProviderBuildServiceProvider(IServiceProviderBuilderParamparam){varcontainer=newAutofac.ContainerBuilder();container.Populate(param.ServiceCollection);returnnewAutofacServiceProvider(container.Build());}}privateclassHost:IHost{privatereadonlyIHostingEnvironment_env;privatereadonlyIConfigurationRoot_configurationRoot;privatereadonlyILogger<Host>_logger;publicHost(IHostingEnvironmentenv,IConfigurationRootconfigurationRoot,ILogger<Host>logger){_env=env;_configurationRoot=configurationRoot;_logger=logger;}publicTaskRunAsync(CancellationTokenct){_logger.LogDebug("Running host...");returnTask.CompletedTask;}}}