ASP.NET - Unity - 从外部配置文件读取配置部分

发布于 2025-01-06 12:45:51 字数 1711 浏览 0 评论 0原文

我想将 Unity 集成到我的应用程序中,并且希望它使用外部配置文件

Unity 初始化代码是

var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "unity.config" };
System.Configuration.Configuration configuration =
ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

// *** problem starts here ***
var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
var container = new UnityContainer().LoadConfiguration(unitySection);

我还有用于 Unity 的外部配置文件,名为“unity.config”,包含以下内容

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <alias alias="IEmailConfigurator" type="Server.Common.Interfaces.IEmailConfigurator, Server.Common" />
  <alias alias="EmailConfigurator" type="Server.Common.EmailConfigurator, Server.Common" />

  <namespace name="Server.Common.Interfaces" />
  <namespace name="Server.Common" />

  <container>
    <register type="IEmailConfigurator" mapTo="EmailConfigurator" />
  </container>
</unity>  

问题是,在以下行中我想加载unitySection,GetSection()返回null。

可能是什么问题?


编辑

我添加了

<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
        </configSections>

但是,现在当我跟踪代码时,行中

System.Configuration.Configuration configuration = //...

的配置变量初始化了 FilePath 属性是 C:\Program Files (x86)\IIS Express\unity.config,而不是我自己的unity.config 文件

知道如何从 web 文件夹引用配置文件吗?

谢谢。

I want to integrate Unity in my app and I want it to use an external config file

The Unity initialization code is

var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = "unity.config" };
System.Configuration.Configuration configuration =
ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

// *** problem starts here ***
var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");
var container = new UnityContainer().LoadConfiguration(unitySection);

Also I have external config file for unity, named "unity.config" with the following content

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <alias alias="IEmailConfigurator" type="Server.Common.Interfaces.IEmailConfigurator, Server.Common" />
  <alias alias="EmailConfigurator" type="Server.Common.EmailConfigurator, Server.Common" />

  <namespace name="Server.Common.Interfaces" />
  <namespace name="Server.Common" />

  <container>
    <register type="IEmailConfigurator" mapTo="EmailConfigurator" />
  </container>
</unity>  

The problem is, in the line where I want to load unitySection, GetSection() returns null.

What could be the problem?


EDIT

I added

<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
        </configSections>

However, now when I trace through code, in line

System.Configuration.Configuration configuration = //...

The configuration variable in initialized that FilePath property is C:\Program Files (x86)\IIS Express\unity.config, and not my own unity.config file

Any idea how to reference the config file from web folder?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

舂唻埖巳落 2025-01-13 12:45:51

http://msdn.microsoft.com/en -us/library/ff660935%28v=pandp.20%29.aspx

您的外部配置文件也必须从 标签,而不是 标签

http://msdn.microsoft.com/en-us/library/ff660935%28v=pandp.20%29.aspx

Your external config file must also start from <configuration> tag, not <unity> tag

唱一曲作罢 2025-01-13 12:45:51

您是否尝试将 元素添加到您的配置文件中?

Did you try to add the <configuration> and <configSections> element to your config file?

清旖 2025-01-13 12:45:51

我不知道你是否已经解决了这个问题。在 Visual Studio 中,您必须确保“Unity.config”具有以下文件属性:

  • Build Action -> 将“内容”
  • 复制到输出目录-> “如果较新则复制”或“始终复制”

我建议“始终复制”以确保构建后始终存在当前的 Unity 配置。

I don't know if you solved this yet. In Visual Studio, you have to ensure the following file properties for "Unity.config":

  • Build Action -> "Content"
  • Copy to Output Directory -> "Copy if newer" or "Copy Always"

I suggest "Copy Always" to ensure the current Unity configuration is always present after a build.

云归处 2025-01-13 12:45:51

要访问 Web 文件夹,请使用 HttpServerUtility.MapPath 方法。

var mappedConfig = Server.MapPath("~/unity.config");

Server 是Page 的一个属性,或者使用HttpContext.Current.Server。

To access the web folder, use the HttpServerUtility.MapPath method.

var mappedConfig = Server.MapPath("~/unity.config");

Server is a property of the Page, or use HttpContext.Current.Server.

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