8

NancyFX内で使用するためにTinyIoc内に追加の依存関係を登録することに関して、もう1つの初心者の質問があります。

アプリケーションの実行時に次の例外が発生し続けます...

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メソッドを呼び出していないことにも注意してください。

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

どんな助けでも本当にありがたいです...どうやら私の無知には限界がありません:)

4

1 に答える 1

40

わかりました。どこから始めればよいか100%わかりません。間違っているのでコンテキストは必要ありません:-)

まず、なぜ「リクエストコンテナの設定」と呼んでいるのですか?また、なぜ子コンテナを作成しているのですか?あなたはそれをしません:-)ConfigureApplicationContainerをオーバーライドすることによって構成されたアプリケーションスコープと、ConfigureRequestContainerをオーバーライドすることによって構成されたリクエストスコープの2つのスコープがあります。自分で呼び出すのではなく、スコープの方法に応じてオーバーライドするだけです。あなたのオブジェクト。

次に、デフォルトのNancyブートストラッパーは、ConfigureApplicationContainerのデフォルトの実装で可能なすべてを「自動登録」します。手動登録後に「ベース」を呼び出すことにより、自動登録によって元の登録を効果的にコピーできます。ベースに電話しないか、手動登録を行う前に電話してください。また、ConfigureApplicationContainerからConfigureRequestContainerを呼び出さないでください:-)

アプリケーションスコープのすべてを気にしない場合(つまり、シンゲトンはリクエストごとに同じインスタンスを取得します)、これは必要ありません。自動登録に頼ることができます。

現在、オブジェクトを手動で作成してコンテナに入れていますが、これはかなり奇妙な方法のようです。通常は、タイプを登録し、必要に応じてコンテナにインスタンス化を処理させます。

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>();
}
于 2012-03-05T18:39:53.463 に答える