Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App.config for dll

Tags:

c#

dll

app-config

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?

like image 639
daharon Avatar asked Sep 30 '08 20:09

daharon


People also ask

Does app config get compiled into DLL?

If you don't want to expose values, make sure you have an app. config with deployment values (empty, 0, or something). The values WILL be compiled into the DLL as default values.

Can DLL have config file?

Using configuration files in DLL is not trivial, but is very simple. Using configuration files in DLL is not trivial.

What is app config file?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 Answers

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.

like image 61
MusiGenesis Avatar answered Nov 03 '22 18:11

MusiGenesis


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>
like image 31
Joel Coehoorn Avatar answered Nov 03 '22 19:11

Joel Coehoorn