0

所以我有一个容器,我在其中添加了一个为类定义默认类型的注册表。

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并使用我最后注册的实例。

任何帮助,将不胜感激。提前致谢!

4

1 回答 1

1

Well I found my answer. It looks like only the Container.Instance does not actually contain the definition until the configuration method is complete. This means that in order to override the implementation you will need to call Container.Instance.Configure() a second time with the override. Once i did this it started to work as expected.

于 2016-10-20T19:38:39.257 回答