سؤال

واني اسعى الى تغيير العنصر bindingRedirect في تثبيت وقت باستخدام فئة XmlDocument وتعديل قيمة مباشرة. هنا هو ما يبدو لي 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>

وI ثم محاولة استخدام التعليمات البرمجية التالية لتغيير 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"));
}

ومع ذلك، فإنه يطرح الاستثناء 'غير موجود ". إذا كنت دعم مسار يصل إلى / التكوين / وقت التشغيل يعمل. ولكن بمجرد أن أضيف assemblyBinding، فإنه لا يجد العقدة. ربما هذه لها علاقة مع XMLNS؟ أي فكرة كيف يمكن تعديل هذا؟ ConfigurationManager أيضا لم يكن لديك الوصول إلى هذا القسم.

هل كانت مفيدة؟

المحلول

ولقد وجدت ما احتاجه. مطلوب XmlNamespaceManager بدور عقدة assemblyBinding تحتوي السمة XMLNS. I تعديل التعليمات البرمجية لاستخدام هذا ويعمل:

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

نصائح أخرى

ويبدو وكأنه كنت قد حصلت على ملف التكوين الخاص بك قرص العمل الآن، لكني أعتقد أنك لا تزال مهتمة في كيفية ضبط الموجهات ملزمة في وقت التشغيل. والمفتاح هو لاستخدام AppDomain.AssemblyResolve الحدث والتفاصيل في <لأ href = "https://stackoverflow.com/questions/1460271/how-to-use-assembly-binding-redirection-to-ignore-revision-and-build-numbers/2344624#2344624 "> هذه الإجابة . I تفضل ذلك على استخدام ملف التكوين، لأن بلدي نسخة مقارنة عدد يمكن أن يكون قليلا أكثر تطورا وأنا لم يكن لديك قرص ملف التكوين أثناء كل بناء.

وأعتقد بناء الجملة كسباث الصحيح هو:

و/ التكوين / وقت التشغيل / assemblyBinding / dependentAssembly / bindingRedirect @ newVersion

و(لديك خط مائل الكثير).

وأو إذا لم تنجح هذه الطريقة يمكن تحديد (SelectSingleNode استخدام) عنصر bindingRedirect:

و/ التكوين / وقت التشغيل / assemblyBinding / dependentAssembly / bindingRedirect

وبعد ذلك تعديل newVersion سمة لهذا العنصر.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top