运行单元测试时,无法正确读取配置文件。但是,当从控制台应用程序上下文运行时,会读取配置文件。
在下面的代码中,在赋值后访问var 部分时,运行测试 TestIoCInit() 时为空,从控制台应用程序调用 InitIoC() 时不为空。
[TestMethod]
public void TestIocInit()
{
InitIoC();
}
internal static void InitIoC()
{
IUnityContainer unityContainer = new UnityContainer(); // the host app domain creates the unity container and pass it to the resolver, the resolver is a static member of IoC class, thus the container is static
ServiceLocator.SetLocatorProvider(unityContainer);
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(unityContainer);
var unityResolver = new UnityDependencyResolver(unityContainer);
IoC.Initialize(unityResolver);
new IoCTypeRegistrationTask(unityContainer).Execute();
}
感谢你的帮助!