4

In Startup.ConfigureServices, I have this piece of code:

services.AddSingleton<IControllerActivator>(new LicenseKeyControllerActivator(keyService));

LicenseKeyControllerActivator looks like this:

public class LicenseKeyControllerActivator
    : IControllerActivator
{
    private readonly ILicenseKeyService _keyService;

    public LicenseKeyControllerActivator(ILicenseKeyService licenseKeyService)
    {
        _keyService = licenseKeyService;
    }

    public object Create(ControllerContext context)
    {
        return new LicenseController(_keyService);
    }

    public void Release(ControllerContext context, object controller)
    {
        return;
    }
}

During the request-response lifecycle, the activator creates the controller, the DataAnnotations-based validations work, the filters filter, but no controller method is ever called. In fact, after the set is called on the DTO, Release is called immediately.

I'm not sure if this is a routing problem, or a controller activator issue.

Community
  • 1
  • 1
rianjs
  • 7,767
  • 5
  • 24
  • 40
  • I am not sure what you try to accomplish. By default Controllers are **NOT RESOLVED** by the ASP.NET Core DI (see http://stackoverflow.com/documentation/asp.net-core/1949/dependency-injection/23212/resolve-controllers-viewcomponents-and-taghelpers-via-dependency-injection#t=201704181554355419976) but by the default implementation of the controller activator. If people want Controllers to be resolved by DI they need to explicitly opt-in for it – Tseng Apr 18 '17 at 15:55
  • 1
    This absolutely seems a routing problem, because overriding the `IControllerActivator` _is_ the way to practice **Pure DI* in ASP.NET Core. – Steven Apr 18 '17 at 18:49
  • Yes, it is. Routing is broken due to a bad ActionFilter. Sorry, I would delete or close my own question, because the question contains the answer. – rianjs Apr 18 '17 at 20:42

0 Answers0