1

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?

one noa
  • 345
  • 1
  • 3
  • 10
Dave
  • 1,912
  • 4
  • 16
  • 34
  • 1
    You can have the AppSettings in a own file and link the same file info multiple config-files. as in this question: https://stackoverflow.com/questions/17437340/appsettings-in-app-or-web-config-using-a-linked-file – Daniel Stackenland Apr 16 '19 at 13:27
  • @DanielStackenland thank you, this resource is very useful! – Dave Apr 16 '19 at 13:28

3 Answers3

1

Simply read the file and parse it as an XML. Then retrieve your value reading the interested key.

manurob91
  • 11
  • 2
  • I'm trying to use the `System.Configuration` assembly. The problem is that I can't figure out how to get that property from the other project `app.config`. – Dave Apr 16 '19 at 13:12
0

I found this link very useful:

AppSettings in App or Web Config Using a Linked File

Thanks to @Daniel Stackendland

Dave
  • 1,912
  • 4
  • 16
  • 34
-1

You can put your application settings from your webservice project into the app.config of your UI-project.

DGi
  • 79
  • 1
  • 7
  • In production I'll run both projects. So, I would like to have a single `app.config` file where configuration settings are defined. – Dave Apr 16 '19 at 09:32
  • Yes, thats exactly what you do. Yout put the lines with the values from the webservice-project into yout config of your UI-project – DGi Apr 16 '19 at 13:03
  • But I don't want to duplicate the configuration. I would like to have a single file where `config` is available. And this file should be placed in the `WebService` project. – Dave Apr 16 '19 at 13:15