Injecting IRequest into GlobalRequestFilter

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?

You can just inject it if you want your dependency injected with the IRequest in a Global Request Filter, i.e:

GlobalRequestFilters.Add((request, response, dto) => {
    var something = request.TryResolve<ICurrentCaller>();
    something.Request = request;
});

Yes, I can, but that pre-supposes that the class that implements ICurrentCaller has a IRequest property.
And even if I check if the class implements IRequiresRequest, then that pre-supposes the possibility of IRequiresRequest

Neither of these things maintains modularity.

But going through all dependencies in the container does.

I was hoping for 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?

You can override TryResolve<T> in your AppHost but that doesn’t have access to the IRequest which is only valid within the scope of a Request. The only way you can access it from a singleton method is to create a IRequest from the HttpContext.Current.ToRequest() singleton, but that only works in ASP.NET Hosts and should Ideally be avoided.

I’d personally avoid coupling dependencies to IRequest as it limits its reuse and testability to the context of a Request which is why I prefer to inject the info from the IRequest I need as a param instead of trying to couple the construction of the dependency to only be available within the scope of a Request.

So if you don’t want to manually inject the IRequest you’ll need to follow a similar reflection approach to injecting IRequest inside your Global Filters, i.e:

var something = request.TryResolve<ICurrentCaller>();
something.InjectRequestIntoDependencies(request);

Where you can save an extra line at each call-site by wrapping these 2 lies behind a custom “Resolve” extension method.