Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Proposal: Support IServiceProviderFactory #829

Closed
davidfowl opened this issue Aug 6, 2016 · 7 comments
Closed

Proposal: Support IServiceProviderFactory #829

davidfowl opened this issue Aug 6, 2016 · 7 comments

Comments

@davidfowl
Copy link
Member

davidfowl commented Aug 6, 2016

Based on aspnet/DependencyInjection#442

The WebHostBuilder API would take advantage of this by using it as a way to override the default IServiceProvider implementation without returning it directly from the application's Startup class. The wire up could look like this:

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseAutofac() // Replace the built-in IServiceProviderFactory with an autofac impl
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

One downside to this is that you don't get direct access to the container APIs in the Startup class (which is part of the benefit besides being able to swap out the default impl).

In the above example, UseAutofac would wire up the IServiceProviderFactory. We could also extend the ConventionBasedStartup to allow authoring a ConfigureContainer method that takes the concrete ContainerBuilder directly:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
         services.AddMvc();
    }

    public void ConfigureContainer(ContainerBuilder builder)
    {
        builder.RegisterInstance(new TaskRepository())
           .As<ITaskRepository>();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }
}
@guardrex
Copy link
Contributor

guardrex commented Aug 6, 2016

you don't get direct access to the container APIs in the Startup class

Will there be any consequences for those who despise the vile and dreaded Startup class? 😄

public static void Main(string[] args)
{
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureServices(services =>
        {
            ...
        })
        .Configure((app) =>
        {
            ...
        })
        .Build();

    host.Run();
}

@davidfowl
Copy link
Member Author

You can't replace the IServiceProvider today if you configure everything with the WebHostBuilder today. See #550

@guardrex
Copy link
Contributor

guardrex commented Aug 6, 2016

Fair enough ... just checking. Since I don't (with this pattern "can't" really), it won't affect me. 😄

@Tratcher
Copy link
Member

Tratcher commented Aug 6, 2016

How does it work if UseKestrel is already adding and accessing services?

@davidfowl
Copy link
Member Author

@Tratcher what specifically are you asking about? Passing the IServiceProvider directly or the new IServiceProviderFactory proposal?

@Tratcher
Copy link
Member

Tratcher commented Aug 6, 2016

In the new IServiceProviderFactory.

@davidfowl
Copy link
Member Author

Works fine. Just like it does today. UseKestrel wires up an IConfigureOptions<KestrelOptions> that captures the IServicerProvider provided by the application.

davidfowl referenced this issue in aspnet/DependencyInjection Aug 9, 2016
- A new abstraction that allows the DI container author to describe how
to translate an IServiceCollection to a specific container and then to an IServiceProvider.
- Hosting will use this abstraction to swap out the default container
@davidfowl davidfowl self-assigned this Aug 9, 2016
@davidfowl davidfowl added this to the 1.1.0 milestone Aug 9, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants