I am pretty new to Unity & IoC in general & as usual, I have quickly got myself into a bind…
I have created an Authorization Filter Attribute for the ASP.NET Web API Beta. I now need to inject my Authorizer into the Attribute however since this is an attribute I cannot simply do this public TestAuthAttribute(IAuthorizer Authorizer) in my constructor.
So I then decided to create a public property decorated with the [Dependency] attribute for property injection however it does not get resolved.
Here is the code:
public class TestAuthAttribute : AuthorizationFilterAttribute
{
[Dependency]
public IAuthorizer Authorizer { get; set; }
public TestAuthAttribute() {
...
}
private bool authorizeCore(HttpRequestMessage request)
{
if (Authorizer == null)
throw Error.ArgumentNull("Null Authorizer"); // <<<<< this is null
}
When the controller is decorated with the [TestAuth] the Attribute is triggered but the Authorizer is not resolved, it is null)
I have placed the following code in my controller & Authorizer does get resolved…
[Dependency]
public IAuthorizer Authorizer { get; set; }
Why is this dependency not resolved in my AuthorizationFilterAttribute & how would you go about Injecting the Authorizer into the AuthorizationFilterAttribute?
Full disclosure: I have not used Turbine.
Having said that, I think it might solve your problem for you or at least show you how to solve it.
They have a Unity Nuget package here: http://nuget.org/packages/MvcTurbine.Unity
And you can find more detail on their codeplex site here: http://mvcturbine.codeplex.com/
Hope that helps.