NancyFX中的C#IOC和请求状态 [英] C# IOC and Request State in NancyFX
问题描述
我想使用NancyFX中的IOC框架将一个类注入到我的模块中,并且我希望该类可以访问Context,就像该模块一样.
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.
我该怎么做?
该模块具有一个称为Context的属性,该属性似乎没有被注入(因为它是请求状态,所以也不应该被注入)...它是如何工作的,并且该线程安全吗?
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?
谢谢
推荐答案
假设您使用DefaultNancyBootstrapper
作为引导程序的基础,则可以通过覆盖ConfigureRequestContainer
在每个请求的基础上将依赖项注册到您的容器中:>
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.
这篇关于NancyFX中的C#IOC和请求状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!