如何以编程方式修改 app.config 中的 assemblyBinding?
我试图通过使用 XmlDocument 类并直接修改值来在安装时更改 bindingRedirect 元素。 这是我的 app.config 的样子:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
...
</sectionGroup>
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MyDll" publicKeyToken="31bfe856bd364e35"/>
<bindingRedirect oldVersion="0.7" newVersion="1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
...
</configuration>
然后我尝试使用以下代码将 1.0 更改为 2.0
private void SetRuntimeBinding(string path, string value)
{
XmlDocument xml = new XmlDocument();
xml.Load(Path.Combine(path, "MyApp.exe.config"));
XmlNode root = xml.DocumentElement;
if (root == null)
{
return;
}
XmlNode node = root.SelectSingleNode("/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect/@newVersion");
if (node == null)
{
throw (new Exception("not found"));
}
node.Value = value;
xml.Save(Path.Combine(path, "MyApp.exe.config"));
}
但是,它会抛出“未找到”异常。 如果我将路径备份到 /configuration/runtime ,它就可以工作。 但是,一旦我添加 assemblyBinding,它就找不到该节点。 这可能与 xmlns 有关吗? 知道我该如何修改这个吗? ConfigurationManager 也无权访问此部分。
I am trying to change the bindingRedirect element at install time by using the XmlDocument class and modifying the value directly. Here is what my app.config looks like:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
...
</sectionGroup>
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MyDll" publicKeyToken="31bfe856bd364e35"/>
<bindingRedirect oldVersion="0.7" newVersion="1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
...
</configuration>
I then try to use the following code to change 1.0 to 2.0
private void SetRuntimeBinding(string path, string value)
{
XmlDocument xml = new XmlDocument();
xml.Load(Path.Combine(path, "MyApp.exe.config"));
XmlNode root = xml.DocumentElement;
if (root == null)
{
return;
}
XmlNode node = root.SelectSingleNode("/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect/@newVersion");
if (node == null)
{
throw (new Exception("not found"));
}
node.Value = value;
xml.Save(Path.Combine(path, "MyApp.exe.config"));
}
However, it throws the 'not found' exception. If I back the path up to /configuration/runtime it works. However once I add assemblyBinding, it does not find the node. Possibly this has something to do with the xmlns? Any idea how I can modify this? ConfigurationManager also does not have access to this section.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了我需要的东西。 由于 assemblyBinding 节点包含 xmlns 属性,因此需要 XmlNamespaceManager。 我修改了代码以使用它并且它有效:
I found what I needed. The XmlNamespaceManager is required as the assemblyBinding node contains the xmlns attribute. I modified the code to use this and it works:
听起来您现在已经可以调整配置文件了,但我认为您可能仍然对如何在运行时调整绑定重定向感兴趣。 关键是使用 AppDomain.AssemblyResolve 事件,详细信息位于
Sounds like you've got your configuration file tweak working now, but I thought you might still be interested in how to adjust binding redirects at run time. The key is to use the AppDomain.AssemblyResolve event, and the details are in this answer. I prefer it over using the configuration file, because my version number comparison can be a bit more sophisticated and I don't have to tweak the configuration file during every build.
我认为正确的 Xpath 语法是:
/configuration/runtime/ assemblyBinding/dependentAssembly/bindingRedirect@newVersion
(斜杠太多)。
或者,如果这不起作用,您可以选择 bindingRedirect 元素(使用 SelectSingleNode):
/configuration/runtime/ assemblyBinding/dependentAssembly/bindingRedirect
然后修改该元素的属性 newVersion 。
I think the right Xpath syntax is:
/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect@newVersion
(you have a slash too many).
Or if this doesn't work you could select the bindingRedirect element (using SelectSingleNode):
/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect
Then modify the attribute newVersion of this element.