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

标签 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: 

我当前错误地注册了我的依赖项,但似乎无法找出正确的方法。以下是我的自定义 bootstrap 中的代码。还要注意,我目前未调用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])
}

任何帮助将不胜感激...显然我的无知是没有限制的:)

最佳答案

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

首先,为什么要完全调用“配置请求容器”,为什么要创建子容器?您不这样做:-)有两个作用域,即应用程序作用域(通过覆盖ConfigureApplicationContainer配置)和请求作用域(通过覆盖ConfigureRequestContainer配置),您不必自己调用它们,而只是根据想要的作用域覆盖它们你的对象。

其次,默认的Nancy bootstrapper将“自动注册”它在ConfigureApplicationContainer的默认实现中可以执行的所有操作。进行手动注册后,通过调用“base”,可以有效地通过自动注册来复制原始注册。要么不打基础,要么在您手动注册之前先打基础。同样,不要从您的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>();
}

关于nancy - 在TinyIOC中注册依赖项以在NancyFX中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9571635/

相关文章:

json - 使用 Dapper ORM (.NET Core) 将 JSON 插入 PostgreSQL 数据库

asp.net - 禁用 ASP.NET HttpHandler 响应缓存

c# - 使用 TinyIoC 解析带有构造函数参数的具体类型

xamarin.ios - TinyIoC.TinyIoCResolutionException : Unable to resolve type: on ipad deployment 问题

c# - 在 .NET Core 中通过键解析动态注册的服务

c# - AsPerRequestSingleton() 使用带有 TinyIOC 容器的工厂方法注册

c# - TinyIoC - 接口(interface)的多种实现

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

c# - 使用 Nancy self host + TopShelf 的空白回复

unit-testing - 南希FX : Can I force my unit-test browser to be authenticated by default?