Skip to content

Commit

Permalink
override config set by WebHostBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
fw2568 committed May 9, 2021
1 parent ebebf72 commit 6dca737
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion samples/dotnet50/App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ static Task Main(string[] args)

// you can configure a module host builder like a host builder.
// All configurations set with ConfigureHostConfiguration will be shared between all modules.
builder.UseEnvironment(Environments.Development);
//builder.UseEnvironment(Environments.Development);

//here we add a ServiceCollection to build a DI container that is available for all modules.
var sc = new ServiceCollection();
Expand Down
11 changes: 11 additions & 0 deletions samples/dotnet50/App/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"App": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "right",
"ASPNETCORE_ENVIRONMENT": "wrong"
}
}
}
}
2 changes: 1 addition & 1 deletion samples/dotnet50/SampleWebModule/SampleModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SampleWebModule(IConfiguration configuration)
public IConfiguration Configuration { get; }

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

services.AddSingleton(serviceProvider.GetRequiredService<IMessageDispatcher>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public override void BootstrapHost(BootstrapModuleHostCommand<TModule> command)
Configure(command.ModuleContext, app);
});
})).Reverse());
ApplyConfiguration(builder);

command.Options.ConfigureBuilderAction?.Invoke(builder);

Expand Down
25 changes: 17 additions & 8 deletions src/Hosuto.Hosting/Modules/Hosting/DefaultBootstrapHostHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Dbosoft.Hosuto.Modules.Hosting.Internal;
Expand Down Expand Up @@ -26,32 +27,40 @@ protected virtual void ConfigureServices(HostBuilderContext hostBuilderContext,
(BootstrapContext.ToModuleHostBuilderContext(hostBuilderContext), services);
}


protected virtual IHostBuilder CreateHostBuilder()
protected virtual void ApplyConfiguration(IHostBuilder builder)
{
var builder = new HostBuilder();

var hostBuilderContext = BootstrapContext.Advanced.FrameworkServices.GetRequiredService<HostBuilderContext>();
var moduleAssemblyName = BootstrapContext.Module.GetType().Assembly.GetName().Name;

builder.ConfigureHostConfiguration((configure) =>
{
configure.Sources.Clear();
configure.AddConfiguration(hostBuilderContext.Configuration);
configure.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>(HostDefaults.ApplicationKey,moduleAssemblyName)
});
});

builder.UseContentRoot(GetContentRoot());

var moduleAssemblyName = BootstrapContext.Module.GetType().Assembly.GetName().Name;

builder.ConfigureAppConfiguration((ctx, config) =>
{
ctx.HostingEnvironment.ApplicationName = moduleAssemblyName;
Filters.BuildFilterPipeline(
BootstrapContext.Advanced.FrameworkServices.GetServices<IModuleConfigurationFilter>(),
(_, __) => { })(BootstrapContext.ToModuleHostBuilderContext(ctx), config);
});

}


protected virtual IHostBuilder CreateHostBuilder()
{
var builder = new HostBuilder();

return builder;
}

Expand Down Expand Up @@ -87,7 +96,7 @@ public virtual void BootstrapHost(BootstrapModuleHostCommand<TModule> command)
BootstrapContext = command.BootstrapContext;

var builder = CreateHostBuilder();

ApplyConfiguration(builder);
builder.ConfigureServices((ctx, services) =>
{
// ReSharper disable once ConvertToUsingDeclaration
Expand Down
10 changes: 6 additions & 4 deletions src/Hosuto.Hosting/Modules/Hosting/ModulesHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ public IModulesHostBuilder ConfigureFrameworkServices(Action<HostBuilderContext,

public IModulesHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate)
{
//only configure the innerBuilder. Generated config will be shared with modules and for module configuration
//there is the method ConfigureModuleConfiguration

_innerBuilder.ConfigureAppConfiguration(configureDelegate);

ConfigureFrameworkServices((ctx, services) =>
{
services.AddTransient<IModuleConfigurationFilter>(sp =>
new DelegateModuleHostBuilderConfigurationConfigurer(configureDelegate));
});

return this;
}

Expand All @@ -66,7 +69,6 @@ IHostBuilder IHostBuilder.ConfigureAppConfiguration(Action<HostBuilderContext, I

public IModulesHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate)
{
// services applied here will be added to inner builder and to all modules
_innerBuilder.ConfigureServices(configureDelegate);

ConfigureFrameworkServices((ctx, services) =>
Expand Down

0 comments on commit 6dca737

Please sign in to comment.