Can I download web configuration from console application?

My question is pretty simple. I have a third-party library, which suggests that it will be used on the website (this requires special configuration sections in web.config). I would like to use this library in a C # console application. Maybe?

I tried WebConfigurationManager.OpenWebConfiguration(); and also just copy everything in app.config, but this will not work. The error I am getting is a ConfigurationErrorsException, which states that An error occurred creating the configuration section handler for ...: Could not load type 'Configuration.RenderingSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

+4
source share
3 answers

Check OpenMappedExeConfiguration and ExeConfigurationFileMap .

This method allows you to open the named configuration file, rather than the default myapp.exe.config file.

 ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); configFileMap.ExeConfigFilename = "custom.config"; // Get the mapped configuration file config = ConfigurationManager.OpenMappedExeConfiguration( configFileMap, ConfigurationUserLevel.None); 

This will load any old configuration file. But looking at your question, I think your real problem is likely to be one of the links. Where is Configuration.RenderingSection defined? Is it included / indicated in your project? It should be.

If you are happy that Configuration.RenderingSection is defined, you may need to start looking for Assembly Resolvers .

+6
source

Use app.config instead.

Exactly the same syntax, parameters, etc. like web.config, but for console and WinForms applications.

Also, look here , I have not tested it, but it seems to work for this guy. But, nevertheless, I would suggest using app.config if your project has nothing to do with the network.

+1
source

You can set APP_CONFIG_FILE AppDomain.CurrentDomain.SetData (row name, Object data) .

 string webconfigPath = "whatever......../web.config"); string webconfigDir = Path.GetDirectoryName(configPath); AppDomain.CurrentDomain.SetData("APPBASE", webconfigDir); AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", webconfigPath); 

Itโ€™s easy and youโ€™ll get the same settings for the console application (cron) and the web application.

+1
source

Source: https://habr.com/ru/post/1445571/


All Articles