https://github.com/simplesoft-pt/hosting
Library to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations
https://github.com/simplesoft-pt/hosting
csharp dotnet dotnet-core netcore
Last synced: 12 months ago
JSON representation
Library to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations
- Host: GitHub
- URL: https://github.com/simplesoft-pt/hosting
- Owner: simplesoft-pt
- License: mit
- Created: 2017-09-16T22:58:19.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-24T13:48:43.000Z (about 8 years ago)
- Last Synced: 2025-06-11T04:11:08.380Z (about 1 year ago)
- Topics: csharp, dotnet, dotnet-core, netcore
- Language: C#
- Size: 79.1 KB
- Stars: 15
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://www.nuget.org/packages?q=SimpleSoft.Hosting) packages:
| NuGet | Description | Version |
| --- | --- | --- |
| [SimpleSoft.Hosting.Abstractions](https://www.nuget.org/packages/simplesoft.hosting.abstractions) | interfaces and abstract implementations (`IHost`, `IHostBuilder`, ...) | [](https://www.nuget.org/packages/simplesoft.hosting.abstractions) |
| [SimpleSoft.Hosting](https://www.nuget.org/packages/simplesoft.hosting) | library implementation that typically is only known by the main project | [](https://www.nuget.org/packages/simplesoft.hosting) |
### Package Manager
```powershell
Install-Package SimpleSoft.Hosting.Abstractions
Install-Package SimpleSoft.Hosting
```
### .NET CLI
```powershell
dotnet add package SimpleSoft.Hosting.Abstractions
dotnet add package SimpleSoft.Hosting
```
### Packet CLI
```powershell
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](https://github.com/simplesoft-pt/Hosting/wiki) or you can check the [working examples](https://github.com/simplesoft-pt/Hosting/tree/master/SimpleSoft.Hosting/SimpleSoft.Hosting.Example) or [test](https://github.com/simplesoft-pt/Hosting/tree/master/test) code.
Here is an example of a console application:
```csharp
public class Program
{
private static readonly CancellationTokenSource TokenSource;
static Program()
{
TokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (sender, eventArgs) =>
{
TokenSource.Cancel();
eventArgs.Cancel = true;
};
}
public static void Main(string[] args) =>
MainAsync(args, TokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();
private static async Task MainAsync(string[] args, CancellationToken ct)
{
var loggerFactory = new LoggerFactory()
.AddConsole(LogLevel.Trace, true);
var logger = loggerFactory.CreateLogger();
logger.LogInformation("Application started");
try
{
using (var hostBuilder = new HostBuilder("ASPNETCORE_ENVIRONMENT")
.UseLoggerFactory(loggerFactory)
.UseStartup()
.ConfigureConfigurationBuilder(p => p.Builder.AddCommandLine(args)))
{
await hostBuilder.RunHostAsync(ct);
}
}
catch (TaskCanceledException)
{
logger.LogWarning("Application was terminated by user request");
}
catch (Exception e)
{
logger.LogCritical(0, e, "Unexpected exception");
}
finally
{
logger.LogInformation("Application terminated. Press to exit...");
Console.ReadLine();
}
}
private class Startup : HostStartup
{
public override void ConfigureConfigurationBuilder(IConfigurationBuilderParam param)
{
param.Builder
.SetBasePath(param.Environment.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{param.Environment.Name}.json", true, true)
.AddEnvironmentVariables();
}
public override IServiceProvider BuildServiceProvider(IServiceProviderBuilderParam param)
{
var container = new Autofac.ContainerBuilder();
container.Populate(param.ServiceCollection);
return new AutofacServiceProvider(container.Build());
}
}
private class Host : IHost
{
private readonly IHostingEnvironment _env;
private readonly IConfigurationRoot _configurationRoot;
private readonly ILogger _logger;
public Host(IHostingEnvironment env, IConfigurationRoot configurationRoot, ILogger logger)
{
_env = env;
_configurationRoot = configurationRoot;
_logger = logger;
}
public Task RunAsync(CancellationToken ct)
{
_logger.LogDebug("Running host...");
return Task.CompletedTask;
}
}
}
```