1

I am converting a .NET 4 console app to a DLL so I can then consume it from multiple consumers, services, console apps and so on.

However I ran into the problem that after I converted it to to a DLL the consumer app ran into bugs whenever it tried to access a value stored in the app.config file attached to the DLL.

Converting a C# Console App to a DLL

These are things like connection strings, paths to folders to store data and other important information.

I was told I could either -Put the config into the consumer app and pass all these options into the DLL as parameters to the methods (a lot of work to rebuild the DLL) -Use some form of XML file to store the values and then use a special method to get them out. Examples would be great! -Use Settings Files -Use some form of function (which I don't understand-not too hot on .NET yet) -Hardcode the values as strings, ints, nools etc in my DLL

The values in this config file won't change as they are related to the content of the DLL not the consumer app. So passing them in seems a lot of work when the DLL could just have them hardcoded in the first place.

Any tips AND examples would be greatly appreciated - I have read and searched for answers for this on this forum by the way before someone says "this has been answered many times" maybe just not in a way some new to DLLS and Consumer console apps would understand?

Thanks for any help!

Community
  • 1
  • 1
MonkeyMagix
  • 677
  • 2
  • 10
  • 30

2 Answers2

1

move the app.config values to the consumer apps app.config. You do not need to pass the config to the dll, it can still reference the config using the same names

ajg
  • 1,743
  • 12
  • 14
0

When I run into this situation, I like to create a settings file in the class library. This will automatically add an app.config to the class library with the necessary config xml. This approach has two advantages: you can access all of the settings in the class library (via Properties.Settings.Default.xxx) and the consumers can also change the values via their own config file.

To create the settings file, right click on the class library project in Solution Explorer -> Properties. Click the Settings tab and then click the Create hyperlink. Make sure to set any Settings you create to Applciation Scope.

You'll then get something like this in the app.config for your class library:

<?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=b77a5c561934e089" >
        <section name="Example.ClassLibrary.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<applicationSettings>
    <Example.ClassLibrary.Properties.Settings>
        <setting name="ExampleConnectionString" serializeAs="String">
            <value>DataSource=xxxx</value>
        </setting>
    </Example.ClassLibrary.Properties.Settings>
 </applicationSettings>
</configuration>

You can then reference the value in your class library

Properties.Settings.Default.ExampleConnectionString.

And you can override in your consumer (ie Console Application) by copying the xml into the app.config for your consumer.

<?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=b77a5c561934e089" >
        <section name="Example.ClassLibrary.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<applicationSettings>
    <Example.ClassLibrary.Properties.Settings>
        <setting name="ExampleConnectionString" serializeAs="String">
            <value>OVERRIDE IN CONSUMER</value>
        </setting>
    </Example.ClassLibrary.Properties.Settings>
 </applicationSettings>
</configuration>
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123