ASP.NET 项目中 bin 文件夹外的程序集解析 [英] Assembly resolving in ASP.NET project outside of bin folder

查看:49
本文介绍了ASP.NET 项目中 bin 文件夹外的程序集解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ASP.NET Web 开发服务器的 bin 文件夹之外解析程序集引用?这对于没有相同 dll 的副本很有用.

How can i resolve assemblies references outside of bin folder of ASP.NET web-development server? This can be useful to not have copies of same dll's.

没有任何效果: web.config 中的探测元素 不起作用,我无法设置域,因为它是应用程序管理器,而且我无法订阅 resolve assembly event 因为太晚了 - 当我可以订阅时初始化已经结束.那我该怎么办?

Nothing is working: probing element at web.config don't work, i can't set up domain because it do application manager, and i can't subscribe on the resolve assembly event because it's too late - when i can subscribe initialization is over. So what can i do?

推荐答案

我们可以使用 PreApplicationStartMethodAttribute并将它们标记为一些没有参数的公共静态无效方法(在 web 项目程序集中).这可以在 AssemblyInfo.cs 类中完成例如:

We can use PreApplicationStartMethodAttribute and mark them some public static void method(in web-project assembly) with no arguments. This can be done at AssemblyInfo.cs class For example:

[assembly: PreApplicationStartMethod(
  typeof(Web.Initializer), "Initialize")]

该方法将在编译之前但在处理 web.config 之后调用.所以我们必须明确地告诉编译器女巫程序集它在编译期间需要使用.我们还需要在这里订阅程序集解析事件,以便我们可以管理程序集解析.示例如下:

That method will be called before compilation but after processing of the web.config. So we must explicitly tell to the compiler witch assembly it need to use during compilation. Also we need to subscribe here on Assembly Resolve event so we can manage assemblies resolving. Here is example:

  public static class Initializer
    {
        public static void Initialize()
        {
            AppDomain.CurrentDomain.AssemblyResolve += LoadFromCommonBinFolder;
            var referAsm = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
            foreach (var assemblyName in referAsm)
            {
               try
               {
                var curAsm = Assembly.Load(assemblyName);
                BuildManager.AddReferencedAssembly(curAsm);
                LoadChildReferences(curAsm);
               }
               catch {}
            }
        }

    private static void LoadChildReferences(Assembly curAsm)
    {
       foreach (var assemblyName in curAsm.GetReferencedAssemblies())
       {
           try
           {
             BuildManager.AddReferencedAssembly(Assembly.Load(assemblyName));
           }
           catch {}
       }
    }

        private static Assembly LoadFromCommonBinFolder(object sender, ResolveEventArgs args)
        {
            string commonBinFolder = System.Configuration.ConfigurationManager.AppSettings["CommonBinFolderPath"];

            if (String.IsNullOrEmpty(commonBinFolder))
            {
                throw new InvalidOperationException("​​CommonBinFolderPath in the app.config isn't seted.");
            }

            string assemblyName = new AssemblyName(args.Name).Name;
            string assemblyPath = Path.Combine(commonBinFolder, assemblyName);

            if (!File.Exists(assemblyPath + ".dll"))
            {
                if (!File.Exists(assemblyPath + ".exe"))
                {
                    //searching for resources
                    var ci = CultureInfo.CurrentUICulture;
                    assemblyPath = Path.Combine(commonBinFolder, ci.Name, assemblyName + ".dll");
                    if (!File.Exists(assemblyPath))
                    {
                        assemblyPath = Path.Combine(commonBinFolder, ci.Parent, assemblyName + ".dll");
                        if (!File.Exists(assemblyPath))
                        {
                            return null;
                        }
                    }
                }
            }

            return Assembly.LoadFrom(assemblyPath);
        }
    }

在这种情况下,Web.Project.Assembly"仍必须位于 bin 文件夹中.其他程序集可以从任何文件夹共享.

At this case "Web.Project.Assembly" still must be located in the bin folder. Others assemblies can shared from any folder.

包含在 web.config 文件中的编译元素下的程序集也必须位于 bin 文件夹或设置了探测元素的子文件夹中.

Assemblies that are included under compilation Element in the web.config file must be also in the bin folder or at sub folder with probing element setted.

在同样的情况下,我们还必须向此代码添加对子程序集的引用.

In same cases we must also add to this code adding references to child assemblies.

这篇关于ASP.NET 项目中 bin 文件夹外的程序集解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆