Best way to store Multiple ConnectionStrings in ASP.NET Api RRS feed

  • Question

  • User715841376 posted

    I have many databases (Over 50) that has same schema. Each one stores data of a different client. I am exposing CRUD operations through Webapi. Connection string is being changed at run time. Client passes database alias that correspond to a connection string in my web.config file

    <add name="DatabaseNameAlias" connectionString="Data Source=hhhhhxxxu.database.windows.net;Initial Catalog=MyDB;User ID=abc;password=xyz;Trusted_Connection=false;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

    that works well but the issue is we have to type in 50 connection strings in my web.Config file. It's not very manageable approach. Although connection strings are stored in one common database as well but if i try to fetch them from database for each web api call that looks bad as well. In that case i have to hit database for each web api request.

    Can anyone help me with their experience. Any recommendation would be highly appreciated.

    Thursday, July 2, 2015 4:40 PM

All replies

  • User-821857111 posted

    You can store them in the database and then in the Application_Start event, read the values from the database into a dictionary and save that as an application level variable. Whenever you make a change to the data in the database, kill the application variable. Whenever you need a connection string, try to read it from the variable. Check first to make sure the application variable is not null. If it is, repopulate it from the database. It's a simple form of caching, really. You only read form the database when you need to, and you don't have to worry about app restarts whenever you mess about changing the web.config.

    Thursday, July 9, 2015 3:17 AM