sm_
sm_

Reputation: 2602

WCF configuration file

I have created a WCF service, basically it does some interaction with my database. The service is in a project, and it calls functions from a library that i created in another project. In my library, which i will call WCFSerivceLibrary i also have an app.config which i want to pull out some stored values in the AppSettings section.

The issue is that when i call my WCF service from a certain client, and a function is executing in the WCFServiceLibrary, whenever i call AppSettings it checks the configuration file for the calling client!

Further Explanation : lets say we have a windows forms application which calls my WCF service this way :

MyWCFService.DoWork();

in the function DoWork in my WCF service i have the following code :

Type DoWork ()
{
  //MyWCFServiceLibrary is a library in the same solution of the WCF Service.
  MyWCFServiceLibrary.DoWorkOne(); 
  MyWCFServiceLibrary.DoWorkTwo();
}

In the functions DoWorkOne or DoWorkTwo... I'm calling on AppSettings to get some values stored in the app.config of MyWCFServiceLibrary project, but instead, on execution the AppSettings are loaded from the app.cofing of my windows forms client calling the WCF service.

  1. How to avoid the mentioned issue above?
  2. Can I have a single configuration file for my WCF Service and the service library?
  3. How to share it between both?

Upvotes: 2

Views: 3324

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

I'm writing below what I meant about copying the configuration. But I don't think that this is the problem. The problem is probably that you're not even doing WCF communication. I suspect that you included the DLL both in the service project and the client project and you're simple calling the methods on class from the client.

Do do WCF communication you need to have the WCF service running (for example an EXE that creates a ServiceHost with an endpoint). Then in the client, you add a service reference by using Visual Studio's "Add service reference" menu item.

There's no need to include the DLL in the client, as classes will be generated automatically to access the service via WCF.

Now for using application settings properly:

Copy the application settings of your DLL's app.config file to the app.config file of the executable project that's using the DLL. For example, this could then look like this:

<?xml version="1.0"?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Executable.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <section name="DLL.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />        
        </sectionGroup>
    </configSections>

  <applicationSettings>
      <Executable.Properties.Settings>
          <setting name="Test" serializeAs="String">
              <value>Testvalue EXE</value>
          </setting>
      </Executable.Properties.Settings>
      <DLL.Properties.Settings>
          <setting name="Test" serializeAs="String">
              <value>Testvalue DLL</value>
          </setting>
      </DLL.Properties.Settings>
    </applicationSettings>
</configuration>

After doing that, the application can access its settings through Properties.Settings.Default.Test (which returns Testvalue EXE) and the DLL can access its settings through Properties.Settings.Default.Test (which returns Testvalue DLL).

I don't understand why people need to use things like ConfigurationManager when it is actually that simple...

Upvotes: 1

Related Questions