daharon
daharon

Reputation: 2010

App.config for dll

We have an "engine" that loads dlls dynamically (whatever is located in a certain directory) and calls Workflow classes from them by way of reflection.

We now have some new Workflows that require access to a database, so I figured that I would put a config file in the dll directory.

But for some reason my Workflows just don't see the config file.

<configuration>
  <appSettings>
      <add key="ConnectString" value="Data Source=officeserver;Database=mydatabase;User ID=officeuser;Password=officeuser;" />
  </appSettings>
</configuration>

Given the above config file, the following code prints an empty string:

Console.WriteLine(ConfigurationManager.AppSettings["ConnectString"]);

I think what I want is to just specify a config filename, but I'm having problems here. I'm just not getting results. Anyone have any pointers?

Upvotes: 11

Views: 16852

Answers (6)

0xDEAD BEEF
0xDEAD BEEF

Reputation: 2104

Here is one way - AppDomain.CurrentDomain.SetData ("APP_CONFIG_FILE", "path to config file");

Call in constructor.

Upvotes: 1

Mike Dimmick
Mike Dimmick

Reputation: 9802

I wrote this for a similar system. My recollection is that I used Assembly.GetExecutingAssembly to get the file path to the DLL, appended .config to that name, loaded it as an XmlDocument, navigated to the <appSettings> node and passed that to a NameValueSectionHandler's Create method.

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

Funny, where I'm at we're doing something very similar and the config file loads just fine. In our case I think each new config file's name matches that of it's associated assembly. So MyLibrary.dll would have a file named MyLibrary.dll.config with information for that file assembly. Also, the example I have handy is using VB.Net rather than C# (we have some of each) and all the settings in there are for the VB-specific My.Settings namespace, so we don't use the ConfigurationManager class directly to read them.

The settings themselves look like this:

<applicationSettings>
    <MyLibrary.My.MySettings>
        <setting name="SomeSetting" serializeAs="String">
            <value>12345</value>
        </setting>
    </MyLibrary.My.MySettings>
</applicationSettings>

Upvotes: 4

Juan Zamudio
Juan Zamudio

Reputation: 479

I'm not totally sure but I think that class only works with the path of the entry method of the AppDomain (the path of the exe most of the time) by default. You need to call OpenExeConfiguration(string exePath) (Framework 2.0 and later) first to point to a different config file.

Upvotes: 0

MusiGenesis
MusiGenesis

Reputation: 75296

If your code sample for reading the AppSettings is in your DLL, then it will attempt to read the config file for the application and not the config file for the DLL. This is because you're using Reflection to execute the code.

Upvotes: 7

Dana
Dana

Reputation: 32957

If I recall correctly, the app.config will be loaded from your application directory, so if you are loading dlls from some other directory, you'll want the keys they need in your application's config file.

Upvotes: 0

Related Questions