gpt4 book ai didi

c# - 升级到 Enterprise Library 6.0 会出现 'EnterpriseLibraryContainer' 问题

转载 作者:太空狗 更新时间:2023-10-30 00:53:14 25 4
gpt4 key购买 nike

升级到 Enterprise Library 6.0 后,我遇到了以下问题:

私有(private)静态 IUnityContainer 容器 = EnterpriseLibraryContainer.Current.GetInstance();

Cannot resolve EnterpriseLibraryContainer

我在 stackoverflow 上找到了另一篇关于 EnterpriseLibraryCONtainer 的帖子。

在Enterprise Library的升级说明中写到:

“The name ‘EnterpriseLibraryContainer’ does not exist in the current context

The bootstrapping code for all of the blocks has changed in version 6 of Enterprise Library. The blocks no longer use Unity to manage the initialization and configuration, and each block now includes its own bootstrapping code. Any calls to the EnterpriseLibraryContainer.Current.GetInstance method to resolve a type from one of the Enterprise Library blocks should be replaced with the block specific bootstrap code. For example, to create a LogWriter instance based on configuration in the app.config file, you can now use the following code: LogWriterFactory logWriterFactory = new LogWriterFactory(); var logWriter = logWriterFactory.Create();

但我不知道如何处理 IUnityContainer 的情况。我可以使用

IUnityContainer container = new UnityContainer?

谢谢你的帮助

最佳答案

典型的方法是引导 block ,向 Unity 注册适当的对象并让 Unity 注入(inject)依赖项。

例如,如果您正在使用日志记录,那么您将引导 block :

LogWriterFactory logWriterFactory = new LogWriterFactory(); 
LogWriter logWriter = logWriterFactory.Create();

并向 UnityContainer 注册 LogWriter:

IUnityContainer container = new UnityContainer();
// Register LogWriter as singleton
container.RegisterInstance<LogWriter>(logWriter);

如果您使用 EnterpriseLibraryContainer 作为服务定位器并希望继续使用相同的方法,那么您可以创建/包装服务定位器实现或创建静态帮助器方法。 Unity 附带 UnityServiceLocator,您可以重复使用它。

如果您不使用 Unity,另一种方法是引导 block ,然后替换对 EnterpriseLibraryContainer.Current.GetInstance<>() 的调用。使用静态外观方法(例如 Logger.Write() )。

关于c# - 升级到 Enterprise Library 6.0 会出现 'EnterpriseLibraryContainer' 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17315852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com