Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is <clear></clear> not the same as <clear /> in app.config?

Tags:

c#

Can someone please give me the technical reason why <clear></clear> is invalid in app.config vs <clear />

It's typically used like this :

<connectionStrings>
 <clear/>
 <add etc..... />
</connectionStrings>

Problem is I'm using an installer product (InstallShield) with does xml transformation to app.config files, and it's changing <clear /> to <clear></clear>

This breaks the application (service won't even start)

For now I'm just looking for the technical explanation, but if you have any workaround that would be nice too.

like image 393
JL. Avatar asked Mar 30 '16 10:03

JL.


People also ask

What is clear in web config?

Remarks. You can use the <clear> element to remove all settings from your application that were defined at a higher level in the configuration file hierarchy.

What is the AppSetting section in the web config file?

AppSetting section in the configuration file is a section that allows us to keep configurable and application wide settings (for e.g.: ConnectionString) that an application requires in order to perform the tasks properly. This helps in easy maintenance and deployment of the application.

Does .NET core use app config?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings.


1 Answers

It seems that using <clear></clear> breaks the parsing of the connectionStrings section (and probably others too, if used there). See the comment of Hans Passant for a possible explanation.

If <clear></clear> is present, the ConfigurationManager.ConnectionStrings is empty. So the reason for you service crashing may be, that you don't check if this collection contains any elements or try to access them via ConfigurationManager.ConnectionStrings["nameoftheconnection"] which throws an exception if there is no element with that key.

Possible workarounds (which I admin are both rather clumsy and hacky)

  1. You remove the <clear />. This may lead to some additional connect strings created by some referenced assemblies. But as your own connectionstring should be at the end of this collection, you may be able to remove the unwanted entries.

  2. You add a custom task to your installation routine, which processes the app.config file in one of the later steps and replaces <clear></clear> with <clear />

like image 171
derpirscher Avatar answered Oct 12 '22 22:10

derpirscher