gpt4 book ai didi

c# - 使用包含自定义部分的 WebConfigurationManager 读取 web.config,无法加载

转载 作者:行者123 更新时间:2023-11-30 12:48:11 29 4
gpt4 key购买 nike

我正在尝试组合一个小的辅助工具来加密 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 可修复错误。我认为这是因为它现在是编译时引用。

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

我试图在运行时加载所需的程序集:

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

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

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

最佳答案

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

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

你的代码在 (...) 返回你想要解析的程序集基于你在 arg

关于c# - 使用包含自定义部分的 WebConfigurationManager 读取 web.config,无法加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841894/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com