I'm setting up dependency injection in my ASP.NET Web API application. I have a scenario where a dependency has a dependency on something on the current request (the hostname, for example).
So how can I use the current request as a dependency?
Below is what I've been able to come up with, but HttpContext.Current
won't work in a self-hosted scenario.
kernel.Bind<IFoo>()
.ToMethod((context) =>
{
string hostName = HttpContext.Current.Request.Url.Host;
var foo = FooFactory.GetByHostName(hostName);
return foo;
})
.InRequestScope();
I'm currently using Ninject, but I can switch to a different IoC container if there's one that can handle this scenario better.