Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behavior different when "externalizing" certain config settings into external files

I have a legacy ASP.NET 4.0 Webforms application that's been in production for a while. I'm now adding additional functionality in the form of a WebAPI REST service to it.

Adding the WebAPI NuGet packages also added an entry into my web.config configuring the NewtonSoft.Json package runtime version:

Now, since I have my config "compartementalized", I wanted to put this into a separate runtime.config file and reference it from the main web.config:

<runtime configSource="runtime.config" />

When I do this, suddenly my registration of WebAPI routes in global.asax.cs

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

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

fails with an exception:

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

To me, it seems as if the externalized runtime.config is not being read at the same time as the contents of the web.config itself...... which is rather surprising to me, I would have expected that the whole web.config including any "externalized" sub-config files would be read before any code in global.asax.cs is being executed...

Any insights? I don't even know where to go search for this level of detailed information on MSDN ....

like image 774
marc_s Avatar asked Nov 09 '22 06:11

marc_s


1 Answers

web.config contains configuration info for a lot of different parts of the Windows web stack.

Some of it tells IIS what to do, some of it tells .NET what to do, some of it tells your application what to do. As such, different elements behave very differently depending on which part of the stack they target.

<runtime> is pretty low level, see 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" is actually parsed by .NET itself, 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.

So basically .NET is run with the specified <runtime> settings before it will parse configSource.

If you play around with the web.config file in Visual Studio you will see that intellisense will tell you which attributes can go where.

like image 171
Jason Elkin Avatar answered Nov 15 '22 12:11

Jason Elkin