Reputation: 4603
I have an ASP.NET Web API service where I am trying to inject information about the request into a "logging context" which can be reused by objects that have no idea they are part of a web service. The goal is that every message logged is associated with a set of data points taken from the request that spawned it.
The context itself is generated by a Message Handler very early on and then added to Request.Properties
. The problem I'm having is that in the controller constructor the Request
property on the controller hasn't been set yet, so I can't retrieve the context and use it to appropriately configure the logging.
I think I could do it with a filter: In OnActionExecuting
the filter could check if the controller implemented an interface defining "PreAction" and if so it could call that method, but I'm not sure if that's duplicating functionality provided by the framework.
Question: Is there a built-in way to run a method after the constructor has been run and the Request
property has been populated but before the action method executes?
Upvotes: 4
Views: 1151
Reputation: 10576
As option you could use Microsoft Unity IoC container and use Interception functionality for that. http://msdn.microsoft.com/en-us/library/ff647107.aspx
You can intercept any method with it.
Upvotes: 1