Inject into attribute?

521 views Asked by At

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

There are 2 answers

1
Javier Lozano On BEST ANSWER

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.

1
David On

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.