私は、autofacのCaptive Dependency問題を解決する最もクリーンな方法を探しています。Autofacとのキャプティブ依存関係
私はLifeTimeScopeごとregisterdされる短命クラスがあります。
public class ShortLived
{
public void DoSomethingUsefull() {}
}
をそして私は、単一のインスタンスとして登録されます長く生きクラスを持っています。これはShortLivedクラスに依存しています:
public class LongLived
{
private readonly Func<ShortLived> _getCurrentShortLived;
public LongLived(Func<ShortLived> getCurrentShortLived)
{
_getCurrentShortLived = getCurrentShortLived;
}
public void DoSomethingWithShortLived()
{
var currentShortLived = _getCurrentShortLived();
currentShortLived.DoSomethingUsefull();
}
}
次の試みは機能しません。 Autofac.Core.DependencyResolutionExceptionがスローされます。
public void CaptiveDependencyTest()
{
var builder = new ContainerBuilder();
builder.RegisterType<LongLived>()
.SingleInstance();
var container = builder.Build();
using (var scope = container.BeginLifetimeScope(b => b.RegisterType<ShortLived>()))
{
var longLived = scope.Resolve<LongLived>();
longLived.DoSomethingWithShortLived();
}
}
次のように動作します。しかし、私は実際には何とか静的変数に頼るよりも、問題に対するより良い解決策があることを願っています。
private static ILifetimeScope currentLifetimeScope;
public void CaptiveDependencyTest2()
{
var builder = new ContainerBuilder();
builder.Register(c =>
{
Func<ShortLived> shortLivedFacotry =() => currentLifetimeScope.Resolve<ShortLived>();
return new LongLived(shortLivedFacotry);
})
.SingleInstance();
var container = builder.Build();
using (var scope = container.BeginLifetimeScope(b => b.RegisterType<ShortLived>()))
{
currentLifetimeScope = scope;
var longLived = scope.Resolve<LongLived>();
longLived.DoSomethingWithShortLived();
}
}
いくつかのbackroundのに関する情報: 私はOWINに取り組んでいるがASP.Net WebApi2 Microserviceを開催しました。他のサービスを呼び出すときは、currentOwinContext.Request.User.Identityから値を読み取り、それらを次のサービスに送るRequestMessageに追加する必要があります。私のLongLivedクラスはDelegatingHandler(つまり、HttpClient "HttpMessageHandler-Pipeline"の一部)であり、HttpClientは.SingleInstance()である必要があるので、私が行う各リクエストに対して新しいHttpClientをインスタンス化する必要はありません。 ShortLivedクラスはIOwinContextで、OwinパイプラインのLifeTimeScopeに登録されています。
currentLifeTimeScopeの静的変数の代わりに、私はautofacにHttpConfigurationを登録することができました。次に、httpConfig.DependencyResolver.GetRequestLifetimeScope()でcurrentLifeTimeScopeを取得できます。私はまだこのアプローチをテストしていません。私はまだ何かを清潔にしたいと思っています。タイプTがコンテナに登録されている場合