在TinyIOC中注册依赖项以在NancyFX中使用 [英] Registering dependencies within TinyIOC for use in NancyFX

查看:123
本文介绍了在TinyIOC中注册依赖项以在NancyFX中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have another newbie question regarding registering additional dependencies within TinyIoc for use within NancyFX.

运行该应用程序时,我继续收到以下异常情况...

I am continuing to get the following exceptions when running the application...

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: 

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

I am currently registering my dependencies incorrectly, but I cannot seem to figure out the correct way. Below is my code within my custom bootstrapper. Also note that I am not currently calling the base.ConfigureRequestContainer method because I cannot seem to figure out how to get the current context to pass into it.

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])
}

任何帮助都将不胜感激……显然我的无知无止境:)

Any help would really be appreciated...apparently my ignorance has no limits :)

推荐答案

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

Ok, not 100% sure where to start.. you don't need the context because you're doing it wrong :-)

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

Firstly, why are you calling "configure request container" at all, and why are you creating a child container? You don't do that :-) There are two scopes, application scope, configured by overriding ConfigureApplicationContainer, and request scope, configured by overriding ConfigureRequestContainer, you don't call them yourself, you just override them depending on how you want to scope your objects.

第二,默认的Nancy bootstrapper将在它的ConfigureApplicationContainer的默认实现中自动注册"它可以进行的所有操作.通过在您进行手动注册后调用基本" ,您可以通过自动注册有效地复制原始注册.要么不打基础,要么在您手动注册之前先打基础.同样,不要从您的ConfigureApplicationContainer调用ConfigureRequestContainer:-)

Secondly, the default Nancy bootstrapper will "autoregister" everything it can in its default implementation of ConfigureApplicationContainer. By calling "base" after you've made a manual registration you are effectively copying over your original registration by autoregister. Either don't call base, or call it before you do your manual registrations. And, again, don't call ConfigureRequestContainer from your ConfigureApplicationContainer :-)

如果您不关心应用程序范围内的所有事情(因此singetons为每个请求获取相同的实例),则您不需要任何这些,您可以依靠自动注册.

If you don't care about everything being application scoped (so singetons get the same instance for each request) then you don't need any of this, you can just rely on autoregister.

您当前正在手动构造对象并将其放入容器中,这似乎是一种很奇怪的方法.通常,您只需要注册类型,然后让容器在需要时进行实例化即可.

You're currently constructing your objects manually and putting them into the container, that seems a rather odd way to do it. Normally you'd just register the types and let the container handle instantiating as and when it needs to.

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

You're not overriding ConfigureRequestContainer, you are just creating a new method (with a different signature).

所以,您可能想要的是这样的东西:

So, what you probably want is something like:

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>();
}

这篇关于在TinyIOC中注册依赖项以在NancyFX中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆