ASP.NET - Unity - 从外部配置文件读取配置部分
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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您是否尝试将
和
元素添加到您的配置文件中?Did you try to add the
<configuration>
and<configSections>
element to your config file?我不知道你是否已经解决了这个问题。在 Visual Studio 中,您必须确保“Unity.config”具有以下文件属性:
我建议“始终复制”以确保构建后始终存在当前的 Unity 配置。
I don't know if you solved this yet. In Visual Studio, you have to ensure the following file properties for "Unity.config":
I suggest "Copy Always" to ensure the current Unity configuration is always present after a build.
要访问 Web 文件夹,请使用 HttpServerUtility.MapPath 方法。
Server 是Page 的一个属性,或者使用HttpContext.Current.Server。
To access the web folder, use the HttpServerUtility.MapPath method.
Server is a property of the Page, or use HttpContext.Current.Server.