编程语言
首页 > 编程语言> > c# – 升级到Enterprise Library 6.0,提出’EnterpriseLibraryContainer’问题

c# – 升级到Enterprise Library 6.0,提出’EnterpriseLibraryContainer’问题

作者:互联网

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

private static IUnityContainer container = 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?

谢谢你的帮助

解决方法:

典型的方法是引导块,使用Unity注册适当的对象并让Unity注入依赖项.

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

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,另一种方法是引导块,然后使用静态Facade方法(例如Logger.Write())替换对EnterpriseLibraryContainer.Current.GetInstance<>()的调用.

标签:c,enterprise-library
来源: https://codeday.me/bug/20190529/1177783.html