Reading web.config using WebConfigurationManager, which contains custom sections, cannot load

I am trying to put together a small helper for encrypting web.config files.

Linqpad example:

var path = @"F:\project\"; var wcfm = new WebConfigurationFileMap(); wcfm.VirtualDirectories.Add("/",new VirtualDirectoryMapping(path,true)); var config = Configuration.WebConfigurationManager.OpenMappedWebConfiguration(wcfm,"/"); var c = config.Sections["customGroup"]; c.SectionInformation.ProtectSection(null); config.Save(); 

This throws an exception:

 Could not load file or assembly 'customGroupAssembly' or one of its dependencies. The system cannot find the file specified 

Adding an assembly to LinqPad fixes errpr. I would suggest that this is because now it is a compile time reference.

Moving code into a winforms application re-introduces the problem.

I am trying to load the required assembly at runtime:

 if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { byte[] assemblyBuffer = File.ReadAllBytes(this.openFileDialog1.FileName); AppDomain.CurrentDomain.Load(assemblyBuffer); MessageBox.Show("Assembly loaded!"); } 

However, it still cannot find the file.

Is there a way to load a custom assembly into the runtime application domain so that the web configuration can be loaded correctly?

+4
source share
1 answer

Try connecting the assembly solution listener, I had such a problem when the assembly loaded at runtime was not resolved correctly and was manually resolved:

AppDomain.CurrentDomain.AssemblyResolve += (s,arg)=>{ ... }

with your code in (...) returning the assembly you want to resolve based on the name you got in arg

+1
source

All Articles