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

10

我正在尝试使用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也无法访问此部分。

3个回答

10

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

    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"));
    }

1
只是一个提醒,我抛出异常是因为这是安装设置项目的一部分,安装程序会通过这种方式被通知任何错误。如果修改已经完成,最好让该方法返回true或false。 - esac

8
听起来你已经成功地调整了配置文件,但我认为你可能仍然对如何在运行时调整绑定重定向感兴趣。关键是使用AppDomain.AssemblyResolve事件,详细信息请参见此答案。我更喜欢使用它而不是使用配置文件,因为我的版本号比较可以更加复杂,并且我不必在每次构建时调整配置文件。

3
如果程序集最初加载失败,这个方法可以奏效。但是如果你的应用程序成功加载了一个错误的程序集,那么AssemblyResolve就不会触发。因此,在这种情况下,唯一的选择就是修改app.config文件。 - Phil
希望能够撤回我的投票。自 .NET 4.6.1 以来,如果绑定已设置,则此方法实际上无法正常工作。仍然会出现程序集绑定错误。 - marknuzz

-1

我认为正确的Xpath语法是:

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

(你多了一个斜杠)。

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

/configuration/runtime/assemblyBinding/dependentAssembly/bindingRedirect

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


已经走过这条路了,它抱怨绑定重定向@newVersion的无效令牌。第二种情况,它抱怨找不到指定的路径。 - esac

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接