http-headersservicestackfunq

Accessing IHttpRequest or IRequestContext from container


I need to have the resolution of a dependency for my services to be based off the value of an HTTP header in the incoming request.

I've tried registering a factory method like so:

container.Register(c => GetDependencyForRequest(c.Resolve<IHttpRequest>()));

and also I've tried:

container.Register(c => GetDependencyForRequest(c.Resolve<IRequestContext>()));

However, both throw ResolutionExceptions.

I'd prefer not to have to saddle my Services with deciding which implementation to use. I'd just like them to have an IDependency in their constructor and let the container resolve it.

Is there some way to do this? Or is there another way to go about this?


Solution

  • The answer was much simpler than I thought:

    container.Register(c => FindDependencyForRequest(HttpContext.Current.ToRequestContext()));