none
app.config in class library (or shared location) RRS feed

  • Question

  • Hello, 

    How do I add an app.config to my class library and share it with all projects that are consumers of the class library?  I thought there was a way to add one as a link but I cannot seem to do this in my .net 4.7 project. 

    So my solution consists of a class library and then perhaps up to 10 console applications that will be API consumers.  The app settings and connection strings will all be the same so it makes sense to have just one app.config in the Teamwork project, as below 

    Thanks 

    e.g 

    PS - All Get* projects have a reference to the Teamwork class library. 
    Thursday, March 12, 2020 4:57 PM

Answers

  • All the dlls access at runtime the .config file of the .exe that called the dll. The app.config is only used by the designer at development time, but the executable doesn't use it.

    So if you have any settings that have to be used by several dlls that call each other, then you have to build your Setup program so that it deploys all the necessary settings to the main .exe.config. From there, they will be read by all the libraries that are called directly or indirectly from the executable.

    Thursday, March 12, 2020 6:25 PM
    Moderator

All replies

  • All the dlls access at runtime the .config file of the .exe that called the dll. The app.config is only used by the designer at development time, but the executable doesn't use it.

    So if you have any settings that have to be used by several dlls that call each other, then you have to build your Setup program so that it deploys all the necessary settings to the main .exe.config. From there, they will be read by all the libraries that are called directly or indirectly from the executable.

    Thursday, March 12, 2020 6:25 PM
    Moderator
  • Hello friend @Albert is correct . A gain you may try the bellow suggestion


    Sharing single Configuration in Multiple projects

    https://dailydotnettips.com/sharing-single-configuration-in-multiple-projects/


    or

    Create a shared project and using for all

    https://dev.to/rionmonster/sharing-is-caring-using-shared-projects-in-aspnet-e17


    To add a Shared Project to your existing solution, right-click on the Solution and choose "Add Project",
    and then select "Shared Project" from the Visual C# tab:
    You'll see the Shared Project appear within the Solution Explorer and from this point,
    you can begin adding any types of files that you want to use to it

    Friday, March 13, 2020 7:03 AM
  • Simply put, the Windows form project is the root project, which creates the programname.exe. So all the config information in app.configs for subordinate classlib projects or DLL(s) must be copied to the app.config of the root project. which is the Windows form project.

    Upon the successful build of the Windows form project,  a runtime config file named programname.exe.config is made from the app.config in the project that's created in the same folder as the programname.exe. When the solution is deployed, the runtime config must accompany the programname.exe so that .NET finds the config info at runtime, becuase .NET only looks for config information in the root.config the programname.exe.config.  

    Saturday, March 14, 2020 7:21 AM