Search code examples
c#asp.net.netvisual-studioclass-library

How to customize class library config settings across projects & clients


Apologies if this has been asked before; after two days of searching I can only find partial answers that don't fully relate to my situation, and are difficult to follow with my lack of experience.

I have a solution that contains four projects:

  1. Class library (containing database connection strings, email server settings, plus lots of other settings)
  2. Web application (web forms)
  3. Web application (MVC)
  4. Web API

Projects 2,3 & 4 all reference the class library, and use the database connection strings, etc, to function. These projects also contain their own additional settings in web.config, bespoke to that project.

Everything works great so far... However, I now need to publish client-specific versions of my solution, e.g. the solution for ClientABC requires different settings for each project than for ClientXYZ. All other aspects remain the same, it is simply the config settings across the four projects that need to change.

From my research, I hit upon something called SlowCheetah which transforms the config files based on the publish profile. That sounded promising, but then I get this problem, where the class library settings aren't pushed into the other projects. I can see bits of useful info in this question, but don't have the experience to apply it to my problem. I'd rather not duplicate the settings into respective project's config file if possible, as that feels messy.

Can anyone please offer me some help as to what's best here? I don't even know if I'm taking the right approach, but am pretty sure I can't be the first ask this?


Solution

  • but then I get this problem, where the class library settings aren't pushed into the other projects

    you have to keep in mind that the configuration file is readed by the SturtUp application, your client. Class Library can't run directly, but inside a WebApp or WinApp or ConsoleApp

    So, any settings that you put in your ClassLibrary configuration file must be copied in the configuration file of your WebApp.

    Generally, I copy some settings from app.config to web.config but, if you search on internet, you can find a method to automate this operation.

    I now need to publish client-specific versions of my solution

    You can create many configuration profile and use a web.config transformation:

    1. From ToolBar or Build Menu, select Configurazion Manager...

    Configurazion Manager...

    1. Create all configuration you need for clients ClientABC

    2. Now you can see different web.configuration files

    enter image description here

    1. Now you can specify different configurazion transformation for your ClientABC, ClientXYZ and publish them with specific configuration

    EDIT: So, you can adopt this solution for your Class Library too, or external config file, and include external file in your web.config: External Config