{"id":19100250,"url":"https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting","last_synced_at":"2025-04-18T17:33:12.115Z","repository":{"id":34489022,"uuid":"179429808","full_name":"dapplo/Dapplo.Microsoft.Extensions.Hosting","owner":"dapplo","description":"Extensions for generic host based applications","archived":false,"fork":false,"pushed_at":"2024-08-13T13:01:54.000Z","size":739,"stargazers_count":209,"open_issues_count":16,"forks_count":35,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-11-04T11:09:41.493Z","etag":null,"topics":["hacktoberfest","hacktoberfest2021"],"latest_commit_sha":null,"homepage":"","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/dapplo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"contributing.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"lakritzator","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"custom":null}},"created_at":"2019-04-04T05:46:51.000Z","updated_at":"2024-10-25T09:25:31.000Z","dependencies_parsed_at":"2023-02-14T06:46:18.007Z","dependency_job_id":null,"html_url":"https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting","commit_stats":null,"previous_names":["dapplo/dapplo.microsoft.extensions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapplo%2FDapplo.Microsoft.Extensions.Hosting","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapplo%2FDapplo.Microsoft.Extensions.Hosting/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapplo%2FDapplo.Microsoft.Extensions.Hosting/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dapplo%2FDapplo.Microsoft.Extensions.Hosting/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dapplo","download_url":"https://codeload.github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223768721,"owners_count":17199394,"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":["hacktoberfest","hacktoberfest2021"],"created_at":"2024-11-09T03:52:38.877Z","updated_at":"2024-11-09T03:53:29.298Z","avatar_url":"https://github.com/dapplo.png","language":"C#","funding_links":["https://github.com/sponsors/lakritzator"],"categories":["hacktoberfest"],"sub_categories":[],"readme":"# Dapplo.Microsoft.Extensions.Hosting\n\n[![Build Status](https://dev.azure.com/Dapplo/Dapplo.Microsoft.Extensions.Hosting/_apis/build/status/dapplo.Dapplo.Microsoft.Extensions.Hosting?branchName=master)](https://dev.azure.com/Dapplo/Dapplo.Microsoft.Extensions.Hosting/_build/latest?definitionId=6\u0026branchName=master)\n\nEver wondered if it's possible to have a nice modular way to develop a .NET application, being able to reuse services and logic between ASP.NET core, Service workers, Console application or even a UI application? Maybe even have the possibility to combine them all in one application? The [generic host](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host) makes this (partly) possible, although it might look like it's for ASP.NET core it's not and it will probably move elsewhere.\n\nThis repository brings you a few extensions on the generic host which will help you on your way to quickly build a new application with extra functionality:\n- Dapplo.Microsoft.Extensions.Hosting.AppServices - Simple services, e.g. make sure you application runs only once!\n- Dapplo.Microsoft.Extensions.Hosting.CaliburnMicro - Bases upon Dapplo.Microsoft.Extensions.Hosting.Wpf and bootstraps [Caliburn.Micro](https://caliburnmicro.com)\n- Dapplo.Microsoft.Extensions.Hosting.WinForms - Have a way to bootstrap Windows Forms with all the possible generic host functionality, and manage the lifetime.\n- Dapplo.Microsoft.Extensions.Hosting.Wpf - Have a way to bootstrap WPF with all the possible generic host functionality, and manage the lifetime.\n- Dapplo.Microsoft.Extensions.Hosting.Plugins - Makes it possible to find \u0026 load additional plug-in which can add services to your application.\n\nFYI: there is a solution with samples in the samples directory and one which is used on the build server in the src.\n\nI've created a dotnet new template on [![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.CaliburnMicro.Template.CSharp.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.CaliburnMicro.Template.CSharp/) so to quickly start, you can type the following:\n```\ndotnet new --install Dapplo.Microsoft.Extensions.Hosting.CaliburnMicro.Template.CSharp\n```\n\nAfter this you can create a new project by doing something like this (the enable metro and mutex arguments are optional, default is true):\n```\ndotnet new caliburnmicrohost --EnableMetro true --EnableMutex true\n```\n\n\nDapplo.Microsoft.Extensions.Hosting.Plugins\n--------------------------------------------\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.Plugins.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.Plugins/)\n\nThis extension adds plug-in support to generic host based dotnet core 6.0 applications.\n\nYou can simply add the location of plug-ins by specifying globs to find your plug-in assemblies.\nThis can be both files to include and / or exclude.\nEach located plug-ins is loaded into it's own AssemblyLoadContext, dependencies are found and loaded in the same AssemblyLoadContext via an AssemblyDependencyResolver (which was introduced in dotnet core 3.0).\n\n[Here](https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting/blob/master/samples/Dapplo.Hosting.Sample.ConsoleDemo/Program.cs#L27)\n is an example how to use the loading, and also how to handle framework assemblies:\n```C#\n.ConfigurePlugins(pluginBuilder =\u003e\n\t{\n\t\t// Specify the location from where the Dll's are \"globbed\"\n\t\tpluginBuilder.AddScanDirectories(Path.Combine(Directory.GetCurrentDirectory(), @\"..\\..\"));\n\t\t// Add the framework libraries which can be found with the specified globs\n\t\tpluginBuilder.IncludeFrameworks(@\"**\\bin\\**\\*.FrameworkLib.dll\");\n\t\t// Add the plugins which can be found with the specified globs\n\t\tpluginBuilder.IncludePlugins(@\"**\\bin\\**\\*.Plugin*.dll\");\n\t})\n```\n\nIn the assembly (DLL) which is your plugin you should have a public class which implements IPlugin.\n\nIf you want more speed, you can use the previous code and specify the AssemblyScanFunc to use PluginScanner.ByNamingConvention\nNow you will need to follow a naming convention, this is for speed so there is no need to scan all types. It should have a class named Plugin which implements IPlugin, in a package which has the same name as the assembly.\n\nExample for the IPlugin implementation, this can configure the HostBuilderContext:\n\n```C#\n    /// \u003csummary\u003e\n    /// This plug-in configures the HostBuilderContext to have the hosted services from the online example\n    /// \u003c/summary\u003e\n    public class Plugin : IPlugin\n    {\n        /// \u003cinheritdoc /\u003e\n        public void ConfigureHost(HostBuilderContext hostBuilderContext, IServiceCollection serviceCollection)\n        {\n            serviceCollection.AddHostedService\u003cLifetimeEventsHostedService\u003e();\n            serviceCollection.AddHostedService\u003cTimedHostedService\u003e();\n        }\n    }\n```\n\nThis can also be simplified to use the following code with up to 3 configured services:\n```C#\n    /// \u003csummary\u003e\n    /// This plug-in configures the HostBuilderContext to have the hosted services from the online example\n    /// \u003c/summary\u003e\n    public class Plugin : PluginBase\u003cLifetimeEventsHostedService, TimedHostedService\u003e\n    {\n    }\n```\n\n\nDapplo.Microsoft.Extensions.Hosting.AppServices\n-----------------------------------------------\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.AppServices.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.AppServices/)\n\nThis extension adds some generic application services for desktop applications, currently only the mutex functionality is included but more are coming.\n\n[Here](https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting/blob/master/samples/Dapplo.Hosting.Sample.WinFormsDemo/Program.cs#L25) is an example how to make sure your application only runs once.\n\n```C#\n.ConfigureSingleInstance(builder =\u003e\n\t{\n\t\tbuilder.MutexId = \"{B9CE32C0-59AE-4AF0-BE39-5329AAFF4BE8}\";\n\t\tbuilder.WhenNotFirstInstance = (hostingEnvironment, logger) =\u003e\n\t\t{\n\t\t\t// This is called when an instance was already started, this is in the second instance\n\t\t\tlogger.LogWarning(\"Application {0} already running.\", hostingEnvironment.ApplicationName);\n\t\t};\n\t})\n```\nIn general you call hostBuilder.ConfigureSingleInstance and supply a mutex id.\n\n\n\nDapplo.Microsoft.Extensions.Hosting.WinForms\n--------------------------------------------\n\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.WinForms.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.WinForms/)\n\nThis extension adds WinForms support to generic host based applications.\nWith this you can enhance your application with a UI, and use all the services provided by the generic host like DI, logging etc.\n\n[Here](https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting/blob/master/samples/Dapplo.Hosting.Sample.WinFormsDemo/Program.cs#L48) is an example how to start your application with a Form1 and have the application automatically shutdown whenever you exit the Form1. To make this possible Form1 must implement a marker interface, which currently has no methods, called IWinFormsShell. The IWinFormsShell is considered the main entry point of your UI. You only specify the type, the instance will be created at a later time by the generic host and will automatically go through the DI process.\n\nThis means you can have a constructor which requests a logger, or other forms.\n\nIt's not much more than adding something like this to your hostBuilder:\n```C#\n .ConfigureWinForms\u003cForm1\u003e()\n .UseWinFormsLifetime()\n```\n\n\nDapplo.Microsoft.Extensions.Hosting.Wpf\n---------------------------------------\n\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.Wpf.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.Wpf/)\n\nThis extension adds WPF support to generic host based dotnet core 3.0 applications.\nWith this you can enhance your application with a UI, and use all the services provided by the generic host like DI, logging etc.\n\n[Here](https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting/blob/master/samples/Dapplo.Hosting.Sample.WpfDemo/Program.cs#L48) is an example how to start your application with a MainWindow and have the application automatically shutdown whenever you exit the MainWindow. To make this possible MainWindow must implement a marker interface, which currently has no methods, called IWpfShell. The IWpfShell is considered the main entry point of your UI. You only specify the type, the instance will be created at a later time by the generic host and will automatically go through the DI process.\n\nThis means your MainWindow can have a constructor which requests a logger, or other windows.\n\nIt's not much more than adding something like this to your hostBuilder:\n```C#\n\t.ConfigureWpf\u003cMainWindow\u003e()\n\t.UseWpfLifetime()\n```\n\n\nDapplo.Microsoft.Extensions.Hosting.CaliburnMicro\n---------------------------------------\n\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.CaliburnMicro.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.CaliburnMicro/)\n\nThis extension adds [Caliburn.Micro](https://caliburnmicro.com) support to generic host based applications.\nWith this you can enhance your application with a UI, and use all the services provided by the generic host like DI, logging etc, together with this great MVVM framework.\n\n[Here](https://github.com/dapplo/Dapplo.Microsoft.Extensions.Hosting/blob/master/samples/Dapplo.Hosting.Sample.CaliburnMicroDemo/Program.cs#L54) is an example how to start your application with a MainWindowViewModel and have the application automatically shutdown whenever you exit the MainWindowViewModel. To make this possible MainWindowViewModel must implement a marker interface, which currently has no methods, called ICaliburnMicroShell. The ICaliburnMicroShell is considered the main entry point of your UI. You only specify the type, the instance will be created at a later time by the generic host and will automatically go through the DI process.\n\nThis means your MainWindowViewModel can have a constructor which requests a logger, or other windows.\n\nIt's not much more than adding something like this to your hostBuilder:\n```C#\n\t.ConfigureCaliburnMicro\u003cMainViewModel\u003e()\n```\nIt assumes Dapplo.Microsoft.Extensions.Hosting.Wpf is used!\n\n\nDapplo.Microsoft.Extensions.Hosting.ReactiveUI.Wpf\n---------------------------------------\n\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.ReactiveUI.Wpf.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.ReactiveUI.Wpf/)\n\nThis extension adds [ReactiveUI](https://reactiveui.net/) support to generic host based applications.\nWith this you can enhance your application with ReactiveUI functions, and use all the services provided by the generic host like combining Splat with Microsoft DI, logging etc, together with this great Reactive MVVM framework.\n\nThis is based on the Dapplo.Microsoft.Extensions.Hosting.Wpf extension, so you can use that to start your application.\n\nUse the following code to merge ReactiveUI.Splat with the Microsoft Dependency Resolver in your application:\n```C#\n    .ConfigureSplatForMicrosoftDependencyResolver()\n```\n\nDapplo.Microsoft.Extensions.Hosting.ReactiveUI.WinForms\n---------------------------------------\n\n[![Nuget](https://img.shields.io/nuget/v/Dapplo.Microsoft.Extensions.Hosting.ReactiveUI.WinForms.svg)](https://www.nuget.org/packages/Dapplo.Microsoft.Extensions.Hosting.ReactiveUI.WinForms/)\n\nThis extension adds [ReactiveUI](https://reactiveui.net/) support to generic host based applications.\nWith this you can enhance your application with ReactiveUI functions, and use all the services provided by the generic host like combining Splat with Microsoft DI, logging etc, together with this great Reactive MVVM framework.\n\nThis is based on the Dapplo.Microsoft.Extensions.Hosting.WinForms extension, so you can use that to start your application.\n\nUse the following code to merge ReactiveUI.Splat with the Microsoft Dependency Resolver in your application:\n```C#\n    .ConfigureSplatForMicrosoftDependencyResolver()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdapplo%2FDapplo.Microsoft.Extensions.Hosting","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdapplo%2FDapplo.Microsoft.Extensions.Hosting","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdapplo%2FDapplo.Microsoft.Extensions.Hosting/lists"}