4

I'm trying to put together a small helper tool to encrypt 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 the exception:

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

Adding the assembly to LinqPad fixes the errpr. I would assume this is because it's now a compile time reference.

Moving the code to a winforms application, reintroduces the issue.

I'm trying to load the needed assembly at runtime with this:

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

However it still doesn't seem to find the file.

Is there a way to load the custom assembly into the runtime app domain so that the web config can be loaded properly?

4
  • try attaching assembly resolve listener, I've had such a problem where runtime-loaded assembly was not resolved properly and needed to be manually resolved Feb 14, 2013 at 21:20
  • @StenPetrov How does one go about trying that?
    – asawyer
    Feb 14, 2013 at 21:22
  • AppDomain.CurrentDomain.AssemblyResolve += (s,arg)=>{ ... } with your code at the (...) Feb 14, 2013 at 21:29
  • @StenPetrov I won't pretend I understand why, but if I push the loaded assemblies into a field, then assembly.GetName().Name == args.Name to find the right one and return it from the AssemblyResolve it loads perfectly. Can you move your comment to an answer so I can give you the bounty points?
    – asawyer
    Feb 15, 2013 at 13:16

1 Answer 1

1
+50

Try attaching assembly resolve listener, I've had such a problem where runtime-loaded assembly was not resolved properly and needed to be manually resolved:

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

with your code at the (...) returning the assembly you want resolved based the name you got in arg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.