Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Custom IServiceProvider is ignored by MVC

Open Katulus opened this issue 10 years ago • 6 comments

I have a scenario where I need to use custom IServiceProvider in my ASP.NET application, So I define following Startup class.

public class Startup
{
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        // return custom service provider
        return new MyServiceProvider(services.BuildServiceProvider());
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseMvc();
    }
}

public class MyServiceProvider : IServiceProvider
{
    private readonly IServiceProvider _baseServiceProvider;

    public MyServiceProvider(IServiceProvider baseServiceProvider)
    {
        _baseServiceProvider = baseServiceProvider;
    }

    public object GetService(Type serviceType)
    {
        // handle some services differently - in real scenario they are loaded from plugins
        if (serviceType == typeof(IMyClass))
            return new MyClass();
        return _baseServiceProvider.GetService(serviceType);
    }
}

This is simplified example just to show the concept. My custom service provider sometimes gives services from "base" service provider but in some cases it serves services from different sources. Now when I have following controller I would expect that it gets service IMyClass injected into constructor using custom service provider.

[Route("api/[controller]")]
public class ValuesController : Controller
{
    public ValuesController(IMyClass myClass)
    {
    }

    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

But it does not. MVC is not using my custom service provider. In order to make this work I have to change Configure method in Startup to:

public void Configure(IApplicationBuilder app)
{
    // This is needed so that MVC uses our custom service provider when creating controllers
    app.Use(async (context, next) =>
    {
        context.RequestServices = app.ApplicationServices;
        await next();
    });

    app.UseMvc();
}

I need to manually push custom service provider that is available in app.ApplicationServices to context.RequestServices. Then MVC correctly resolves services and creates controller.

Am I doing something wrong or is this really the way it should work? If so then it's not very intuitive.

Katulus avatar Aug 26 '16 11:08 Katulus

cc @Tratcher

pranavkm avatar Aug 26 '16 16:08 pranavkm

@JunTaoLuo @davidfowl ?

Tratcher avatar Aug 26 '16 16:08 Tratcher

@Katulus your service provider implementation is very incomplete. You need to implement an IServiceScopeFactory to allow creation of scoped services. See http://pakrym.com/aspnetcore-dependency-container/ to understand what it means to write a compatible DI container.

If you're just trying to decorate the existing container, you just need to decorate deeper 😄 .

davidfowl avatar Aug 26 '16 16:08 davidfowl

@davidfowl , thanks for the link, it clarified few things. When I added IServiceScopeFactory it really works with MVC as expected.

Now one suggestion: https://docs.asp.net/en/latest/fundamentals/dependency-injection.html#replacing-the-default-services-container should mention that IServiceScopeFactory is required for MVC. The Configure method signature gives you no clue and official documentation does not mention that. Now when I searched for it I found few blog posts that mention this interface but mostly because I know what to look for now.

Anyway, thanks a lot for clarification. I'm closing the issue now.

Katulus avatar Aug 29 '16 08:08 Katulus

Not only is it required for MVC, it's required for all the things 😄 . A complete DI container implementation is required to make ASP.NET work.

davidfowl avatar Aug 29 '16 08:08 davidfowl

Then it's even more important to have this in official documentation. :-) Or just put there the link you gave me, that one is really great.

Katulus avatar Aug 29 '16 08:08 Katulus