Skip to content

Latest commit

History

74 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Hosting

Library to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations.

Installation

The library is available via NuGet packages:

NuGetDescriptionVersion
SimpleSoft.Hosting.Abstractionsinterfaces and abstract implementations (IHost, IHostBuilder, ...)NuGet
SimpleSoft.Hostinglibrary implementation that typically is only known by the main projectNuGet

Package Manager

Install-Package SimpleSoft.Hosting.Abstractions
Install-Package SimpleSoft.Hosting

.NET CLI

dotnet add package SimpleSoft.Hosting.Abstractions
dotnet add package SimpleSoft.Hosting

Packet CLI

paket add package SimpleSoft.Hosting.Abstractions
paket add package SimpleSoft.Hosting

Compatibility

This 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;

Usage

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;}}}

About

Library to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations

Topics

Resources

Stars

15 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages