ASP.NET-Unity-从外部配置文件读取配置部分 [英] ASP.NET - Unity - Read configuration section from external configuration file
问题描述
我想将Unity集成到我的应用中,并且希望它使用外部配置文件
I want to integrate Unity in my app and I want it to use an external config file
Unity初始化代码为
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);
我还有一个统一的外部配置文件,名为"unity.config",具有以下内容
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>
问题是,在我要加载unitySection的行中,GetSection()返回null.
The problem is, in the line where I want to load unitySection, GetSection() returns null.
可能是什么问题?
编辑
我添加了
<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 = //...
在初始化时,该FilePath属性的配置变量是C:\ Program Files(x86)\ IIS Express \ unity.config,而不是我自己的unity.config文件
The configuration variable in initialized that FilePath property is C:\Program Files (x86)\IIS Express\unity.config, and not my own unity.config file
有什么想法如何从Web文件夹引用配置文件?
Any idea how to reference the config file from web folder?
谢谢.
推荐答案
要访问Web文件夹,请使用HttpServerUtility.MapPath方法.
To access the web folder, use the HttpServerUtility.MapPath method.
var mappedConfig = Server.MapPath("~/unity.config");
Server是Page的属性,或使用HttpContext.Current.Server.
Server is a property of the Page, or use HttpContext.Current.Server.
这篇关于ASP.NET-Unity-从外部配置文件读取配置部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!