Autofacのファクトリメソッドサポートを抽象化する正しい方法は何ですか?
私はこの例外を受け取り続けます:
この解決操作はすでに終了しています。ラムダを使用してコンポーネントを登録する場合、ラムダへのIComponentContext'c'パラメーターを保存できません。代わりに、「c」からIComponentContextを再度解決するか、Func<>ベースのファクトリを解決してから後続のコンポーネントを作成します。
これが私の試みです。
public void Register<T>(Func<IFactoryContext, T> factoryMethod)
{
_containerBuilder.Register<Func<Type, T>>(c => {
var ctx = c.Resolve<IComponentContext>();
return request => factoryMethod(new AutofacFactoryContext(ctx));
});
}
私も試しました
public void Register<T>(Func<IFactoryContext, T> factoryMethod)
{
_containerBuilder.Register<Func<Type, T>>(c => {
var ctx = c.Resolve<IComponentContext>();
return request => factoryMethod(new AutofacFactoryContext((IComponentContext)ctx.Resolve(request)));
});
}
を返すメソッドが欲しいのですがT
。
AutofacFactoryContext
はの実装でありIFactoryContext
、これはAutofacの単なるラッピングですIComponentContext
。
結局、これは私が期待している結果です:
bootstrapper.Register<IFoo>(c => new Foobar());
コンテナを使用する場合:
var foobar = container.Resolve<IFoo>();