9

We have ASP.NET Core solution with standard Microsoft.Extensions.DependencyInjection and need to register certain control depending on configuration setting.

Some Example ApiController that inherits from ControllerBase and all their related actions should only be registered if certain bool is true.

Is this possible? I looked at services.AddMvc() but I didn't see any option that would easily allow me to either:

  • Prevent certain ExampleController from being registered
  • Remove ExampleController and all it's related actions from IServiceCollection after being registered
Guillaume S.
  • 1,515
  • 1
  • 8
  • 21
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
  • If im using autofac lib to register my services like this if (!DataSettingsManager.DatabaseIsInstalled) { if (config.UseFastInstallationService) builder.RegisterType().As().InstancePerLifetimeScope(); else builder.RegisterType().As().InstancePerLifetimeScope(); } so i can able to avoid as per my condition so try with autofac to register controller. – Dharmeshsharma Jan 07 '20 at 07:13
  • I think you can find your answer in the link below: [Related stackoverflow post](https://stackoverflow.com/questions/47617994/how-to-use-a-controller-in-another-assembly-in-asp-net-core-mvc-2-0) – ylmz Jan 07 '20 at 07:30
  • 1
    There is also an option to implement custom `IApplicationFeatureProvider` like in the answer: https://stackoverflow.com/questions/36680933/discovering-generic-controllers-in-asp-net-core You can implement separate `IApplicationFeatureProvider` that will filter out specific controller types. You can find simple example in this gist: https://gist.github.com/Umqra/89b6f26c539a72bdf8c479d5438000dc – Nikita Sivukhin Jan 07 '20 at 08:28

1 Answers1

10

As pointed out in comments, implement feature filter and register it in your services config:

public class MyFeatureProvider: ControllerFeatureProvider
{
    private readonly bool condition;

    public MyFeatureProvider(bool condition)
    {
        this.condition = condition;
    }
    
    protected override bool IsController(TypeInfo typeInfo) 
    {
        if (condition && typeInfo.Name == "ExampleController") 
        {
            return false;
        }
        return base.IsController(typeInfo);
    }
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {            
        services.AddMvc().ConfigureApplicationPartManager(mgr => 
        {
            mgr.FeatureProviders.Clear();
            mgr.FeatureProviders.Add(new MyFeatureProvider(true));
        });            
    }
}

I'll link the source code in case you'd like to check out stock standard implementation and see how it works for reference

PhatBuck
  • 326
  • 5
  • 15
timur
  • 14,239
  • 2
  • 11
  • 32
  • Does not work for me... Endpoints are still accessible... (Debugged the IsController method which returns false on the ExampleController) – Roel Jun 28 '21 at 12:06
  • I think the part of removing the existing ControllerFeatureProvider is missing see: https://stackoverflow.com/a/50382268/199252 – Roel Jun 28 '21 at 12:16
  • 2
    @Roel You might probably want to remove only existing `ControllerFeatureProvider`s, not all `FeatureProvider`s. – Tony Jul 19 '21 at 10:30
  • On default you only have the existing one and this is the place to add your own custom ones. – Roel Jul 21 '21 at 11:47