简体   繁体   中英

Connection string has been loaded from Web.Config instead of App.Config

I have console application in cron.exe which reference the web application. Cron project has its own App.Config with the connection string. The Cron Project reference web application which has web.config with different connection strings.

When I run the cron.exe, it loads the connection strings from web application's web.config instead from it's own app.config?

How do I force the console application to use the app.config which is included within the project?

    string key = "SqlServices";
    var connStr = ConfigurationManager.ConnectionStrings[key];

    if (connStr == null)
    {
        List<string> conns = new List<string>();
        for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
        {
            var connI = ConfigurationManager.ConnectionStrings[i];

            conns.Add(connI.Name + ": " + connI.ConnectionString);
        }

        throw new ConfigurationErrorsException(string.Format("Missing connection string {0}. Founded these connection strings: {1} ", key, string.Join(";", conns.ToArray())));
                }

                return connStr.ConnectionString;
            }
     }

SOLUTION :

There were few lines in the project merging configuration lines:

AppDomain.CurrentDomain.SetData("APPBASE", configDir);
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath);

It is possible to use web.config in console application and vice versa:

AppDomain.CurrentDomain.SetData("APPBASE", configDir); AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configPath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM