C# IOC and Request State in NancyFX

∥☆過路亽.° 提交于 2019-12-11 14:33:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!