I’ve got a MVC attribute that I need to use property injection vs constructor injection. I’m using structure map to do the injection via an xml config. The property never gets set, anyone see what I’m doing wrong? thanks
public class RequirePermissionAttribute: AuthorizeAttribute, IAuthorizationFilter
{
#region Fields
private readonly IPermissionService _permissionService;
#endregion
#region Properties
[SetterProperty]
public IPermissionService PermissionService
{
get
{
return _permissionService;
}
set
{
value = _permissionService;
}
}
….
Config file
<DefaultInstance PluggedType="Service.Permissions.PermissionService,Service" PluginType="Service.Permissions.IPermissionService,Service" Scope="HttpContext" />
I guess that
RequirePermissionAttributeis not resolved by StructureMap so it can set your property. Attributes are compile time so you have to take additional steps to enable this.I don’t know if you’re using a ControllerFactory or a DependencyResolver to enable the DI but with a ControllerFactory I used to supply an implementation of
ActionInvokerproperty.you can find some informations here : http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/. It’s old but still applicable.