AssemblyResolve event is not firing during compilation of a dynamic assembly for an aspx page

末鹿安然 提交于 2019-11-28 14:04:16

I'm not sure what's causing the missing assembly reference but if we step back a little and go to the point where your program works as expected then we have to solve a different problem. This problem is the locking of the loaded assembly. .Net framework always locks the loaded assemblies. The reason that you can update dll files inside bin folder is actually a trick. You see, AppDomain has a nice property called ShadowCopyDirectories that dictates the directories which will be shadow copied when an assembly is loaded. So by altering the shadow copied directories list you can load from any folder without locking your assemblies :

    protected const string ApplicationAssembliesFolder = "~/Assemblies";

    protected void Application_Start(object sender, EventArgs e)
    {
        var assembliesPath = Server.MapPath(ApplicationAssembliesFolder);

        AppDomain.CurrentDomain.SetShadowCopyPath(
            AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories + 
            Path.PathSeparator + assembliesPath);

        Assembly.LoadFrom(
            Path.Combine(assembliesPath, "Example.dll"));
    }

I think I got it working with the following:

 public Assembly GetAssembly()
    {
        Assembly result = cache.Get(assemblyKey) as Assembly;

        if (result == null)
        {
            lock (this)
            {
                result = cache.Get(assemblyKey) as Assembly;
                if (result == null)
                {
                    assemblyName = System.Reflection.AssemblyName.GetAssemblyName(assemblyFile);
                    result = Assembly.Load(assemblyName);
                    cache.Add(assemblyKey, result, new CacheDependency(assemblyFile), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, new CacheItemRemovedCallback(OnAssemblyRemoved));
                }
            }
        }
        return result;
    }

This only works when the new assembly has a different version from the old one which kind of makes sense. The application does not restart but the new assembly is loaded. I tried updating both the markup of the aspx page and the codebehind and it worked as expected.

To summarize:

  1. AppDomain.CurrentDomain.SetShadowCopyPath
  2. Assembly.Load(AssemblyName.GetAssemblyName(assemblyFile))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!