Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed: ModulesHostBuilder dosn't configures ModulesHost #28

Merged
merged 14 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
9 changes: 8 additions & 1 deletion samples/dotnet50/App/App.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -19,8 +19,15 @@
<Folder Include="Properties\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.9" />
</ItemGroup>

<PropertyGroup>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<DockerfileContext>..\..\..</DockerfileContext>
<UserSecretsId>a1dd55de-8bfa-4245-9eb1-530d27ddffde</UserSecretsId>
</PropertyGroup>

</Project>
30 changes: 30 additions & 0 deletions samples/dotnet50/App/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build
WORKDIR /src
COPY ["samples/dotnet50/App/App.csproj", "samples/dotnet50/App/"]
COPY ["src/Hosuto.Hosting.AspNetCore/Hosuto.Hosting.AspNetCore.csproj", "src/Hosuto.Hosting.AspNetCore/"]
COPY ["src/Hosuto.Hosting/Hosuto.Hosting.csproj", "src/Hosuto.Hosting/"]
COPY ["src/Hosuto/Hosuto.csproj", "src/Hosuto/"]
COPY ["src/Hosuto.Abstractions/Hosuto.Abstractions.csproj", "src/Hosuto.Abstractions/"]
COPY ["samples/shared/Hosuto.Samples.SimpleModule/Hosuto.Samples.SimpleModule.csproj", "samples/shared/Hosuto.Samples.SimpleModule/"]
COPY ["samples/shared/Hosuto.Samples.Common/Hosuto.Samples.Common.csproj", "samples/shared/Hosuto.Samples.Common/"]
COPY ["src/Hosuto.SimpleInjector/Hosuto.SimpleInjector.csproj", "src/Hosuto.SimpleInjector/"]
COPY ["samples/dotnet50/SampleWebModule/SampleWebModule.csproj", "samples/dotnet50/SampleWebModule/"]
RUN dotnet restore "samples/dotnet50/App/App.csproj"
COPY . .
WORKDIR "/src/samples/dotnet50/App"
RUN dotnet build "App.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "App.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dbosoft.Hosuto.Samples.App.dll"]
5 changes: 4 additions & 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 All @@ -25,6 +25,9 @@ static Task Main(string[] args)
builder.UseAspNetCoreWithDefaults((module, webBuilder) =>
{
});



var host = builder.Build();
var test = host.Services.GetService<IMessageDispatcher>();
var module = host.Services.GetService<SampleWebModule>();
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public DelegateModuleHostBuilderConfigurationConfigurer(Action<HostBuilderContex
{
return (ctx, config) =>
{
_configureDelegate(ctx, config);
//configuration has to be applied in reversed order, so first all
//other filters than override
next(ctx, config);
_configureDelegate(ctx, config);

};
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Hosuto.Hosting/Modules/Hosting/ModulesHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public IModulesHostBuilder ConfigureFrameworkServices(Action<HostBuilderContext,

public IModulesHostBuilder ConfigureAppConfiguration(Action<HostBuilderContext, IConfigurationBuilder> configureDelegate)
{
_innerBuilder.ConfigureAppConfiguration(configureDelegate);

ConfigureFrameworkServices((ctx, services) =>
{
services.AddTransient<IModuleConfigurationFilter>(sp =>
Expand All @@ -67,6 +69,8 @@ IHostBuilder IHostBuilder.ConfigureAppConfiguration(Action<HostBuilderContext, I

public IModulesHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate)
{
_innerBuilder.ConfigureServices(configureDelegate);

ConfigureFrameworkServices((ctx, services) =>
{
services.AddTransient<IModuleServicesFilter>(sp =>
Expand Down Expand Up @@ -211,7 +215,7 @@ private IServiceProvider CreateFrameworkServiceProvider(HostBuilderContext ctx)

private void AddModuleContextAccessor()
{
ConfigureServices((ctx, services) =>
((IHostBuilder)this).ConfigureServices((ctx, services) =>
{
services.AddSingleton<IModuleContextAccessor, ModuleContextAccessor>();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>net5.0</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
28 changes: 26 additions & 2 deletions test/Hosuto.Hosting.AspNetCore.Tests/SomeWebModule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Dbosoft.Hosuto.Modules;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Hosuto.Hosting.AspNetCore.Tests
Expand All @@ -20,19 +22,41 @@ public void CallMe()

public class SomeWebModule : WebModule
{
public IConfiguration HostConfiguration { get; }
public IConfiguration Configuration { get; private set; }

public override string Name => "I'm a module";
public override string Path => "path";

#if NETCOREAPP2_1
public IHostingEnvironment Environment { get; private set; }
#else
public IWebHostEnvironment Environment { get; private set; }
#endif

public SomeWebModule(IConfiguration hostConfiguration)
{
HostConfiguration = hostConfiguration;
}

public void ConfigureServices(IServiceCollection services)
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
Configuration = configuration;
services.AddTransient<ISomeService, SomeServiceImplementation>();

services.AddRouting();
}

public void Configure(IApplicationBuilder app)
public void Configure(IApplicationBuilder app,
#if NETCOREAPP2_1
IHostingEnvironment env
#else
IWebHostEnvironment env
#endif

)
{

#if NETCOREAPP2_1
app.UseRouter(route =>
#else
Expand Down