Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to load web-config from console application?

My question is quite simple. I have a third party library which assumes that it will is used in a web site (needs special configuration sections in web.config). I would like to use this library in a C# console application. It is possible?

I have tried WebConfigurationManager.OpenWebConfiguration(); as well as just copy everything into app.config but it does not work. The error I receive is a ConfigurationErrorsException stating 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'.

like image 562
Erik Pettersson Avatar asked Nov 03 '22 11:11

Erik Pettersson


1 Answers

You can set APP_CONFIG_FILE by AppDomain.CurrentDomain.SetData(string 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 is easy and you will get same setting for console application (cron) and web application.

like image 63
Tomas Kubes Avatar answered Nov 15 '22 01:11

Tomas Kubes