This is going to sound like a crazy request (given the predicament), but I have a problem in a custom GlobalRequestFilter that we have, and I am trying to keep things decoupled:
We have this filter that is added to the GlobalRequestFilters collection:
GlobalRequestFilters.Add((request, response, dto) =>
{
... some code
var something = request.TryResolve<ICurrentCaller>();
var userId = something.UserId;
... other stuff
});
The class that implements ICurrentCaller looks like this:
public class CurrentCaller : ICurrentCaller, IRequiresRequest
{
public IRequest Request { get; set; }
public string UserId
{
get { return Request.GetSession().UserAuthId; }
}
}
In our AppHost.cs we are registering CurrentCaller like this:
public void Configure(Container container)
{
container.Register<ICurrentCaller>(x => new CurrentCaller())
.ReusedWithin(ReuseScope.Request);
}
Our problem is that when the GlobalFilter is run ‘CurrentCaller.Request’ is null of course.
We had the same problem with services using the ICurrentCaller as a dependency, and so we had to write code that injects the IRequest into any dependencies of the service that implement IRequiresRequest as described here: How To Inject IRequest into my own type by overriding AppHostBase.OnPreExecuteServiceFilter
How would we approach the same problem for GlobalFilters that use IRequest.TryResolve()?
I am thinking that there could be an override or handler in AppHostBase that I can hook, and then inject the IRequest into any of my own types in the current container that implement IRequiresRequest. (i.e. enumerate all dependencies in the Funq.Container, see if they derive from IRequiresRequest and inject the current IRequest into them. Is that feasible?