I'm developing an application which has 2 controllers - an AdminController and a JsonController (based off ControllerBase). The application contains 2 or more self-hosts for the controllers, but only 1 self-host will contain the AdminController - the other hosts will each contain the JsonController only.
I'm trying to use the ControllerFeatureProvider in just one self-host for the moment to disable the AdminController (and leave the JsonController active), however, if I implement this it disables both controllers, i.e. i cannot query either of them. I've confirmed that the IsController is returning true/false for each of the controllers in question. The ControllerFeatureProvider is shown below
public class CustomControllerFeatureProvider : ControllerFeatureProvider
{
protected override bool IsController(TypeInfo typeInfo)
{
if (base.IsController(typeInfo))
{
if (typeInfo.Name == nameof(AdminController))
{
return false;
}
return true;
}
return false;
}
}
And the Startup contents are:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(
"CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.Providers.Add<BrotliCompressionProvider>();
});
services.Configure<GzipCompressionProviderOptions>(options =>
{
options.Level = CompressionLevel.Optimal;
});
services.Configure<BrotliCompressionProviderOptions>(options =>
{
// Found via Fiddler that 'Optimal' is very slow
options.Level = CompressionLevel.Fastest;
});
services.AddControllers()
.ConfigureApplicationPartManager(manager =>
{
manager.FeatureProviders.Add(new CustomControllerFeatureProvider());
})
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressInferBindingSourcesForParameters = true;
options.SuppressModelStateInvalidFilter = true;
})
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Ignore;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseResponseCompression();
app.UseCors("CorsPolicy");
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(name: "JSON API", pattern: "json", defaults: new { controller = "json" });
});
}
I've tried all sorts: removing any other Feature Providers, specifically mapping the route to the JsonController. Am i missing something here?
self-hostwhich can be multiple? looks like you run multiple instances of your web app, each one has a different configuration (with some controllers enabled/disabled)? For one application I've tried, it seems working to disable/enable specific controllers (of course you need to remove the defaultControllerFeatureProviderbefore adding your custom one, otherwise the default provider will still take effect and enable all controllers).