Skip to content

Modules

Frank Wagner edited this page Aug 27, 2020 · 31 revisions

Getting started

With Hosuto modules you can create isolated parts of your application that will all run in their own host but share global settings and a DI container. As each module has it's own service provider you can register different services in each module. For example if you host a server component in one module you could configure another module to host a client of the same component. If you now distribute the application you can choose to run each module in it's own application or to run the modules in the same application.

To use modules first create a modules host in your applications main method. This host replaces the default .Net Generic Host.

Add a reference to the nuget package Dbosoft.Hosuto.Hosting and build the modules host from a ModulesHostBuilder:

        static Task Main(string[] args)
        {

            var builder = ModulesHost.CreateDefaultBuilder(args);
            
            // you can configure a modules host builder like a host builder.
            // All configurations set with ConfigureHostConfiguration will be shared between all modules.
            builder.UseEnvironment(EnvironmentName.Development);
            return builder.RunConsoleAsync();

        }

Then you can define your modules. A module is typical placed in a sperated assembly but this is not required. Modules only have to implement the IModule interface but have a convention based setup logic like the Startup class for Asp.Net Core.

    public class SimpleModule : IModule
    {
        public void ConfigureServices(
            IServiceCollection services)
        {

            [...]

The configure service method will configure the DI container of the module host. To do something useful you will typical have to register at least one HostedServices here.

The module has now to be added to the modules host builder:

        static Task Main(string[] args)
        {

            var builder = ModulesHost.CreateDefaultBuilder(args);
            
            [...]
            
            builder.HostModule<SomeModule>();
            return builder.RunConsoleAsync();

        }

Asp.Net Core

Hosuto supports also hosting of Asp.Net Core WebApplications in modules. To enable the aspnetcore support you have to method UseAspNetCore on ModulesHostBuilder:

For Asp.NetCore 3.0 or higher:

            builder.HostModule<SampleWebModule>();
            builder.UseAspNetCoreWithDefaults((module, webBuilder) =>
            {
            });

or without defaults

            builder.HostModule<SampleWebModule>();
            builder.UseAspNetCore((module, webBuilder) =>
            {
            });

For Asp.Net Core 2.0 and 2.1:

            builder.HostModule<SampleWebModule>();
            builder.UseAspNetCore(() => WebHost.CreateDefaultBuilder(args), (module, webBuilder) =>
            {
            });

Please note that if you host multiple web modules in one application you will have to configure each module to its own port / path. For Kestrel you should assign a unique port to each module. On Windows you can also use http.sys. This has the advantage that http.sys supports port sharing and therefore you could use the path of the module instead of a port:

            .UseAspNetCore((module, webHostBuilder) =>
            {
               webHostBuilder.UseHttpSys(options =>
               {
                   options.UrlPrefixes.Add($"https://localhost:8080{module.Path}");
               })
               .UseUrls($"https://localhost:8080{module.Path}");
            })

The Module has to be defined as WebModule instead of IModule. As the module is compatible with the AspNet.Core startup class you can rename your generated startup class to the module name and inherits from WebModule:

    public class SampleWebModule : WebModule
    {

        public override string Name => "SampleWebModule";
        public override string Path => "/sample";

        public SampleWebModule(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {


            [...]

Module Names

It is recommended to use the same name for module, the module assembly and the project folder of the module. For example the SampleWebModule from the code above has both a project folder name SampleWebModule (see also https://github.com/dbosoft/Hosuto/tree/master/samples/dotnetcore31).

At least for web modules this a requirement as the name of the web module will set as application name of the internal Asp.Net Core host and the application name will be used to lookup assembly parts.

Clone this wiki locally