2

I am using ASP.Net Core Dependency Injection in an MVC App (not .Net Core app, but classic ASP.NET MVC Applicatio) I am using DI by adding the Microsoft.Extensions.DependencyInjection Nuget package. I am trying to create scoped life time for my controllers so I have a new scope whenever I create my controllers but I am getting the same instance always for my requests and there is an error as below "A single instance of controller 'X.Controllers.HomeController' cannot be used to handle multiple requests. If a custom controller factory is in use, make sure that it creates a new instance of the controller for each request"

I have used a custom factory to create my controllers and used new scope to create the controllers . and the scope is disposed in the ReleaseController method

public class MyServiceFactory : DefaultControllerFactory
{
        private readonly IServiceContainer _dependencyManager;

    public MyServiceFactory (IServiceContainer dependencyManager)
    {
            this._dependencyManager = dependencyManager;
    }

    public override void ReleaseController(IController controller)
    {
        _dependencyManager.Release(((ServiceEndPoint)controller).Context.RuntimeContext.Scope);
    }


    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {


            if (controllerType == null)
            {
                throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
            }
            var scope = _dependencyManager.GetNewScope();
            var service=(ServiceEndPoint)_dependencyManager.Resolve(scope, controllerType);
            service.Context.RuntimeContext.SetScope(scope);
            return service;
    }
}

The ServiceEndpoint is just a base class derived from Controller and I am using it as the base for all my controllers which contains some common logic. I am setting a Context for my controllers which also contain the newly created scope and I am disposing my scope in Releasecontroller by getting it from the Context. _dependencyManager.GetNewScope() create a New scope as below

   return _container.CreateScope(); 

where _container is an Instance of IServiceProvider

The code _dependencyManager.Resolve(scope, type) is as below

    public object Resolve(IServiceScope scope,Type type)
    {
        return scope.ServiceProvider.GetService(type);
    }
Steven
  • 166,672
  • 24
  • 332
  • 435
jereesh thomas
  • 113
  • 1
  • 13
  • 1
    And how are the controllers registered? – ZorgoZ Jan 01 '19 at 07:46
  • The controllers are registered by calling Add method of IServiceCollection the code is below _baseContainer.Add(new ServiceDescriptor(type, impl, GetLifeStyle(depscope))) where _baseContainer is an Object of IServiceCollection , I tried register it as Transitent as well as Scoped Lifestyles. I pass the type and impl as the type of the controllers – jereesh thomas Jan 01 '19 at 08:18
  • It was My mistake , I have multiple ways of registering types , the controllers are always get registered as singleton. – jereesh thomas Jan 01 '19 at 09:58

2 Answers2

5

You are doing something wrong, but as you hid the use of the Microsoft.Extensions.DependencyInjection (MS.DI) container behind your own abstraction, it is impossible to see what is going on.

However, the following is an example of a working sample application that integrates ASP.NET MVC with MS.DI.

MS.DI-specific controller factory:

public class MsDiControllerFactory : DefaultControllerFactory
{
    private readonly ServiceProvider container;

    public MsDiControllerFactory(ServiceProvider container) => this.container = container;

    protected override IController GetControllerInstance(RequestContext c, Type type) =>
        (IController)this.GetScope().ServiceProvider.GetRequiredService(type);

    public override void ReleaseController(IController c) => this.GetScope().Dispose();

    private IServiceScope GetScope() =>
       (IServiceScope)HttpContext.Current.Items["scope"] ??
          (IServiceScope)(HttpContext.Current.Items["scope"] = this.container.CreateScope());
}

MVC application configuring the container and replacing the default controller factory:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // Default MVC stuff
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        // create container builder to register dependencies in
        var services = new ServiceCollection();

        // register controller in the controller
        services.AddScoped<HomeController>();

        // Build the container while ensuring scopes are validated
        ServiceProvider container = services.BuildServiceProvider(true);

        // Replace default controller factory
        ControllerBuilder.Current.SetControllerFactory(
            new MsDiControllerFactory(container)); 
    }
}

When you apply the above code to an MVC application created using the default MVC template for Visual Studio, you'll get a working MVC application that uses MS.DI as its application container.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • thanks a lot, I used this and also I'm using ApplicationDbContext in my project.it work corectly if i request one time to controller But otherwise I get this Error: System.ObjectDisposedException: 'Cannot access a disposed object. Could you please help me how to solve it? – farshad Apr 18 '20 at 16:16
  • thanks a lot, it solved my issue, I was searching for the right solution for a couple of hours. – mzain Oct 23 '20 at 04:55
  • This implementation depends on the fact that `GetControllerInstance` and `ReleaseController` is only called once per `HttpContext.Current` instance. Are we sure about this? If ReleaseController is called twice on the same `HttpContext.Current` instance, this code will throw an exception. – huysentruitw Nov 26 '21 at 10:15
  • Also interesting to see how they solve this in Unity, they don't seem to be using a ControllerFactory but depend on a IHttpModule to dispose the childscope at the end of each request: https://github.com/devtrends/Unity.Mvc5/blob/master/Unity.Mvc5/UnityDependencyResolver.cs and https://github.com/devtrends/Unity.Mvc5/blob/master/Unity.Mvc5/RequestLifetimeHttpModule.cs – huysentruitw Nov 26 '21 at 11:55
2

(MVC 5, .NET Framework 4.8, not .NET Core or ASP.NET Core)

I was able to get Singleton / Scoped / Transient service lifecycles by creating an HttpContext-bound scope from within the dependency resolver, and did not need to modify the default controller factory:

private void ConfigureDependencyInjection(IAppBuilder app)
{
    var services = new ServiceCollection();
            
    AddControllersAsServices(services);

    var provider = services.BuildServiceProvider();
    var resolver = new DefaultDependencyResolver(provider);
    DependencyResolver.SetResolver(resolver);
}

private void AddControllersAsServices(IServiceCollection services)
{
    var controllers = typeof(Startup).Assembly.GetExportedTypes()
        .Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition && typeof(IController).IsAssignableFrom(t));

    foreach(var controller in controllers)
    {
        services.AddTransient(controller);
    }
}

class DefaultDependencyResolver : IDependencyResolver
{
    private readonly IServiceProvider serviceProvider;

    public DefaultDependencyResolver(IServiceProvider serviceProvider)
    {
        this.serviceProvider = serviceProvider;
    }

    public object GetService(Type serviceType)
    {
        var scope = this.GetHttpContextScope();

        if (scope == null)
        {
            return this.serviceProvider.GetService(serviceType);
        }
        else
        {
            return scope.ServiceProvider.GetService(serviceType);
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return this.serviceProvider.GetServices(serviceType);
    }

    private IServiceScope GetHttpContextScope()
    {
        var context = HttpContext.Current;

        if (context == null)
        {
            return null;
        }

        var scope = context.Items[this] as IServiceScope;

        if (scope != null)
        {
            return scope;
        }

        scope = this.serviceProvider.CreateScope();

        context.Items[this] = scope;
        context.AddOnRequestCompleted(_ => scope.Dispose());

        return scope;
    }
}

The code works as follows: if a service is requested and there is a current HttpContext check if there is an IServiceScope associated with it, otherwise create a new Scope instance and bind it to the context. When the request is completed, dispose of the scope instance.

If there is no HttpContext simply resolve the service from the root ServiceProvider instance. I am not sure what that means for scoped services, but I assume they will behave like singletons in that case.

nicholas
  • 2,969
  • 20
  • 39