如何替换 Unity 解决方案中过时的 Microsoft.Practices.Unity.Configuration.ContainerElement.Configure 方法?

发布于 2024-10-11 03:08:44 字数 1363 浏览 2 评论 0原文

我有一个正在运行且运行良好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-10-18 03:08:44

我已经设法让它发挥作用。秘密在于使用使用容器名称来配置每个容器元素的Configure 方法的重载:

foreach (ContainerElement containerElement in section.Containers)
{
    container = section.Configure(container, containerElement.Name);
}

到目前为止,这看起来好像它所做的与过时的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:

foreach (ContainerElement containerElement in section.Containers)
{
    container = section.Configure(container, containerElement.Name);
}

So far this looks as if it's doing the same as the obsolete Container.Configure method was doing - everything is working as expected.

靑春怀旧 2024-10-18 03:08:44

如果配置中的部分没有名称,则将其视为默认名称,您无需指定名称:

<containers>
  <container>
    <types>
      <type type="IObject" mapTo="Object" />
    </types>
  </container>
</containers>

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:

<containers>
  <container>
    <types>
      <type type="IObject" mapTo="Object" />
    </types>
  </container>
</containers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文