4

我在这里遇到了一个真正令人头疼的问题……它似乎是 ASP.NET 中更令人沮丧的话题之一。

我有一个程序集,它实现了很多自定义的 Linq 东西,它的核心是零网络功能。我有一个额外的程序集,它使用特定于 Web 的行为扩展了这个程序集。

Web 特定行为带有在 ASCX 模板化的用户控件中标记的几个用户控件。

我无法很好地完成此程序集,以便重新部署以用于其他应用程序。让我来看看我到目前为止所尝试的:

  1. 使用构建事件将 ASCX 文件复制到使用 Web 应用程序;远非理想,而且是一场部署噩梦
    • 实现了一个自定义 VirtualPathProvider 并将 ASCX 模板作为嵌入式资源嵌入到程序集中。不幸的是,当在消费应用程序中使用 Register 指令时,它会将设计器声明创建为 UserControl,我需要在其中声明实际控件类型;不可预见的(通常)和不受欢迎的。
    • 创建了一个 Web 部署项目来编译 UserControls,但是编译的用户控件然后成为另一个程序集的一部分,并且不再来自我的 Web 程序集中的类定义——程序集需要根据请求上下文实例化它们

所以第 1 号只是废话,第 2 号并没有给我我想要的类型支持,第 3 号我想我将提出一个合理的解决方案:

  • 将所有非控制类集中到App_Code文件夹中,准备一个工厂类,该类将使用反射构造所需控制类型的对象,并期望被反射的类型将出现在部署输出中(希望通过ClassName属性的存在来保证在Control指令中)。

还有将 ASCX 控件重写为自定义控件的另一种选择,但目前没有资源来考虑它,而且我们没有这样做的专业知识,它们作为 UserControls 可以正常工作。

我是否遗漏了一些明显的东西,可能更简单的东西,或者这只是故意困难?我读过关于 ASP.NET 编译过程的故事,在我穿越这个主题的过程中,它的设计非常不幸。

4

1 回答 1

2

好吧,我想我已经做到了......通过注意我最后一种方法的一些烦人的陷阱,我在使用 Web 部署项目在 Web 应用程序项目中编译 ASCX 用户控件时建议以下内容:

  1. 避免放入类,App_Code除非它们是独立类或辅助类,ASP.NET 将其视为一个speshul文件夹,我失去了它的含义,随之而来的是混乱、混乱和混乱。不过,此文件夹中的代码确实会在 Web 部署项目中得到输出。
    • Pay close attention to your assembly names, their root namespaces and deployment output assembly name- you'll get access is denied errors if you have any naming conflicts during the aspnet_merge process.
    • Ultimately you'll most likely end up deploying 2 assemblies, I tried to create only one but the deployment output was still pointing to type definitions in the source assembly. This isn't a problem if you don't have any other types in your Web Application Project--I have so it was a problem for me. In my case, my final output was:
    • <Organisation>.<TechnologyName>.Web.DLL - Compiled Web Application Assembly (containing the ASCX templates)
    • <Organisation>.<TechnologyName>.Web.UI.DLL - ASP.NET Compiled UserControl assembly, created by Web Deployment Project
    • Clean often, and check that the Web Application Project's bin and obj paths are cleared of any previous junk built when you perhaps hadn't finalised your namespace or assembly naming scheme--the Web Deployment Project will be quite keen to include these, causing a fine mess.
    • Check your imported namespaces, the ASP.NET compiler likes to refer to the Import directive in the ASCX template, and it also considers imported namespaces present in web.config's <configuration><system.web><pages><namespaces> element, tweak if you get unknown definitions appearing during the deployment process.

Have some patience spare, it's quite tricky! But you do get some nice re distributable UserControls at the end of it!

Phew!

于 2009-06-18T10:04:13.523 回答