Укажите явное имя сборки для динамически скомпилированного папки App_code веб-кода веб-сайта ASP.NET?

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

Вопрос

В динамически скомпилированном проекте веб-сайта ASP.NET может быть явно назван сборкой для папки App_Code?

Например, при регулярных обстоятельствах, когда я запускаю веб-сайт ASP.NET, имя сборки, создаваемое в папку Temporary ASP.NET Files\, частично рандомизирован, как App_Code.neizakfo.dll, где Neizakfo - это часть, которая может отличаться. Могу ли я явно предоставить имя для сборки, как App_Code_Web1.dll?

Разъяснение

по бизнес-требованиям, веб-сайт не может быть предложенным скомпилированным / развернутым. Поэтому я ищу решение в контексте папки Temporary ASP.NET Files и динамически скомпилированные сборки, как отмечено выше.


Фон :

Я наткнулся на этот вопрос, ищете способ выполнить создание динамического типа на классе в папке App_Code веб-сайта с использованием квалификации сборочного имени, хранящегося в конфигурации, но созданного с веб-страницы, при этом пересекают границу сборки. Поскольку веб-страница и код App_Code скомпилированы на две разные сборки по умолчанию, по умолчанию по умолчанию метода типа. Getttype (..) по поэтапному поиску имени типа либо в текущей выполнении сборки (веб-страницы) или в MSCorlib не Достаточно для получения любого типа из сборки App_Code. Будучи рандомизированным, имя сборки App_Code не известно для меня, чтобы включить в классную квалифицированную строку.

Я могу поставить тип данных в библиотеке классов (потому что это имеет предварительно определенное / точное имя), чтобы избавиться от этой проблемы, однако я хотел бы знать, как сделать это внутри самого веб-сайта без создания класса Библиотечный проект для этой цели.

Это было полезно?

Решение

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