nancy - 访问模块外部的 NancyContext

标签 nancy

我有一个“服务”类,它在我的 Nancy 模块之外执行某些逻辑。服务类已注册到容器中,以便我可以通过构造函数注入(inject)访问模块中的实例。如果我的类依赖于 NanyContext,我如何从模块外部访问它?

最佳答案

NancyContext 是根据请求创建的,因此只有当您的服务范围不超过请求时,对其进行依赖才有意义。否则,您必须将 NancyContext 与您对服务进行的方法调用一起传递。

如果服务具有请求范围,您可以在 BootstrapperConfigureRequestContainer 中创建并注册它:

public class Bootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
    {
         var service = new Service(context);
         container.Register(service);
         base.ConfigureRequestContainer(container, context);
    }
}

关于nancy - 访问模块外部的 NancyContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26434591/

相关文章:

nancy - 远程访问 Nancy Self Host

c# - 其他 Nancy.Testing.Browser GET/PUT/POST/DELETE

url-routing - NancyFX Catch All 路线

unit-testing - 当 razor 布局中包含 @using 语句时,为什么 Nancy.Testing 会失败?

.net - 当 OWIN-self-host 不需要时,为什么 Nancy 需要 localhost 的 URLACL?

南希使用 JSON 的复杂对象不起作用

.net - "Oh noes!"南希测试异常

nancy - 使用 AutoFac Bootstrapper 在 Nancy 注册启动类

c# - 使用 Nancy TinyIoC 配置 JsonNetSerializer 和 JsonNetBodyDeserializer

Nancy 框架示例应用程序