4

我正在尝试组合一个小型辅助工具来加密 web.config 文件。

Linqpad 示例:

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();

这会引发异常:

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

将程序集添加到 LinqPad 可修复 errpr。我认为这是因为它现在是编译时参考。

将代码移动到 winforms 应用程序,重新引入了问题。

我正在尝试使用以下方法在运行时加载所需的程序集:

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

但是它似乎仍然没有找到该文件。

有没有办法将自定义程序集加载到运行时应用程序域中,以便可以正确加载 Web 配置?

4

1 回答 1

1

尝试附加程序集解析侦听器,我遇到了这样一个问题,即运行时加载的程序集没有正确解析,需要手动解决:

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

在 (...) 处的代码根据您输入的名称返回您想要解析的程序集arg

于 2013-02-15T15:19:26.917 回答