これをどのように尋ねればよいかよくわからないので、コード サンプルを投稿して、私がやろうとしていることを簡単に説明します。次のバインディング設定があります。
kernel.Bind<IAuthenticationService>().To<FormsAuthenticationService>();
kernel.Bind<IAuthenticationService>().To<TokenAuthenticationService>().When(r => HasAncestorOfType<MyWebApiController>(r));
HasAncestorOfType のコードは次のとおりです (ここでは関係ないと思いますが)。
private static bool HasAncestorOfType<T>(IRequest request)
{
if (request.Target == null)
return false;
if (request.Target.Member.ReflectedType == typeof(T))
return true;
return HasAncestorOfType<T>(request.ParentRequest);
}
これらのバインディングは両方とも意図したとおりに機能します (に注入されない限り にIAuthenticationService
バインドされ、その場合は にバインドされます)。ただし、次のようなファクトリ バインディングを作成して、から作成されたオブジェクトにバインドされるようにしたいと思います。FormsAuthenticationService
MyWebApiController
TokenAuthenticationService
ICurrentCompany
IAuthentcationService
kernel.Bind<ICurrentCompany>().ToMethod(x => new Company { CompanyId = x.Kernel.Get<IAuthenticationService>().User.CompanyId});
これはうまくいきません。IAuthenticationService
は常に にバインドされFormsAuthenticationService
ます。