【发布时间】:2018-01-22 22:29:28
【问题描述】:
我们有一个新的 ASP.NET Core 2.0 应用程序,但我们需要使用 .NET Framework 4.6.1 库,该库需要访问 Web.config(或类似)中定义的自定义 ConfigSection .初始尝试导致错误,表明内部组件无法加载/找到该部分。这在非核心应用程序中运行良好,我们希望有一种方法可以在核心中实现这一点。
这是一个如何在配置文件中注册的示例。
<configSections>
<section name="data-connector" type="Connector.Configuration.ConnectorConfigSection, Connector" />
</configSections>
如果有帮助,组件将使用以下代码加载其配置部分。
/// <summary>
/// Loads an instance of a specified configuration section or a default instance if one was not found
/// </summary>
/// <typeparam name="T">The Configuration Section type to be loaded</typeparam>
/// <param name="sectionName">The name/path of the section to be loaded as seen in the config file</param>
public static T GetSectionInstance<T>(string sectionName) where T : ConfigurationSection
{
if (string.IsNullOrEmpty(sectionName)) throw new ArgumentNullException("sectionName");
var instance = (ConfigurationSection)ConfigurationManager.GetSection(sectionName);
return (instance == null || !typeof(T).IsAssignableFrom(instance.GetType()))
? Activator.CreateInstance<T>()
: instance as T;
}
我们可以要求组件所有者对其加载过程进行更改,但我们不能将其设为原生核心组件。
是否有解决方法来确保仍然可以支持 web.config 部分?
【问题讨论】:
-
你可以访问设置文件源代码(通常由VS生成)吗?如果是,也许您可以将其移植到 .NET Core?加载逻辑应该很基本吧?
-
抱歉 Meikel,这不是我们的选择。
标签: c# asp.net-core