Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use has the default (assembly).dll.config file for .NET-Assemblies?

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 ?

like image 760
thoros1179 Avatar asked Feb 07 '14 12:02

thoros1179


People also ask

What is an assembly in ASP NET?

An assembly in ASP.NET is a collection of single-file or multiple files. The assembly that has more than one file contains either a dynamic link library (DLL) or an EXE file.

What is the config file for assembly components?

assemblies don't have their own app.config file. They use the app.config file of the application that is using them. So if your assembly is expecting certain things in the config file, then just make sure your application's config file has those entries in there.

What is assembly manifest in ASP NET?

An assembly in ASP.NET is a collection of single-file or multiple files. The assembly that has more than one file contains either a dynamic link library (DLL) or an EXE file. The assembly also contains metadata that is known as assembly manifest.

Why is my Assembly expecting certain things in the config file?

So if your assembly is expecting certain things in the config file, then just make sure your application's config file has those entries in there. If your assembly is being used by multiple applications then each of those applications will need to have those entries in their app.config file.


2 Answers

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>
like image 152
Holstebroe Avatar answered Sep 17 '22 13:09

Holstebroe


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.

like image 27
Sean Avatar answered Sep 18 '22 13:09

Sean