none
Dynamically loading multiple DLL versions RRS feed

  • General discussion

  • I'm running some code that uses HtmlAgilityPack version 1.5.5.0, for example like the following:

    public class Work
    {
        public void DoSomething()
        {
            Console.WriteLine(typeof(HtmlDocument).AssemblyQualifiedName);
            var doc = new HtmlDocument();
            // ...
        }
    }

    But I also wanted to execute the same code with different version so I renamed an older DLL to "HtmlAgilityPack.1.5.4.dll", added a reference to it, changed its alias from "global" to "HtmlAgilityPack154" and created a new class which uses it:

    extern alias HtmlAgilityPack154;
    using System;
    using HtmlAgilityPack154.HtmlAgilityPack;
    
    namespace ConsoleApplication1
    {
        public class WorkOlder
        {
            public void DoSomething()
            {
                Console.WriteLine(typeof(HtmlDocument).AssemblyQualifiedName);
                var doc = new HtmlDocument();
                // ...
            }
        }
    }
    

    Additionally I had to specify the assembly binding for this older DLL in App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="HtmlAgilityPack" publicKeyToken="bd319b19eaf3b43a" culture="neutral" />
            <codeBase version="1.5.4.0" href="\htmlagilitypacks\HtmlAgilityPack.1.5.4.dll"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>

    Now I'm executing them as following and I'm getting the desired behavior:

    class Program
    {
        static void Main(string[] args)
        {
            new Work().DoSomething();
            new WorkOlder().DoSomething();
    
            Console.Read();
        }
    }
    But is it possible to achieve this dynamically?
    What if I want to execute that DoSomething with version "1.5.3.0", "1.5.2.0", etc. as well?
    • Changed type Pusting Thursday, October 12, 2017 7:22 AM
    Wednesday, October 11, 2017 9:01 AM

All replies

  • Hi,

    Thank you for posting here.

    As far as I know that I cannot find a better solution to achieve the process dynamically.

    I think that you can change the type of your thread as discussion type, the other community will participate it.

    Based on my search, the click once can make assemblies as Optional. I am not sure whether it can help you .

    To mark assemblies as optional in your ClickOnce application by using Manifest Generation and Editing Tool — Graphical Client (MageUI.exe)

    For more information, please see the document.

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Hart Wang Thursday, October 12, 2017 6:29 AM
    Thursday, October 12, 2017 6:24 AM
  • Unfortunately I don't see how ClickOnce would help, essentially what I'm trying to do is execute that same code over all the DLL versions that I have inside my "htmlagilitypacks" folder at some given time.

    One thing that I did try is to execute that Work.DoSomething code in separate AppDomains, in which I would load different versions of HtmlAgilityPack.
    However, for this to work that code had to be re-written so that it uses HtmlAgilityPack through a reflection and thus the members accessing would be ambiguous to an exact version.
    But I would like to avoid this reflection conversion... it will be hard to maintain it.

    Thursday, October 12, 2017 8:33 AM
  • Hi,

    Yes, the click once is not good solution. 

    Based on my research, did you try to use the #define directive to separate all version dll. 

    First you add all version DLL into project. then use #define directive . 

    When you want to use which version DLL, you can open which #define directive. 

    like this.

    #define DEBUG_version_dll  
    //#define DEBUG_version_dll    
    #undef DEBUG_version_dll    
    
    using System;  
    using DEBUG_version_dll;
    
    public class TestDefine  
    {  
        static void Main()  
        {  
    #if (DEBUG_version_dll  )  
            Console.WriteLine("Debugging is enabled.");  
    #endif  
    
    #if (DEBUG_version_dll  )  
         Console.WriteLine("Tracing is enabled.");  
    #endif  
        }  
    }  

    Maybe this is a not good solution. I hope that it would be helpful for you.

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 16, 2017 7:50 AM