Search code examples
c#.netwcfapp-config

Read config properties from another project in the same solution C#


I have a solution named WebServiceProject.

Inside this solution I have three projects:

  • Common (Class Library)
  • UserInterface (WinForms Project)
  • WebService (WCF Service Project)

In the WebService project I have an app.config file with this content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b72a5b561321d079">
      <section name="WebService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b72a5b561321d079" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
  <appSettings>
    <add key="ConnectionString" value="mydatabase@localhost"/>
  </appSettings>
  <system.serviceModel>
    <bindings/>
    <client/>
  </system.serviceModel>
</configuration>

In the WebService project I execute some scheduled routine calling the Common project classes reading the ConnectionString from app.config file:

if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["ConnectionString"]))
{
    // I do something
}

If I start the WebService alone everything works fine.

Inside the WinForms project UserInterface, I have a button that start the routine inside the Common project as the WebService does.

But if I "Set as StartUp project" the UserInterface project the previous piece of code ConfigurationManager.AppSettings["ConnectionString"] throw an error, because I didn't specified the ConnectionString in the app.config in UserInterface project.

So, my question is: how can I read the ConnectionString property from the WebService project event if I "Set as StartUp project" the UserInterface project? More in general, how can I read an app.config property from another project different from the executed one?


Solution

  • I found this link very useful:

    AppSettings in App or Web Config Using a Linked File

    Thanks to @Daniel Stackendland