{"id":23334059,"url":"https://github.com/simplesoft-pt/hosting","last_synced_at":"2025-06-30T14:05:47.576Z","repository":{"id":75309663,"uuid":"103789085","full_name":"simplesoft-pt/Hosting","owner":"simplesoft-pt","description":"Library to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations","archived":false,"fork":false,"pushed_at":"2018-04-24T13:48:43.000Z","size":81,"stargazers_count":15,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-11T04:11:08.380Z","etag":null,"topics":["csharp","dotnet","dotnet-core","netcore"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simplesoft-pt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-16T22:58:19.000Z","updated_at":"2022-01-12T18:40:00.000Z","dependencies_parsed_at":"2023-07-10T08:00:20.898Z","dependency_job_id":null,"html_url":"https://github.com/simplesoft-pt/Hosting","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/simplesoft-pt/Hosting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FHosting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FHosting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FHosting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FHosting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplesoft-pt","download_url":"https://codeload.github.com/simplesoft-pt/Hosting/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FHosting/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262788709,"owners_count":23364396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","dotnet-core","netcore"],"created_at":"2024-12-21T00:37:33.784Z","updated_at":"2025-06-30T14:05:47.568Z","avatar_url":"https://github.com/simplesoft-pt.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hosting\nLibrary to simplify the hosting of console applications by making it easier to setup dependency injection, logging and configurations.\n\n## Installation\nThe library is available via [NuGet](https://www.nuget.org/packages?q=SimpleSoft.Hosting) packages:\n\n| NuGet | Description | Version |\n| --- | --- | --- |\n| [SimpleSoft.Hosting.Abstractions](https://www.nuget.org/packages/simplesoft.hosting.abstractions) | interfaces and abstract implementations (`IHost`, `IHostBuilder`, ...) | [![NuGet](https://img.shields.io/nuget/vpre/simplesoft.hosting.abstractions.svg)](https://www.nuget.org/packages/simplesoft.hosting.abstractions) |\n| [SimpleSoft.Hosting](https://www.nuget.org/packages/simplesoft.hosting) | library implementation that typically is only known by the main project | [![NuGet](https://img.shields.io/nuget/vpre/simplesoft.hosting.svg)](https://www.nuget.org/packages/simplesoft.hosting) |\n\n### Package Manager\n```powershell\nInstall-Package SimpleSoft.Hosting.Abstractions\nInstall-Package SimpleSoft.Hosting\n```\n\n### .NET CLI\n```powershell\ndotnet add package SimpleSoft.Hosting.Abstractions\ndotnet add package SimpleSoft.Hosting\n```\n\n### Packet CLI\n```powershell\npaket add package SimpleSoft.Hosting.Abstractions\npaket add package SimpleSoft.Hosting\n```\n## Compatibility\nThis library is compatible with the following frameworks:\n\n* SimpleSoft.Hosting.Abstractions\n  * .NET Standard 1.1;\n* SimpleSoft.Hosting\n  * .NET Framework 4.5.1;\n  * .NET Standard 1.3;\n  * .NET Standard 1.5;\n\n## Usage\nDocumentation 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.\n\nHere is an example of a console application:\n```csharp\npublic class Program\n{\n    private static readonly CancellationTokenSource TokenSource;\n\n    static Program()\n    {\n        TokenSource = new CancellationTokenSource();\n        Console.CancelKeyPress += (sender, eventArgs) =\u003e\n        {\n            TokenSource.Cancel();\n            eventArgs.Cancel = true;\n        };\n    }\n\n    public static void Main(string[] args) =\u003e\n        MainAsync(args, TokenSource.Token).ConfigureAwait(false).GetAwaiter().GetResult();\n\n    private static async Task MainAsync(string[] args, CancellationToken ct)\n    {\n        var loggerFactory = new LoggerFactory()\n            .AddConsole(LogLevel.Trace, true);\n\n        var logger = loggerFactory.CreateLogger\u003cProgram\u003e();\n\n        logger.LogInformation(\"Application started\");\n        try\n        {\n            using (var hostBuilder = new HostBuilder(\"ASPNETCORE_ENVIRONMENT\")\n                .UseLoggerFactory(loggerFactory)\n                .UseStartup\u003cStartup\u003e()\n                .ConfigureConfigurationBuilder(p =\u003e p.Builder.AddCommandLine(args)))\n            {\n                await hostBuilder.RunHostAsync\u003cHost\u003e(ct);\n            }\n        }\n        catch (TaskCanceledException)\n        {\n            logger.LogWarning(\"Application was terminated by user request\");\n        }\n        catch (Exception e)\n        {\n            logger.LogCritical(0, e, \"Unexpected exception\");\n        }\n        finally\n        {\n            logger.LogInformation(\"Application terminated. Press \u003center\u003e to exit...\");\n            Console.ReadLine();\n        }\n    }\n\n    private class Startup : HostStartup\n    {\n        public override void ConfigureConfigurationBuilder(IConfigurationBuilderParam param)\n        {\n            param.Builder\n                .SetBasePath(param.Environment.ContentRootPath)\n                .AddJsonFile(\"appsettings.json\", true, true)\n                .AddJsonFile($\"appsettings.{param.Environment.Name}.json\", true, true)\n                .AddEnvironmentVariables();\n        }\n\n        public override IServiceProvider BuildServiceProvider(IServiceProviderBuilderParam param)\n        {\n            var container = new Autofac.ContainerBuilder();\n            container.Populate(param.ServiceCollection);\n            return new AutofacServiceProvider(container.Build());\n        }\n    }\n\n    private class Host : IHost\n    {\n        private readonly IHostingEnvironment _env;\n        private readonly IConfigurationRoot _configurationRoot;\n        private readonly ILogger\u003cHost\u003e _logger;\n\n        public Host(IHostingEnvironment env, IConfigurationRoot configurationRoot, ILogger\u003cHost\u003e logger)\n        {\n            _env = env;\n            _configurationRoot = configurationRoot;\n            _logger = logger;\n        }\n\n        public Task RunAsync(CancellationToken ct)\n        {\n            _logger.LogDebug(\"Running host...\");\n\n            return Task.CompletedTask;\n        }\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplesoft-pt%2Fhosting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplesoft-pt%2Fhosting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplesoft-pt%2Fhosting/lists"}