Dan Soltesz
Dan Soltesz

Reputation: 886

Asp.net 5 mvc 6 action filter with iserviceProvider dependency

when registering your filter in startup, how can you pass in an instance of IServiceProvider?

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc().AddMvcOptions(o =>
        {

            o.Filters.Add(new MyFilterAttribute(???)); //how to get IServiceProvider
        });
}

public class MyFilterAttribute : ActionFilterAttribute
{
  public MyFilterAttribute(IServiceProvider serviceProvider)
  {
     var myService = (IMyService)serviceProvider.GetService(typeof(IMyService));
  }
}

Upvotes: 1

Views: 843

Answers (1)

Dan Soltesz
Dan Soltesz

Reputation: 886

services.AddMvc().AddMvcOptions(o =>
{
    o.Filters.Add(new ServiceFilterAttribute(typeof(MyFilterAttribute)));

});

factory.RegisterType(typeof(MyFilterAttribute)).InstancePerDependency();

Upvotes: 2

Related Questions