Skip to content

Make the IServiceCollection work #38

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

Merged
merged 2 commits into from
Mar 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions src/FeatherHttp/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ internal WebApplicationBuilder(Assembly callingAssembly, Action<IHostBuilder> co

Configuration.SetBasePath(environment.ContentRootPath);
Logging = new LoggingBuilder(Services);
Server = _deferredWebHostBuilder = new DeferredWebHostBuilder(Configuration, environment);
Host = _deferredHostBuilder = new DeferredHostBuilder(Configuration, configureHost, environment);
Server = _deferredWebHostBuilder = new DeferredWebHostBuilder(Configuration, environment, Services);
Host = _deferredHostBuilder = new DeferredHostBuilder(Configuration, configureHost, environment, Services);
}

/// <summary>
Expand Down Expand Up @@ -89,6 +89,22 @@ public WebApplication Build()
{
WebApplication sourcePipeline = null;

_hostBuilder.ConfigureServices(services =>
{
foreach (var s in Services)
{
services.Add(s);
}
});

_hostBuilder.ConfigureAppConfiguration((hostContext, builder) =>
{
foreach (var s in Configuration.Sources)
{
builder.Sources.Add(s);
}
});

_deferredHostBuilder.ExecuteActions(_hostBuilder);

_hostBuilder.ConfigureWebHostDefaults(web =>
Expand Down Expand Up @@ -163,22 +179,6 @@ public WebApplication Build()
_deferredWebHostBuilder.ExecuteActions(web);
});

_hostBuilder.ConfigureServices(services =>
{
foreach (var s in Services)
{
services.Add(s);
}
});

_hostBuilder.ConfigureAppConfiguration((hostContext, builder) =>
{
foreach (var s in Configuration.Sources)
{
builder.Sources.Add(s);
}
});

var host = _hostBuilder.Build();

return sourcePipeline = new WebApplication(host);
Expand All @@ -194,12 +194,15 @@ private class DeferredHostBuilder : IHostBuilder

private readonly WebHostEnvironment _environment;
private readonly Configuration _configuration;
private readonly IServiceCollection _services;

public DeferredHostBuilder(Configuration configuration, Action<IHostBuilder> configureHost, WebHostEnvironment environment)
public DeferredHostBuilder(Configuration configuration, Action<IHostBuilder> configureHost, WebHostEnvironment environment, IServiceCollection services)
{
_configuration = configuration;
_environment = environment;
_operations += configureHost;
_services = services;

configureHost(this);
}

public IHost Build()
Expand Down Expand Up @@ -238,7 +241,14 @@ public IHostBuilder ConfigureHostConfiguration(Action<IConfigurationBuilder> con

public IHostBuilder ConfigureServices(Action<HostBuilderContext, IServiceCollection> configureDelegate)
{
_operations += b => b.ConfigureServices(configureDelegate);
// Run these immediately so that they are observable by the imperative code
configureDelegate(new HostBuilderContext(Properties)
{
Configuration = _configuration,
HostingEnvironment = _environment
},
_services);

return this;
}

Expand Down Expand Up @@ -267,11 +277,13 @@ private class DeferredWebHostBuilder : IWebHostBuilder
private readonly WebHostEnvironment _environment;
private readonly Configuration _configuration;
private readonly Dictionary<string, string> _settings = new Dictionary<string, string>();
private readonly IServiceCollection _services;

public DeferredWebHostBuilder(Configuration configuration, WebHostEnvironment environment)
public DeferredWebHostBuilder(Configuration configuration, WebHostEnvironment environment, IServiceCollection services)
{
_configuration = configuration;
_environment = environment;
_services = services;
}

IWebHost IWebHostBuilder.Build()
Expand All @@ -287,7 +299,12 @@ public IWebHostBuilder ConfigureAppConfiguration(Action<WebHostBuilderContext, I

public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceCollection> configureServices)
{
_operations += b => b.ConfigureServices(configureServices);
configureServices(new WebHostBuilderContext
{
Configuration = _configuration,
HostingEnvironment = _environment
},
_services);
return this;
}

Expand Down