如何替换 Unity 解决方案中过时的 Microsoft.Practices.Unity.Configuration.ContainerElement.Configure 方法?
我有一个正在运行且运行良好的 Unity 应用程序,但我们目前正在通过处理所有编译警告来清理我们的代码。
由于过时的 Microsoft.Practices.Unity.Configuration.ContainerElement.Configure 方法,我收到以下代码片段的警告:
var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection(SectionName);
if (section != null)
{
var container = new UnityContainer();
foreach (ContainerElement containerElement in section.Containers)
{
containerElement.Configure(container);
}
Container = container; // Set the main container
}
我想按照建议将其替换为 UnityConfigurationSection.Configure 方法,但看不到它们是由于位于对象层次结构的不同级别,因此是等效的。
我已经尝试过了:
var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection(SectionName);
if (section != null)
{
IUnityContainer container = new UnityContainer();
container = section.Configure(container);
Container = container; // Set the main container
}
但是由于空引用而失败了。
我应该如何更新代码以消除对过时方法的使用?
I have a Unity application that is running and working perfectly but we are currently in the process of cleaning up our code by acting on all the compilation warnings.
I get a warning on the following piece of code because of the obsolete Microsoft.Practices.Unity.Configuration.ContainerElement.Configure method:
var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection(SectionName);
if (section != null)
{
var container = new UnityContainer();
foreach (ContainerElement containerElement in section.Containers)
{
containerElement.Configure(container);
}
Container = container; // Set the main container
}
I'd like to replace it with the UnityConfigurationSection.Configure method as suggested but can't see that they are equivalent because of being at different levels of the object hierarchy.
I've tried:
var map = new ExeConfigurationFileMap { ExeConfigFilename = GetConfigFolderForFile("unity.config") };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection(SectionName);
if (section != null)
{
IUnityContainer container = new UnityContainer();
container = section.Configure(container);
Container = container; // Set the main container
}
but that falls down with null references.
How should I be updating the code to eliminate use of the obsolete method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经设法让它发挥作用。秘密在于使用使用容器名称来配置每个容器元素的Configure 方法的重载:
到目前为止,这看起来好像它所做的与过时的Container.Configure 方法所做的相同 - 一切都按预期工作。
I have managed to get this to work. The secret was using the overload of the Configure method that takes a container name to configure each container element:
So far this looks as if it's doing the same as the obsolete Container.Configure method was doing - everything is working as expected.
如果配置中的部分没有名称,则将其视为默认名称,您无需指定名称:
If the section in the config doesn't have a name, it takes it as default and you don't need to specify a name: