对于动态编译ASP.NET网站的APP_ code文件夹提供一个明确的大会名称? [英] Provide an Explicit Assembly Name for a Dynamically Compiled ASP.NET Website's App_Code Folder?

查看:287
本文介绍了对于动态编译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网站生成到临时ASP.NET文件\\ 文件夹中的程序集名称是部分随机像<$ C常规情况$ C> 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?

澄清

按业务需求,该网站不能被precompiled /部署。因此,我正在寻找在的情况下解决临时ASP.NET文件文件夹和动态编译组件如上所述。

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_ $ C $文件夹C类执行动态类型实例化的一种方式,但是从网页实例化,从而跨越组装边界。因为网页和APP_ code 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.

我可以把数据类型在类库(因为这的确有一个predefined /确切名称),以摆脱这个问题的,但是我想知道如何做到这一点的网站本身没有内为此目的设立一个类库项目。

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文章/ A>编译项目时。

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.

您可以做的另一件事就是把所有的code在APP_ code OUT到它自己组装,然后添加作为项目引用。这也将简化这个问题。

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.

最后,您可以枚举所有DLL的bin目录中,搜索每一个你正在寻找的类型。由于这是相当昂贵的,做一次,某处缓存结果,所以你不要继续做下去,每次你看那种类型的。这可能是最糟糕的解决方案。

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.

这是微不足道的web应用项目做,但我相信你被卡住的网站吗?

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

编辑:对于评论的更新;如果我使用的Web发布工具,那么所有在APP_ code中的code的推移在bin目录中名为APP_ code.dll DLL - 即使我用固定命名此行为不会改变(所有固定效应命名的DLL的命名每个页面,用户控件)。如果我使用<一个href=\"http://www.google.co.uk/url?sa=t&rct=j&q=ilspy&source=web&cd=1&ved=0CDIQFjAA&url=http://ilspy.net/&ei=eb5PT9i1HsjKsgbuh7jTCw&usg=AFQjCNG_QJ5lsOBsz1ot_aWA964m5Gpcnw&cad=rja\"相对=nofollow> 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!

我创建了一个简单的类叫做人带标识和名称,把它放在APP_ code,编译站点,然后运行以下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的传人,我得到以下信息:

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

类型'人'两个'APP_ code.dll'存在和'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文件夹提供一个明确的大会名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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