0

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);
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
  • 1
    can you show us how you read the connection string? – Sefa Dec 27 '14 at 17:16
  • ConfigurationManager.AppSettings["NodeNameInAppConfig"] check this – mybirthname Dec 27 '14 at 17:33
  • That sounds it is something connected with. I have only one share code which I share between console application and web application as well, I cannot/don't want to write separate code for the web and for console. It may be something within setting the application's default configuration you have mentioned. Furthermore ConfigurationManager.AppSettings is not intended for saving connection strings. – Tomas Kubes Dec 27 '14 at 17:41
  • Sorry guys, you couldn't know that there is something special in the project. It was also surprise for me. – Tomas Kubes Dec 27 '14 at 17:52

1 Answers1

0

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);

Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148