Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change ConnectionStrings at Runtime for a Web API

I hope this is a simple question:

How can you change 2 connection strings at runtime in the Global.asax under Application_Start()

Web.config

<connectionStrings>
  <add connectionString="DB1" value=""/>
  <add connectionString="DB2" value=""/>
</connectionStrings>

Global.asax

protected void Application_Start() {
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

Details

Before I start getting questions as to why I'm doing this or reasons I shouldn't, please refer to the following post Azure Key Vault Connection Strings and N-Layered Design.

Essentially, I'm trying to use Key Vault with an N-Layered application. The WebAPI defines the connection string via the Web.config. In order to avoid hard-coding connection strings, they will be stored in Key Vault. However, due to the Unit Of Work pattern used, I'm not sure the best route and I'm currently trying to figure out the potential solution of injecting or changing the connection string at runtime for the Web API project only.

like image 783
RoLYroLLs Avatar asked Feb 22 '19 21:02

RoLYroLLs


People also ask

How do I change my connection string?

Select the TableAdapter or query that has the connection you want to edit. In the Properties window, expand the Connection node. To quickly modify the connection string, edit the ConnectionString property, or click the down arrow on the Connection property and choose New Connection.

How do I change the connection string after deployment?

If you save your connection string in the udl file, the user can change the connection via an interface by simply double clicking that file. You can set your connection string in the app to point to the udl file. You can also launch the udl interface programmatically if you want.

Where are connectionStrings in web config?

Connection strings can be added any where in configuration in such a way it should be a child of configuration. Its recommended that it should be placed after all tags so it remains visible if you need to change it in future.


1 Answers

I feel maybe I have not understood the question (as I don't know anything about Azure Key Vault), but you are not really getting your connection string in Application_Start...

Looking at this answer, I think you can implement a function which would return the desired connection string based on a variable:

string GetConnectionString()
{
    if (/* some dynamic variable is set */) {
        return  "DB1";
    }
    else {
        return "DB2";
    }
}

Now, assuming you have the above function, you can use it to initialize your DbContext:

MyDbContext myDbContext = new MyDbContext(GetConnectionString());

Or if you are injecting your DbContext, you can use it in your DI code (ninject example):

kernel.Bind<IMyDbContext>()
    .ToConstructor(ctorArg => new MyDbContext(GetConnectionString()))
    .InRequestScope();
like image 70
Hooman Bahreini Avatar answered Nov 14 '22 23:11

Hooman Bahreini