在TinyIOC中注册依赖项以在NancyFX中使用

The*_*per 8 nancy tinyioc

关于在TinyIoc中注册其他依赖项以便在NancyFX中使用,我还有另一个新手问题.

运行应用程序时,我将继续获得以下异常...

Unable to resolve type: AdvancedSearchService.Interfaces.IResponseFactory

Exception Details: TinyIoC.TinyIoCResolutionException: Unable to resolve type: AdvancedSearchService.Interfaces.IResponseFactory

Source Error: 
Line 25:             var container = TinyIoCContainer.Current;
Line 26: 
Line 27:             _responseFactory = container.Resolve<IResponseFactory>();
Line 28:           
Line 29: 
Run Code Online (Sandbox Code Playgroud)

我目前正在错误地注册我的依赖项,但我似乎无法弄清楚正确的方法.下面是我的自定义引导程序中的代码.另请注意,我目前没有调用base.ConfigureRequestContainer方法,因为我似乎无法弄清楚如何将当前上下文传递给它.

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    container.Register<IRavenSessionManager>(new RavenSessionManager());
    base.ConfigureApplicationContainer(container);

    ConfigureRequestContainer(container);
}


protected void ConfigureRequestContainer(TinyIoCContainer applicationContainer)
{
    var requestContainer = applicationContainer.GetChildContainer();
    requestContainer.Register<ISearchRepository>(new    SearchRepository(requestContainer.Resolve<IRavenSessionManager>().GetSession()));
    requestContainer.Register<IResponseFactory>(new ResponseFactory(requestContainer.Resolve<ISearchRepository>()));
    //base.ConfigureRequestContainer(requestContainer,[I NEED THE CONTEXT])
}
Run Code Online (Sandbox Code Playgroud)

真的很感激任何帮助......显然我的无知没有限制:)

Ste*_*ins 38

好吧,不是100%确定从哪里开始..你不需要上下文,因为你做错了:-)

首先,为什么要调用"configure request container",为什么要创建子容器?你不这样做:-)有两个范围,应用范围,通过覆盖ConfigureApplicationContainer和请求范围配置,通过重写ConfigureRequestContainer配置,你不自己调用它们,你只需根据你想要的范围覆盖它们你的对象.

其次,默认的Nancy引导程序将在其默认的ConfigureApplicationContainer实现中"自动调整"它所能做的一切.您进行手动注册调用"base" ,您实际上是通过autoregister复制原始注册.要么不打电话,要么在进行手动注册之前调用它.而且,再次,不要从ConfigureApplicationContainer调用ConfigureRequestContainer :-)

如果您不关心应用程序作用域的所有内容(因此singetons为每个请求获取相同的实例),那么您不需要任何此类实例,您可以只依赖自动注册.

您当前正在手动构建对象并将它们放入容器中,这似乎是一种相当奇怪的方式.通常,您只需注册类型,并让容器句柄在需要时进行实例化.

您没有覆盖ConfigureRequestContainer,您只是创建一个新方法(具有不同的签名).

那么,你可能想要的是:

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    base.ConfigureApplicationContainer(container);

    // Autoregister will actually do this for us, so we don't need this line,
    // but I'll keep it here to demonstrate. By Default anything registered
    // against an interface will be a singleton instance.
    container.Register<IRavenSessionManager, RavenSessionManager>();
}

// Need to override this, not just make a new method
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
    // Get our session manager - this will "bubble up" to the parent container
    // and get our application scope singleton
    var session = container.Resolve<IRavenSessionManager>().GetSession();

    // We can put this in context.items and it will be disposed when the request ends
    // assuming it implements IDisposable.
    context.Items["RavenSession"] = session;

    // Just guessing what this type is called
    container.Register<IRavenSession>(session);

    container.Register<ISearchRepository, SearchRepository>();
    container.Register<IResponseFactory, ResponseFactory>();
}
Run Code Online (Sandbox Code Playgroud)

  • Nancy还自动解析模块(和其他类型)中的构造函数依赖项,因此您_never_应该使用_TinyIoCContainer.Current_并直接从中解析.只需在构造函数中添加一个依赖项,然后就可以了!谢谢 (4认同)