0
所以我有容器中添加一個註冊表,它定義了一個類的默認類型。如何設置覆蓋註冊在結構圖4.4
TestRegistry : Registry{
For<Foo>.Use<Foo>();
}
此註冊表在應用程序初始化期間添加在Web服務的開始處。直接添加我的所有我的註冊表用於應用程序後,我需要覆蓋Foo
類的註冊。所以:
Container.Instance.Configure(x =>
{
x.AddRegistry<TestRegistry>();
var fooInstance = new Foo();
x.For<Foo>.Use(fooInstance);
}
當我去從我的容器
var fromContainer = Container.Instance.GetInstance<Foo>();
現在我又回到null
檢索該實例。如果我使用獲取所有實例:
var fromContainer = Container.Instance.GetAllInstances<Foo>();
我找回兩個結果。每個註冊一個。這是有道理的。
不過,如果我嘗試第二次註冊時,清除登記:
Container.Instance.Configure(x =>
{
x.AddRegistry<TestRegistry>();
var fooInstance = new Foo();
x.For<Foo>.ClearAll().Use(fooInstance);
}
var fromContainer = Container.Instance.GetInstance<Foo>();
這仍然返回null
。我似乎在這裏錯過了一些東西。我希望這可以清除Foo
的所有註冊,並使用我最後註冊的實例。
任何幫助,將不勝感激。提前致謝!