{"id":19244390,"url":"https://github.com/nanoframework/nanoframework.hosting","last_synced_at":"2026-04-28T03:08:52.384Z","repository":{"id":37683581,"uuid":"504940890","full_name":"nanoframework/nanoFramework.Hosting","owner":"nanoframework","description":"📦 nanoFramework.Hosting library for .NET nanoFramework","archived":false,"fork":false,"pushed_at":"2026-04-21T01:21:55.000Z","size":214,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2026-04-21T03:03:32.548Z","etag":null,"topics":["csharp","dependency-injection","esp32","generic-host","hacktoberfest","ioc","iot","nanoframework","stm32","texas-instruments"],"latest_commit_sha":null,"homepage":"https://www.nanoframework.net","language":"C#","has_issues":false,"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/nanoframework.png","metadata":{"funding":{"open_collective":"nanoframework","github":"nanoframework"},"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-06-18T20:10:19.000Z","updated_at":"2026-04-17T14:12:24.000Z","dependencies_parsed_at":"2023-02-05T00:17:04.609Z","dependency_job_id":"bfe437aa-29cd-4a54-9742-313d14d4c3c2","html_url":"https://github.com/nanoframework/nanoFramework.Hosting","commit_stats":{"total_commits":68,"total_committers":6,"mean_commits":"11.333333333333334","dds":"0.47058823529411764","last_synced_commit":"9874371226fa3027b0fcdbade261846431a7d45e"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"purl":"pkg:github/nanoframework/nanoFramework.Hosting","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.Hosting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.Hosting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.Hosting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.Hosting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nanoframework","download_url":"https://codeload.github.com/nanoframework/nanoFramework.Hosting/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nanoframework%2FnanoFramework.Hosting/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32179387,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-23T11:42:27.955Z","status":"ssl_error","status_checked_at":"2026-04-23T11:42:18.877Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","dependency-injection","esp32","generic-host","hacktoberfest","ioc","iot","nanoframework","stm32","texas-instruments"],"created_at":"2024-11-09T17:23:13.790Z","updated_at":"2026-04-23T12:01:25.517Z","avatar_url":"https://github.com/nanoframework.png","language":"C#","funding_links":["https://opencollective.com/nanoframework","https://github.com/sponsors/nanoframework"],"categories":[],"sub_categories":[],"readme":"[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.Hosting\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.Hosting) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=nanoframework_nanoFramework.Hosting\u0026metric=reliability_rating)](https://sonarcloud.io/dashboard?id=nanoframework_nanoFramework.Hosting) [![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) [![NuGet](https://img.shields.io/nuget/dt/nanoFramework.Hosting.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.Hosting/) [![#yourfirstpr](https://img.shields.io/badge/first--timers--only-friendly-blue.svg)](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md) [![Discord](https://img.shields.io/discord/478725473862549535.svg?logo=discord\u0026logoColor=white\u0026label=Discord\u0026color=7289DA)](https://discord.gg/gCyBu8T)\n\n![nanoFramework logo](https://raw.githubusercontent.com/nanoframework/Home/main/resources/logo/nanoFramework-repo-logo.png)\n\n-----\n\n# Welcome to the .NET nanoFramework Generic Host Library repository\nThe .NET nanoFramework Generic Host provides convenience methods for creating [dependency injection (DI)](https://github.com/nanoframework/nanoFramework.DependencyInjection/tree/main) application containers with preconfigured defaults.\n\n## Build status\n\n| Component | Build Status | NuGet Package |\n|:-|---|---|\n| nanoFramework.Hosting | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.Hosting/_apis/build/status/nanoFramework.Hosting?branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.Hosting/_build/latest?definitionId=96\u0026branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.Hosting.svg?label=NuGet\u0026style=flat\u0026logo=nuget)](https://www.nuget.org/packages/nanoFramework.Hosting/) |\n\n## Samples\n\n[Hosting Samples](https://github.com/nanoframework/Samples/tree/main/samples/Hosting)\n\n[Hosting Unit Tests](https://github.com/nanoframework/nanoFramework.Hosting/tree/main/tests)\n\n## Generic Host\n\nA Generic Host configures a DI application container as well as provides services in the DI container which handle the the application lifetime. When a host starts it calls `Start()` on each implementation of `IHostedService` registered in the service container's collection of hosted services. In the application container all `IHostedService` object that inherent `BackgroundService` or `SchedulerService` have their `ExecuteAsync()` methods called.\n\nThis API mirrors as close as possible the official .NET \n[Generic Host](https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host).\n\n```csharp\nusing nanoFramework.Hosting;\nusing nanoFramework.DependencyInjection;\n\nnamespace Hosting\n{\n    public class Program\n    {\n        public static void Main()\n        {\n            IHost host = CreateHostBuilder().Build();\n            \n            // starts application and blocks the main calling thread \n            host.Run();\n        }\n\n        public static IHostBuilder CreateHostBuilder() =\u003e\n            Host.CreateDefaultBuilder()\n                .ConfigureServices(services =\u003e\n                {\n                    services.AddSingleton(typeof(BackgroundQueue));\n                    services.AddHostedService(typeof(SensorService));\n                    services.AddHostedService(typeof(DisplayService));\n                });\n    }\n}\n```\n\n## IHostedService interface\n\nWhen you register an `IHostedService` the host builder will call the `Start()` and `Stop()` methods of `IHostedService` during application start and stop respectively. You can create multiple implementations of `IHostedService` and register them using the `ConfigureService()` method in the DI container. All hosted services will be started and stopped along with the application.\n\n```csharp\npublic class CustomService : IHostedService\n{\n    public void Start() { }\n\n    public void Stop() { }\n}\n```\n\n## BackgroundService base class\n\nProvides a base class for implementing a long running `IHostedService`. The method `ExecuteAsync()` is called asynchronously to run the background service. Your implementation of `ExecuteAsync()` should finish promptly when the `CancellationRequested` is fired in order to gracefully shut down the service.\n\n```csharp\npublic class SensorService : BackgroundService\n{\n    protected override void ExecuteAsync()\n    {\n        while (!CancellationRequested)\n        {\n            // to allow other threads time to process include \n            // at least one millsecond sleep in loop\n            Thread.Sleep(1);\n        }\n    }\n}\n```\n\n## SchedulerService base class\n\n Provides a base class for implementing a scheduled [Timer](https://docs.nanoframework.net/api/System.Threading.Timer.html) running `IHostedServce`. The timer triggers at a specified time and interval the `ExecuteAsync()` method. The timer is disabled on `Stop()` and disposed when the service container is disposed.\n\n```csharp\npublic class DisplayService : SchedulerService\n{\n    // represents a timer control that involks ExecuteAsync at a \n    // specified interval of time repeatedly\n    public DisplayService() : base(TimeSpan.FromSeconds(1)) {}\n\n    protected override void ExecuteAsync(object state)\n    {   \n    }\n}\n```\n\n## IServiceCollection extensions method\n\nExtending `IServiceCollection` is a pretty straightforward way to add additional features to the application container.\n\n```csharp\npublic static IServiceCollection AddLogging(this IServiceCollection services, LogLevel level)\n{\n    if (services == null)\n    {\n        throw new ArgumentNullException();\n    }\n\n    var loggerFactory = new DebugLoggerFactory();\n    LogDispatcher.LoggerFactory = loggerFactory;\n\n    var logger = (DebugLogger)loggerFactory.GetCurrentClassLogger();\n    logger.MinLogLevel = level;\n\n    // using TryAdd prevents duplicate logging objects if AddLogging() \n    // is added more then once to ConfigureServices\n    services.TryAdd(new ServiceDescriptor(typeof(ILogger), logger));\n    services.TryAdd(new ServiceDescriptor(typeof(ILoggerFactory), loggerFactory));\n\n    return services;\n}\n```\n\nThe extension can then be registered like this:\n\n```csharp\npublic static IHostBuilder CreateHostBuilder() =\u003e\n    Host.CreateDefaultBuilder()\n        .ConfigureServices(services =\u003e\n        {\n            services.AddLogging(LogLevel.Debug);\n            services.AddSingleton(typeof(LoggingService));\n        });\n```\n\nAnd used like this:\n\n```csharp\npublic class LoggingService : IHostedService\n{\n    private ILogger Logger { get; set; }\n\n    public LoggingService(ILogger logger)\n    {\n        Logger = logger;\n    }\n\n    public void Start()\n    {\n        Logger.Log(LogLevel.Information, new EventId(10, \"Start\"), \"Logging started\", null);\n    }\n\n    public void Stop()\n    {\n        Logger.Log(LogLevel.Information, new EventId(11, \"Stop\"), \"Logging stopped\", null);\n    }\n}\n```\n\n## Validate On Build\n\nThe default builder enables DI validation when the debugger is attached. This check is performed to ensure that all services registered with the container can actually be created. This can be particularly useful during development to fail fast and allow developers to fix issues. The setting can be modified by using the `UseDefaultServiceProvider()` method.\n\n```csharp\npublic static IHostBuilder CreateHostBuilder() =\u003e\n    Host.CreateDefaultBuilder()\n        .UseDefaultServiceProvider(options =\u003e\n        {\n            options.ValidateOnBuild = false;\n        });\n```\n\n## Feedback and documentation\n\nFor documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).\n\nJoin our Discord community [here](https://discord.gg/gCyBu8T).\n\n## Credits\n\nThe list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md).\n\n## License\n\nThe **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md).\n\n## Code of Conduct\n\nThis project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community.\nFor more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).\n\n## .NET Foundation\n\nThis project is supported by the [.NET Foundation](https://dotnetfoundation.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fnanoframework.hosting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnanoframework%2Fnanoframework.hosting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnanoframework%2Fnanoframework.hosting/lists"}