Pergunta

I have a question regarding AppSettings in C#. First I´ll describe my situation.

My solution consists of an executable program program.exe and an assembly.dll.

The program references the assembly and works with it. The assembly-project has application settings set up with the Visual Studio project settings manager. Now when I compile my solution in my assembly\bin\release folder there is an assembly.dll.config file which contains the settings I have set up earlier.

Now the thing I don´t understand : in my program-project where I reference the assembly.dll I have checked CopyLocal=True, but in my program\bin\release folder there is only the assembly.dll but not the assembly.dll.config file BUT STILL the assembly.dll knows the settings I have set up in the assembly-project application settings.

Now I have read several times that assemblies always access the settings of the executable program but the program has no corresponding settings, so why does the assembly know the correct settings when there is no assembly.dll.config file present?

I assume the settings are compiled into the assembly at compiletime (of course), but then it makes no sense that in my assembly\bin\release folder there actually IS an assembly.dll.config file.

I tried copying this file into my program\bin\release folder where the assembly.dll is copied to on build action, but the assembly.dll just ignores if there is an assembly.dll.config file present in the same folder. It always uses the settings from compiletime. I just don´t understand the use of the assembly.dll.config file. Why is it created when it never has impact on the assembly.dll´s behaviour ?

Foi útil?

Solução

The default values are built into the .dll file. You can of course still change those settings, but you do that in the program.exe config instead by referring to the assembly settings with a section in configSections/sectionGroup. The assembly settings can then be changed in the applicationSettings by creating a XML block with the same name as the section.

The section tag in the section group can simply be copied from the app.config file of your assembly project. That way the token, name, etc. will be correct. The same goes for the applicationSettings part. Just copy it from the app.config in the assembly project and into the app.config file of the program.exe project.

example program.exe.config:

<configuration>
  <configSections>
    ... references to all dll settings ...
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="MyAssemblyNamespace.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  ... more config stuff...
  <applicationSettings>
    ... override your dll settings ...
    <MyAssemblyNamespace.Properties.Settings>
       <setting name="MaxUserNameLength" serializeAs="String">
          <value>100</value>
       </setting>
    </MyAssemblyNamespace.Properties.Settings>
  </applicationSettings>

Outras dicas

It's valid for an assembly to open any config file it likes using the OpenExeConfiguration method. This way that can access their own config rather than the one implicitly available from the executing assembly. This isn't commonly done, but there's nothing to stop you doing it.

If your assembly is functioning correctly without the config file then it sounds like it has a sensible set of default value that it uses when it cant find the appropriate configuration.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top