Search code examples
c#sqlasp.net-mvcmef

C# How can I embed a connection string in a class?


I've got a C# MVC project I'm working on at the moment with a basic plugin architecture implemented via MEF. Plugins and their corresponding parts get uploaded through a web interface and are placed in a plugin folder they are then discovered at application start.

Some of the plugins will require data access to external databases (not the main database). When the application goes live I won't be able to easily make changes to the main application's web.config file.

So my question is how can I embed a connection string directly into one of my plugin's classes?


Solution

  • A couple of ideas:

    1) Just hard code it: Con: I think you are concerned about changing the connection string on the fly. So this wouldn't work well for what you need.

    2) Store another config file on a shared drive that you CAN access. Pro: Let's you change the connection string on the fly. Con: Not the best option for security.

    3) Use an authenticated web service. On startup, call the web service (and possibly every hour, or on connection failure) to get the connection string. Pro: This allows you to change the connection string on the fly. Pro: This can be implemented with security

    Of the three options, number three seems to best fit your needs. But those are just off the top of my head.