动态加载页面类/程序集:在全局命名空间中找不到类型或名称 [英] Dynamically Loading Page Class/Assembly: Type or name could not be found in the global namespace

查看:338
本文介绍了动态加载页面类/程序集:在全局命名空间中找不到类型或名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个ASP.Net Web应用程序,将其内容(ASPX / ASCX和程序集)存储在文件系统(例如,服务内部)以外的地方,并根据需要动态加载它们。 / p>

我已经成功创建了一个VirtualPathProvider,它负责从备用位置读取ASPX / Master / ASCX文件,但是当这些ASPX页面继承一个类时,



感谢另一个问题我现在能够成功地在运行时将程序集加载到应用程序中。但是当我的运行时试图编译我的页面时,我得到以下错误:


编译器错误消息:CS0400:
类型或命名空间名称Web不能在全局命名空间中找到
(是
,您缺少程序集引用?)



.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
public class default_aspx
:global :: Web.Code.CodeBehind,
System.Web.SessionState.IRequiresSessionState,
System.Web。 IHttpHandler


我已经将我的代码归结为一个简单的例子,说明了什么是错误的,这样很容易看到。 您可以在此下载此代码



在运行时使用AppDomain.AssemblyResolve事件动态加载程序集。这是在global.asax,看起来像这样:

  protected void Application_Start(object sender,EventArgs e)
{
AppDomain.CurrentDomain.AssemblyResolve + = new ResolveEventHandler(Assembly_Resolve);
}

装配Assembly_Resolve(对象发件人,ResolveEventArgs参数)
{
装配assembly = AppDomain.CurrentDomain.Load(Resources.Web_Code);
if(args.Name == assembly.FullName)
{
return assembly;
}
return null;
}

有任何想法吗?



EDIT
如果由于任何原因需要更新Web.Code程序集 - 您需要将其复制到Web项目的ReferencedAssemblies文件夹中,以使这些更改生效。

解决方案

当尝试从DB加载程序集(使用AssemblyResolve事件)时,我遇到了同样的问题。但是如果从磁盘加载程序集或至少将.dll文件的副本保存在磁盘上的某个位置,似乎可以正常工作。


I am trying to create an ASP.Net Web application that stores it's "content" (ASPX/ASCX and assemblies) somewhere other than the file system (Inside a service, for example) and loads them in dynamically as required.

I have successfully created a VirtualPathProvider that takes care of reading ASPX/Master/ASCX files from alternate locations but I am running into problems when those ASPX pages Inherit a class - ie: when they have code behind.

Thanks to answers in another question I am now successfully able to load the assemblies into the application at run time. But when my the runtime attempts to compile my page I get the following error:

"Compiler Error Message: CS0400: The type or namespace name 'Web' could not be found in the global namespace (are you missing an assembly reference?)"

[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()] public class default_aspx : global::Web.Code.CodeBehind, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler

I have boiled down my code to a simple example of what is going wrong so that it is easy for you to see. You can download this code here.

The assembly is being loaded in dynamically at run time using the AppDomain.AssemblyResolve event. This is in the global.asax and looks like this:

protected void Application_Start(object sender, EventArgs e)
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Assembly_Resolve);
}

Assembly Assembly_Resolve(object sender, ResolveEventArgs args)
{
    Assembly assembly = AppDomain.CurrentDomain.Load(Resources.Web_Code);
    if (args.Name == assembly.FullName)
    {
        return assembly;
    }
    return null;
}

Any ideas?

EDIT If for any reason you need to update the Web.Code assembly - you will need to copy it into the "ReferencedAssemblies" folder of the web project for those changes to take effect.

解决方案

I encountered the same problem when trying to load assemblies from DB (and using the AssemblyResolve event). But it seems to work fine if you load assemblies from disk or at least save a copy of the .dll file somewhere on the disk.

这篇关于动态加载页面类/程序集:在全局命名空间中找不到类型或名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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