In my ASP.NET Core project, I currently inject an IFilterMetadata
dependency into MvcOptions
in the ConfigureServices
method in the following way:
public override IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddProjectSpecificStuff();
IExceptionFilter exceptionFilter = null;
services.AddMvc(options => { options.Filters.Add(exceptionFilter); });
var provider = base.ConfigureServices(services);
exceptionFilter = provider.GetService<IExceptionFilter>();
return provider;
}
This works, but causes code analyzers such as ReSharper to complain about access to modified closure.
Is there an alternative to achieve the same dependency injection without using modified closure?
When your filters have dependencies, just register them with the generic add method instead of passing an instance.