本文介绍了为动态编译的 ASP.NET 网站的 App_Code 文件夹提供显式程序集名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在动态编译的 ASP.NET 网站项目中,是否可以显式命名 App_Code 文件夹的程序集?

In a dynamically compiled ASP.NET Website project, can the assembly for the App_Code folder be explicitly named?

例如,在正常情况下,当我运行 ASP.NET 网站时,生成到 Temporary ASP.NET Files 文件夹中的程序集名称是部分随机的,如 App_Code.neizakfo.dll 其中 neizakfo 是可以不同的部分.我可以显式地为程序集提供一个名称,例如 App_Code_Web1.dll?

For example, under regular circumstances when I run an ASP.NET website the assembly name generated into the Temporary ASP.NET Files folder is partially randomized like App_Code.neizakfo.dll where neizakfo is the portion that can differ. Can I explicitly provide a name for the assembly like App_Code_Web1.dll?

说明

根据业务需求,无法预编译/部署网站.因此,我在 Temporary ASP.NET Files 文件夹和动态编译的程序集的上下文中寻求解决方案,如上所述.

By business requirements, the website cannot be precompiled/deployed. Therefore I'm seeking a solution in context of the Temporary ASP.NET Files folder and dynamically compiled assemblies as noted above.

背景:
我在寻找一种方法来使用存储在配置中的程序集限定名称对网站的 App_Code 文件夹中的类执行动态类型实例化时遇到了这个问题,但从网页实例化,从而跨越了程序集边界.由于默认情况下网页和 app_code 代码被编译为两个不同的程序集,因此 Type.GetType(..) 方法在当前执行程序集(网页)或 mscorlib 中搜索类型名称的默认行为不会足以从 App_Code 程序集中选择任何类型.由于随机化,我不知道 app_code 程序集名称包含在程序集限定字符串中.

Background:
I came across this question while looking for a way to perform dynamic type instantiation on a class in the App_Code folder of a website using an assembly-qualified name stored in configuration, but instantiated from the web page, thus crossing an assembly boundary. Because the web page and app_code code are compiled into two different assemblies by default, the Type.GetType(..) method's default behaviour of searching for the Type name either in the current executing assembly (the web page) or in mscorlib doesn't suffice for picking any Type from the App_Code assembly. Being randomized, the app_code assembly name is not known for me to include in the assembly-qualified string.

我可以将数据类型放在一个类库中(因为它有一个预定义/确切的名称)来解决这个问题,但是我想知道如何在网站本身内部执行此操作而不创建一个类图书馆项目为目的.

I can put the data Type in a class library (because that does have an predefined/exact name) to get rid of this problem, however I'd like to know how to do this inside the website itself without creating a class library project for the purpose.

推荐答案

您可以在网站项目中执行此操作.

You can sort of do this in a WebSite project.

有一篇关于使用 -fixednames 标志的 MSDN 文章编译项目时.

There's an MSDN article on using the -fixednames flag when compiling the project.

这有效地为每个页面创建了一个程序集 - default.aspx.dll.但是,这对您来说只是稍微有用一点,因为您在加载时仍然需要知道要查找的控件或页面的名称 - 因此您必须确保您的类型和命名是一致的.但是,它应该尊重 app_code 中类的名称,因此这可能对您有用.

This effectively creates an assembly for each page - default.aspx.dll. However, this is only marginally more useful to you as you still need to know the name of the control or page you are looking for when you are loading - so you have to ensure your types and naming is consistent. It should, however, respect the name of the classes in app_code so this may work for you.

您可以做的另一件事是将 app_code 中的所有代码移出到它自己的程序集中,然后将其添加为项目引用.这也将简化这个问题.

One other thing you could do is move all of the code in app_code out into it's own assembly, and then add that as a project reference. That would also simplify this problem.

最后,您可以枚举 bin 目录中的所有 dll,并在每个 dll 中搜索您要查找的类型.由于这是相当昂贵的,所以只做一次,并将结果缓存在某处,这样您就不会每次查找该类型时都继续这样做.这可能是最糟糕的解决方案.

Lastly, you could enumerate all of the dll's in the bin directory, and search each one for the type you are looking for. As this is fairly expensive, do it once, and cache the result somewhere so you don't keep doing it everytime you look that type up. This is probably the worst solution.

这在 WebApplication 项目中是微不足道的,但我认为您被 WebSite 困住了?

This is trivial to do in a WebApplication project, but I assume you are stuck with the WebSite one?

作为评论的更新;如果我使用发布 Web 工具,那么 app_code 中的所有代码都会进入名为 App_Code.dll 的 dll 中的 bin 目录 - 即使我使用固定命名,这种行为也不会改变(所有固定命名都会影响 dll 的命名)每个页面,用户控件).如果我使用 ILSpy 在这个文件上,我可以在那里看到我的课程.所以我知道程序集的名称和它的位置 - 我应该能够以最少的努力获得其中的类型.我想知道为什么我看到你的行为不同!

As an update for the comments; if I use the Publish Web Tool, then all of the code in app_code goes in the bin directory in a dll called App_Code.dll - this behaviour does not change even if I use fixed naming (all fixed naming effects the naming of the dll's for each page, usercontrol). If I use ILSpy on this file, I can see my classes in there. So I know the name of the assembly, and it's location - I should be able to get at the types in it with minimal effort. I wonder why I'm seeing different behavior to you!

我创建了一个简单的类,叫做Person",有一个 Id 和 Name,把它放在 App_Code 中,编译网站,然后运行下面的代码:

I created a simple class called "Person" with an Id and Name, put it in App_Code, compiled the site, and then ran the following code:

  Type myType = Assembly.LoadFrom(Server.MapPath("~/bin/App_Code.dll")).GetType("Person", true);
  Response.Write(myType.ToString());

正如预期的那样,它写出了人".

It wrote out "Person", as expected.

进一步编辑

一分钱掉了!如果我这样做:

The penny drops! If I then do:

  object myObject= Activator.CreateInstance("App_Code.dll", "Person");

并尝试将 myObject 转换为 person,我收到以下消息:

And try to cast myObject to person, I get the following message:

'App_Code.dll' 和 'App_Code.jydjsaaa.dll' 中都存在类型 'Person'

The type 'Person' exists in both 'App_Code.dll' and 'App_Code.jydjsaaa.dll'

所以是时候变得狡猾了.

So it's time to be devious.

在 Global.asax 中的 Application_OnStart 上,执行以下操作:

in Global.asax, on Application_OnStart, do the following:

Application["App_Code_Assembly"] = Assembly.GetAssembly(typeof(Person));

在我的测试默认页面中,我做了:

In my test default page, I then did:

  Assembly app_Code = Application["App_Code_Assembly"] as Assembly;
  Response.Write(app_Code.FullName);

它给了我随机命名的 app_code,它实际上在临时 ASP.Net 文件中运行.

Which gave me the randomly named app_code it is actually running with in Temporary ASP.Net Files.

这就是我讨厌网站项目的原因;-)

This is why I hate Web Site Projects ;-)

这篇关于为动态编译的 ASP.NET 网站的 App_Code 文件夹提供显式程序集名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:16