0

I have a Windows Service with an key/value in the appSettings area of App.Config that defines the DB's connection string. I don't want to change it every time I push it to production (ie. staging.mydb.com to production.mydb.com). What's the typical implementation of this problem? I considered putting in two keys/values, one for each DB connection string, then doing some kind of test in the code to see which environment it's running in.

Jinxology
  • 1
  • 1

1 Answers1

0

Not quite sure what environment you refer to but you could look for a part of the executable path and decide that way.

if (System.Reflection.Assembly.GetEntryAssembly().Location.Contains("production-path"))
{
    // production connection string
}
else
{
    // staging connection string
}
DerSchnitz
  • 457
  • 3
  • 8
  • Thanks, that could actually be the best solution. I can't decide if I should check machine name or path. Both could theoretically change, but path is probably least likely to change. – Jinxology Nov 10 '20 at 19:11
  • An even more interesting option ist using parameters as mentioned [here](https://stackoverflow.com/questions/6490979). – DerSchnitz Nov 11 '20 at 20:41
  • Thanks, that ended up being the solution. I edited ImagePath for the service in the registry in production and passed in "-prod" as an argument for the service. In the Program() code, I test for that argument and load the appropriate app settings from app.config.Thanks for the help! – Jinxology Nov 13 '20 at 21:33