Quick version of the question:
How do I configure a .Net Core windows service to target the correct database in a multi-tenant environment where each tenant has their own database and they all run on the same self-hosted server?
Some background info:
I am working on a new windows service and, since it is completely new, we are going to use .Net Core. I have read this page and it does talk about how to set environment variable in an IIS app, azure, globally, per command window but it does not really mention a windows service, azure devops or how to handle a multi-tenant environment.
The best I can figure is that you are supposed to set the environment variable in the start parameters for the windows service once it is created but that seems very fragile. This becomes more of a problem when you are looking at 25 with a potential of 100 or more (we are a small growing company). The thought of having to go back and set all of these variables manually if we decide to migrate services to another server is not pleasant.
So the longer version of the question is: Am I on the correct track or is there some better way to set this up so that this does not become such a manual process? Perhaps setting a variable when the service is deployed to the server would do the trick? How would I do that with azure devops though?
Edit 1
Here is a representation of the environment we are running these services in.
Databases (separate machine):
Shared1
Db1
Db2
Db3
Machines:
Server1
Windows Services:
Service1
Service2
Service3
The databases are running on the db server. There is one database per service and there is a shared database which stores some common information (don't think this is relevant for the question). There is one server. On that one server there are copies of the code that needs to run as a windows service. The number of copies of the service corresponds to the number of databases. So for the above scenario: Serivce1
connects to Db1
, Service2
connects to Db2
, Service3
connects to Db3
, etc...
Since I only have one machine, if I set an environment variable to something like ASPNETCORE_DB1
then all three services will read that variable and connect to Db1
which is not what needs to happen.
If I set multiple environment variables: ASPNETCORE_Db1
, ASPNETCORE_Db2
, ASPNETCORE_Db2
, how do each of the services know which environment variable to read?