问题
I want to inject a class into my module using the IOC framework in NancyFX, and I want that class to have access to the Context, just like the module does.
How would I do that?
The module has a property called Context, that doesn't seem to be injected (nor should it be, as it is the request state)... how does that work, and is that thread safe?
Thanks
回答1:
Assuming you use the DefaultNancyBootstrapper
as a base for your bootstrapper you register your dependency in your container on a per request basis by overriding ConfigureRequestContainer
:
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
container.Register(new MyDependency(context));
}
}
If you use a different container, the implementation will differ slightly.
来源:https://stackoverflow.com/questions/28676832/c-sharp-ioc-and-request-state-in-nancyfx