views:

69

answers:

2

I have a [Logger] attribute for my asp.net-mvc projects. This LoggerAttribute takes a ILoggerService in it's constructor.

For regular objects (e.g. my controllers) it works, but attributes don't seem to be resolved.

How can I have Turbine inject the ILoggerService?

+2  A: 

Attribute arguments must be constant expressions, so you cannot instantiate an object to be given to an attribute.

http://msdn.microsoft.com/en-us/library/5y0xyec6.aspx

What I would do is require your controller to accept a ILoggerServer in it's constructor and have Turbine inject into your controller's constructor.

David
To add to this correct answer, you have to think about why this is the case. Attributes are embedded in metadata in the assembly and apply to the type, not instances of the type. If attribute parameters were not constant, this could not be the case.
Jason
+2  A: 

Is your Logger attribute an Action|Result|Error|Authorization filter? If so, you can use the InjectableFilterAttribute class to specify the type of the filter you want Turbine to inject for you. This way you can support ctor injection to your services.

To see how this is done, check out the Filter Injection sample that ships with Turbine.

Javier Lozano
Nice :) Excactly what I was looking for
borisCallens