【问题标题】:ASP.NET: FileLoadException when trying to use two different versions of the same dllASP.NET:尝试使用同一 dll 的两个不同版本时出现 FileLoadException
【发布时间】:2015-08-07 11:37:48
【问题描述】:

所以我需要在同一个解决方案中使用同一个 dll 的两个不同版本,我设法通过使用 extern aliases 来做到这一点(任何尝试使用同一个 dll 的两个不同版本的人都应该尝试 this solution)。问题是,现在我的应用程序一开始运行,我就会得到这个 FileLoadException HRESULT: 0x80131040。有人知道此问题的可能原因和/或解决方案吗?

是的,我必须更改其中一个 dll 的名称,不,此时仅使用后一个版本是不可能的。

【问题讨论】:

    标签: asp.net .net dll fileloadexception


    【解决方案1】:

    我设法通过 appDomain 使用了两个不同版本的同一个 dll。它非常干净且始终有效,但是是的,它涉及反射和 appDomain 创建,这是性能关键型应用程序的开销。以下是在控制台应用程序中的操作方法,您可以在 asp.net 中执行相同操作:

    //Console app
    using System;
    using System.IO;
    using System.Reflection;
    
    namespace ConsoleApp1
    {
    class Program
    {
        static System.AppDomain oldAssemblyDomain = null;
        static System.AppDomain newAssemblyDomain = null;
    
        static void Main(string[] args)
        {
            LoadOldVersion();
            LoadNewVersion();
    
            Console.ReadLine();
        }
    
        private static void LoadOldVersion()
        {
            oldAssemblyDomain = System.AppDomain.CreateDomain("oldAssemblyDomain", null);
            oldAssemblyDomain.DoCallBack(()=>{
                string dllName = "ClassLibrary1.dll";
                Assembly assembly = Assembly.LoadFile(Directory.GetCurrentDirectory() + @"\" + dllName);
                Type type = assembly.GetType("ClassLibrary1.Class1");
                MethodInfo minfo = type.GetMethod("SayHello", BindingFlags.Public | BindingFlags.NonPublic |
                          BindingFlags.Static | BindingFlags.Instance);
                var returnValue=minfo.Invoke(Activator.CreateInstance(type), null);
                Console.WriteLine(returnValue.ToString());
            });
        }
    
        private static void LoadNewVersion()
        {
            newAssemblyDomain = System.AppDomain.CreateDomain("newAssemblyDomain", null);
            newAssemblyDomain.DoCallBack(() =>
            {
                string dllName = "ClassLibrary2.dll";
                Assembly assembly = Assembly.LoadFile(Directory.GetCurrentDirectory() + @"\" + dllName);
                Type type = assembly.GetType("ClassLibrary1.Class1");
                MethodInfo minfo = type.GetMethod("SayHello", BindingFlags.Public | BindingFlags.NonPublic |
                          BindingFlags.Static | BindingFlags.Instance);
                var returnValue = minfo.Invoke(Activator.CreateInstance(type), null);
                Console.WriteLine(returnValue.ToString());
            });
        }
    }
    }
    
    
    //Lib
    namespace ClassLibrary1
    {
    public class Class1
    {
        public static string SayHello()
        {
            return "Hello 1";
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多