動的にコンパイルされたASP.NETのWebサイトのAPP_CODEフォルダの明示的なアセンブリ名を指定しますか?

StackOverflow https://stackoverflow.com/questions/9520231

質問

動的にコンパイルされたASP.NET Webサイトプロジェクトでは、APP_CODEフォルダのアセンブリは明示的に名前を付けられますか?

例えば、ASP.NET Webサイトを実行すると、Temporary ASP.NET Files\フォルダに生成されたアセンブリ名は、App_Code.neizakfo.dllのように部分的にランダム化されています。 Neizakfo は異なる可能性がある部分です。 App_Code_Web1.dllのようなアセンブリの名前を明示的に提供できますか?

明確化

ビジネス要件により、Webサイトをプリコンパイル/デプロイすることはできません。したがって、上記のようにTemporary ASP.NET Filesフォルダと動的にコンパイルされたアセンブリのコンテキストで解決策を模索しています。


背景
私はこの質問に遭遇しました。デフォルトでは、WebページとAPP_CODEコードはデフォルトでは2つの異なるアセンブリにコンパイルされているため、Type.getType(..)メソッドのデフォルトの動作は、現在の実行アセンブリ(Webページ)またはMSCORLIBでのいずれかではありませんApp_codeアセンブリから任意の型を選ぶのに十分です。ランダム化されているため、App_codeアセンブリ名は、アセンブリ修飾文字列に含めることがわかりません。

この問題を解決するためにデータ型をクラスライブラリに入れることができますが、クラスを作成せずにWebサイト自体の中でこれを行う方法を知りたいのですが。目的のための図書館プロジェクト。

役に立ちましたか?

解決

You can sort of do this in a WebSite project.

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

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.

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.

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.

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

EDIT: 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!

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.

Further Edit

The penny drops! If I then do:

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

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

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

So it's time to be devious.

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);

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 ;-)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top