本文介绍了AssemblyResolve事件是不是一个动态装配的编译过程中一个aspx页面射击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这一个是真的得罪我了。这里所说: 我的目标是在包含嵌入的ASPX,ASCX等等。我也想是不锁定磁盘上的汇编文件运行时加载组件这样我就可以在运行时更新,而无需重新启动应用程序(我知道这将离开以前的版本(S)加载)。 要为此我写了做的伎俩虚拟路径提供者。我已经订阅了CurrentDomain.AssemblyResolve事件以便重定向框架到我装配 的问题是,当框架试图编译动态组件为aspx页面我得到了以下内容: 编译器错误信息:CS0400:类型或命名空间名称页数不能在全局命名空间中找到(是否缺少程序集参考) 源错误:公共类app_resource_pages__version_1_0_0_0__culture_neutral__publickeytoken_null_default_aspx:全球:: Pages._Default,System.Web.SessionState.IRequiresSessionState,System.Web.IHttpHandle 我发现,如果我加载程序集与的Assembly.Load(的AssemblyName)或Assembly.LoadFrom(文件名)我不明白上面的错误。如果我的Assembly.Load(字节[])(以便不锁定)加载,抛出异常,但我AssemblyResolve处理程序,被称为是正确返回集时(它被称为一次)。 所以我猜测,当框架解析asp的标记而不是当它试图创建aspx页面动态装配它被称为一次。 解决方案 我不知道是什么导致丢失的程序集的引用,但如果我们退一步一点,走到哪里你的程序运行正常,然后我们要解决的地步不同的问题。这个问题是装载组件的锁定。 Net框架始终锁定加载的程序集。您可以更新bin文件夹内的dll文件的原因其实是一招。你看,的AppDomain 有一个名为很好的属性 ShadowCopyDirectories 的决定将在一个程序集加载将影像复制的目录。因此,通过改变影像复制的目录列表,您可以从任何文件夹载入时没有锁定您的组件: 保护常量字符串ApplicationAssembliesFolder =〜 /组件; 保护无效的Application_Start(对象发件人,EventArgs五) { VAR assembliesPath =使用Server.Mappath(ApplicationAssembliesFolder); AppDomain.CurrentDomain.SetShadowCopyPath( AppDomain.CurrentDomain.SetupInformation.ShadowCopyDirectories + Path.PathSeparator + assembliesPath); Assembly.LoadFrom( Path.Combine(assembliesPathExample.dll)); } This one is really pissing me off. Here goes:My goal is to load assemblies at run-time that contain embedded aspx,ascx etc. What I would also like is to not lock the assembly file on disk so I can update it at run-time without having to restart the application (I know this will leave the previous version(s) loaded). To that end I have written a virtual path provider that does the trick. I have subscribed to the CurrentDomain.AssemblyResolve event so as to redirect the framework to my assemblies.The problem is that the when the framework tries to compile the dynamic assembly for the aspx page I get the following:Compiler Error Message: CS0400: The type or namespace name 'Pages' could not be found in the global namespace (are you missing an assembly reference?)Source Error: public class app_resource_pages__version_1_0_0_0__culture_neutral__publickeytoken_null_default_aspx : global::Pages._Default, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandleI noticed that if I load the assembly with Assembly.Load(AssemblyName) or Assembly.LoadFrom(filename) I dont get the above error. If I load it with Assembly.Load(byte[]) (so as to not lock it), the exception is thrown but my AssemblyResolve handler, when called is returning the assembly correctly (it is called once).So I am guessing that it is called once when the framework parses the asp markup but not when it tries to create the dynamic assembly for the aspx page. 解决方案 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")); } 这篇关于AssemblyResolve事件是不是一个动态装配的编译过程中一个aspx页面射击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-25 10:01