c# – 使用包含自定义部分的WebConfigurationManager读取web.con
发布时间:2020-12-15 21:47:28 所属栏目:百科 来源:网络整理
导读:我正在尝试整理一个小帮手工具来加密web.config文件. Linqpad的例子: var path = @"F:project";var wcfm = new WebConfigurationFileMap();wcfm.VirtualDirectories.Add("/",new VirtualDirectoryMapping(path,true));var config = Configuration.WebConf
我正在尝试整理一个小帮手工具来加密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配置? 解决方法
尝试附加程序集解析侦听器,我遇到了这样一个问题,即运行时加载的程序集未正确解析并需要手动解析:
AppDomain.CurrentDomain.AssemblyResolve =(s,arg)=> {…} 使用代码在(…)根据arg中的名称返回要解析的程序集 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |