Skip to main content
added 175 characters in body
Source Link
Leniel Maccaferri
  • 102.7k
  • 48
  • 381
  • 495

You can inject dependencies in attributes like AuthorizeAttribute in this way

var someservice = (ISomeService)context.HttpContext.RequestServices.GetService(typeof(ISomeService)); 

#######

...more succinct way to write the above code by @Code Ranger's comment below:

var someservice = context.HttpContext.RequestServices.GetService<ISomeService>(); 

You can inject dependencies in attributes like AuthorizeAttribute in this way

var someservice = (ISomeService)context.HttpContext.RequestServices.GetService(typeof(ISomeService)); 

You can inject dependencies in attributes like AuthorizeAttribute in this way

var someservice = (ISomeService)context.HttpContext.RequestServices.GetService(typeof(ISomeService)); 

#######

...more succinct way to write the above code by @Code Ranger's comment below:

var someservice = context.HttpContext.RequestServices.GetService<ISomeService>(); 
Source Link

You can inject dependencies in attributes like AuthorizeAttribute in this way

var someservice = (ISomeService)context.HttpContext.RequestServices.GetService(typeof(ISomeService));