C# IOC and Request State in NancyFX

2019-09-12 10:09发布

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

标签: c# nancy tinyioc
1条回答
姐就是有狂的资本
2楼-- · 2019-09-12 10:48

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.

查看更多
登录 后发表回答