首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在app.config中以编程方式修改assemblyBinding?

如何在app.config中以编程方式修改assemblyBinding?
EN

Stack Overflow用户
提问于 2009-04-30 21:45:29
回答 3查看 12.7K关注 0票数 10

我尝试在安装时通过使用XmlDocument类并直接修改值来更改bindingRedirect元素。下面是我的app.config的样子:

代码语言:javascript
复制
<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

代码语言:javascript
复制
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也没有访问此部分的权限。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-05-01 00:04:56

我找到了我需要的东西。XmlNamespaceManager是必需的,因为assemblyBinding节点包含xmlns属性。我修改了代码以使用它,它可以工作:

代码语言:javascript
复制
    private void SetRuntimeBinding(string path, string value)
    {
        XmlDocument doc = new XmlDocument();

        try
        {
            doc.Load(Path.Combine(path, "MyApp.exe.config"));
        }
        catch (FileNotFoundException)
        {
            return;
        }

        XmlNamespaceManager manager = new XmlNamespaceManager(doc.NameTable);
        manager.AddNamespace("bindings", "urn:schemas-microsoft-com:asm.v1");

        XmlNode root = doc.DocumentElement;

        XmlNode node = root.SelectSingleNode("//bindings:bindingRedirect", manager);

        if (node == null)
        {
            throw (new Exception("Invalid Configuration File"));
        }

        node = node.SelectSingleNode("@newVersion");

        if (node == null)
        {
            throw (new Exception("Invalid Configuration File"));
        }

        node.Value = value;

        doc.Save(Path.Combine(path, "MyApp.exe.config"));
    }
票数 10
EN

Stack Overflow用户

发布于 2010-02-27 04:40:53

听起来你现在已经让你的配置文件调整工作了,但是我想你可能仍然对如何在运行时调整绑定重定向感兴趣。关键是使用AppDomain.AssemblyResolve事件,详细信息在this answer中。与使用配置文件相比,我更喜欢使用配置文件,因为我的版本号比较可能会更复杂一些,而且我不必在每次构建时都调整配置文件。

票数 8
EN

Stack Overflow用户

发布于 2009-04-30 22:30:08

我认为正确的Xpath语法是:

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect@newVersion

(斜杠太多了)。

或者,如果这不起作用,您可以选择bindingRedirect元素(使用SelectSingleNode):

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect

然后修改该元素的newVersion属性。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/809262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档