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 もこのセクションにアクセスできません。

4

3 に答える 3

10

必要なものが見つかりました。assemblyBinding ノードには xmlns 属性が含まれているため、XmlNamespaceManager が必要です。これを使用するようにコードを変更しましたが、動作します:

    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"));
    }
于 2009-05-01T00:04:56.480 に答える
8

構成ファイルの微調整が機能しているように聞こえますが、実行時にバインディング リダイレクトを調整する方法にまだ関心があるのではないかと思いました。重要なのはAppDomain.AssemblyResolveイベントを使用することです。詳細はこの回答にあります。バージョン番号の比較はもう少し洗練されている可能性があり、ビルドのたびに構成ファイルを微調整する必要がないため、構成ファイルを使用するよりもそれを好みます。

于 2010-02-26T20:40:53.080 に答える
-1

正しいXpath構文は次のとおりです。

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

(スラッシュが多すぎます)。

または、これが機能しない場合は、bindingRedirect要素を選択できます(SelectSingleNodeを使用)。

/ configuration / runtime / assemblyBinding /dependentAssembly /bindingRedirect

次に、この要素の属性newVersionを変更します。

于 2009-04-30T22:30:08.470 に答える