Do binding redirects in app.config for class libraries do anything?

C#.NetNugetApp ConfigAssembly Binding-Redirect

C# Problem Overview


The VS solutions I often work with consist of a single executable project (console app, web app) and many class library projects that are all referenced by the executable.

When working with NuGet and installing packages, there's often an app.config file created for each project, usually containing nothing else than a list of binding redirects that consolidate versions of referenced assemblies. Sometimes there's some third-party library-specific content (like Entity Framework config section), but let's leave that aside for now.

When I build the solution and use the binaries of the main executable project, I see all the class library project assemblies in the build output together with the corresponding *.config files (the app.config file gets renamed to AssemblyName.config when built).

When launching the main executable, do the config files of the class library assemblies take any effect? Or is it just the app.config file of the executable that has an effect in this case? What if there are some binding redirects set up on some of the class library projects, and some different binding redirects set up on the main executable project — How are these combined, which take priority?

I've tried to research this online and from what I've read, it looks to me like the app.config files for non-executable assemblies are useless (with regards to binding redirects). Can someone confirm this or elaborate a bit more on the topic?

If it is that way, is it actually undesirable to have these app.config files created by NuGet in class libraries if they contain just the binding redirects? It feels to me that NuGet shouldn't create those binding redirects for class library projects, as it will only increase the confusion about what settings are actually applied.


I found these existing Stack Overflow questions on the topic, but their accepted answers are actually contradictory even when they're marked as duplicates of each other.

The accepted answer to the first question mentions that app.config files are actually used during compile-time, which means they could have effect. Sources like MSDN and MSBuild source code are cited there as a proof it's used during compile-time. Unfortunately, I'm not proficient enough in MSBuild to understand how it's being used, and if it's really a valid argument.

Can anybody describe an example scenario to prove that an app.config with binding redirects for a class library can do anything?

C# Solutions


Solution 1 - C#

I have multiple applications with similar setup - Web application referencing multiple library projects each having their own nuget packages etc., Based on my personal experience the assembly bindings in the library projects are not considered during run time.

The bindings specified web or app config in the root application (web/console) is that only matters. All my library projects are setup with "Copy to Output Directory" setting as "Do not copy" for the app.config file - that way my output folder is not cluttered with dll and their config files.

Here is the link which says how the assembly is loaded and where is it being searched and the sequence of it. No where in the article they talk about individual project config files.

Hope that helps.

Solution 2 - C#

According to this old msdn article:

> An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration. An application configuration file applies only to a specific application manifest and dependent assemblies. Isolated components compiled with an embedded [ISOLATIONAWARE_MANIFEST_RESOURCE_ID] manifest require a separate application configuration file. Manifests managed with CreateActCtx require a separate application configuration file.

So only dll's with the ISOLATIONAWARE_MANIFEST_RESOURCE_ID set actually use an independent application config, otherwise it's deferred to the main process config file.

For more info on what ISOLATIONAWARE is you can read this other MSDN article that goes more in depth.

> ISOLATIONAWARE_MANIFEST_RESOURCE_ID is used primarily for DLLs. It > should be used if the dll wants private dependencies other than the > process default. For example, if an dll depends on comctl32.dll > version 6.0.0.0. It should have a resource of type RT_MANIFEST, ID > ISOLATIONAWARE_MANIFEST_RESOURCE_ID to depend on comctl32.dll version > 6.0.0.0, so that even if the process executable wants comctl32.dll version 5.1, the dll itself will still use the right version of > comctl32.dll.

Solution 3 - C#

The answer is maybe. Depending on the type of project the library file is. Some library projects run in contexts where the library's config file is respected (e.g. Azure Web Roles), but that is not the norm.

See my answer here for more details.

Solution 4 - C#

Generally there is only one configuration file and thats the configuration file of the executeable (.exe.config, web.config).

Any assembly redirects have to be placed in the configuration file of the executable.

Configuration files of dlls need to be loaded manually using the ConfigurationManager class. See also this question https://stackoverflow.com/questions/5190539/equivalent-to-app-config-for-a-library-dll/5191101#5191101

Solution 5 - C#

No, only the app.config of the executable will have effect. For example, if you have a console app hosting a WCF service, and in your WCF service you make use of, for example, ConfigurationManager.AppSettings, the AppSettings will come from the console host app.config file. If you spin up another console application (ConsoleClient) to try connecting to the ConsoleHost, then in the parts where the ConsoleClient can be said to be "executing" (for example in its main method), it will use ConsoleClient's app.config, but as soon as it begins using the WCF service, the WCF service will delegate to use ConsoleHost's app.config. (Note that this last point is more relevant to the details behind WCF though.)

Surprisingly, msdn provided this great source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/e13194df-6308-4cbe-973c-f6a462f43eae/how-can-wcf-library-dll-access-application-settings?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/e13194df-6308-4cbe-973c-f6a462f43eae/how-can-wcf-library-dll-access-application-settings?referrer=http://social.msdn.microsoft.com/Forums/vstudio/en-US/e13194df-6308-4cbe-973c-f6a462f43eae/how-can-wcf-library-dll-access-application-settings?forum=wcf

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTom PažourekView Question on Stackoverflow
Solution 1 - C#KarunView Answer on Stackoverflow
Solution 2 - C#Roy SanchezView Answer on Stackoverflow
Solution 3 - C#gitboxView Answer on Stackoverflow
Solution 4 - C#JehofView Answer on Stackoverflow
Solution 5 - C#information_interchangeView Answer on Stackoverflow