c# - 当 "externalizing"某些配置设置进入外部文件时行为不同

标签 c# asp.net-web-api webforms web-config asp.net-4.0

我有一个遗留的 ASP.NET 4.0 Webforms 应用程序,它已经投入生产一段时间了。我现在以 WebAPI REST 服务的形式向其添加附加功能。

添加 WebAPI NuGet 包还在我的 web.config 中添加了一个条目配置 NewtonSoft.Json 包运行时版本:

现在,因为我的配置“分区化”了,所以我想把它放到一个单独的 runtime.config 中。文件并从主目录引用它 web.config :

<runtime configSource="runtime.config" />

当我这样做时,突然我在 global.asax.cs 中注册了 WebAPI 路由

protected void Application_Start(object sender, EventArgs e) 
{
    ...

    // Route #1
    GlobalConfiguration.Configuration.Routes.MapHttpRoute("Route1", "test/{list}/{check}", new { Controller = "Devices" });
    ...
}        

异常失败:

System.IO.FileLoadException was unhandled by user code
Message=the file or assembly "Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" or a dependency could not be found. Source=System.Net.Http.Formatting
FileName=Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed

对我来说,似乎外化了runtime.config未与 web.config 的内容同时阅读本身......这让我很惊讶,我本以为整个web.configglobal.asax.cs中的任何代码之前,包括任何“外部化”的子配置文件都将被读取。正在执行...

有什么见解吗?我什至不知道去哪里搜索 MSDN 上的这种级别的详细信息....

最佳答案

web.config 包含 Windows 网络堆栈的许多不同部分的配置信息。

它有些告诉 IIS 做什么,有些告诉 .NET 做什么,有些告诉应用程序做什么。因此,不同元素的行为非常不同,具体取决于它们针对的是堆栈的哪一部分。

<runtime>水平很低,请参阅this from MSDN :

"Runtime settings specify how the common language runtime handles garbage collection and the version of an assembly to use in configuration files."

configSource="whatever"实际上是由 .NET 本身解析的,see this from MSDN :

In ASP.NET applications, at run time you can assign to the ConfigSource property the name of an alternative configuration file.

基本上 .NET 是使用指定的 <runtime> 运行的解析之前的设置 configSource .

如果您在 Visual Studio 中使用 web.config 文件,您会看到智能感知会告诉您哪些属性可以放在哪里。

关于c# - 当 "externalizing"某些配置设置进入外部文件时行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32089048/

相关文章:

c# - C# 中可空类型的替代方法

c# - 添加时出现 DbUpdateConcurrencyException

c# - Moq:通过参数进行多次测试,一种测试方法

c# - 如何在 Silverlight 中定位标签页的内容?

c# - 注册外部登录 Web API

c# - 为什么在 ASP.NET 中使用中继器?

azure - 如何在 Azure 上为匿名 HTTP API 消息创建代理?

cookies - 将凭据/ token 保存在 cookie 中?

javascript - window.location 未正确设置新值

javascript - 将错误消息传递给 ajax 错误处理程序