Where is app code compiled to




















But normally, compiled code is stripped out of the output so there aren't multiple versions of the same class causing naming conflicts. In any case, we need to compile the class initially, but there are two options to persist a re-compilable copy:. At a bare minimum, this is a tricky task to charge to the compiler. Any code that consumes a class, must have a guarantee that it exists at compile time. So producing an identical class is treated more like a picture of that class.

The underlying type might be the same, but ce n'est une pipe. If we use an interface , we can compile it with the initial build and any dynamic types can implement that same interface. This way we are safely relying on something that exists at compile time, and our created class can be safely swapped out as a backing property. If we cast types across assemblies , it's important to note that any existing usages rely on the type that was originally compiled.

So we'll need to grab the values from the dynamic type and apply those property values to the original type. Per evk , I like the idea of querying AppDomain. Per S. Deepika , I like the idea of dynamically compiling from a file, but don't want to have to move the values to a separate project. When we debug locally, all. This is really close to solving problem 1. We have access to both types every time the app runs.

We can use the compiled version at design time and any changes to the file itself will automatically be recompiled by IIS into a version that we can access at runtime. The problem is apparent however once we step out of debug mode and try to publish the project. However, when a. If the file was left in, it would produce two identical classes, which would create naming conflicts whenever either one was used.

We can try to force its hand by both compiling the file into the project's assembly and also copying the file into the output directory. Perhaps we could automate that into build events, but we'll try something else Just create a new folder named Config and add a class that will store the values we want to be able to modify dynamically:.

Again, we'll want to go to the file properties F4 and set to compile AND copy to output. This will give us a second version of the file we can use later. We'll consume this class by using it within a static class that exposes the values from anywhere.

This helps separate concerns, especially between the need to dynamically compile and statically access. What we'll attempt to do inside the static constructor, is seed Apple with the values from our dynamic class. This method will be called once every time the application is restarted, and any changes to the bin folder will automatically trigger recycling the app pool.

MapPath instead of Server. Also, rather than fight dependencies, I just gave the compiler access to every assembly that was currently in the App Domain , same as it would have gotten on the original compilation. There's probably a way to do that with less overhead, but it's a one time cost so who cares. CopyProperties - To map the new properties onto the old object, I've adapted the method in this question on how to Apply properties values from one object to another of the same type automatically?

Okay, so there are other more conventional ways to accomplish the same goal. But this provides the absolute easiest, most flexible, strongly typed way to store config values I've ever seen. Normally config values are read in via an XML in an equally odd process that relies on magic strings and weak typing. Instead here, we have the final type from the get go, and we can automate all of the ORM work between identical classes that just happen to be compiled against different assemblies.

In either case, the dream output of that process is to be able to write and consume C directly. In this case, if I want to add an extra, fully configurable property, it's as easy as adding a property to the class.

It will be available immediately and recompiled automatically if that value changes without needing to publish a new build of the app. You can move configuration part to separate project, and create common interface like IApplicationConfiguration. ReadConfiguration to access it. You can compile the code dynamically at run time like below, and you can access the configuration details using reflection.

It should and usually will fail. Because ASP. NET compiles on a directory level and the CustomUserControl lives in a separate directory and goes into a separate assembly. IntelliSense will show a big, red exclamation point or nothing at all for the MessageDisplay control. When you run the page it will bomb.

You can reference the control as the Control type of course, but if you need to access any custom properties on the user control beyond Control properties, you have to resort to using reflection. As an alternative, you can choose to not load controls dynamically or at least provide a mechanism to load user controls beforehand on a page with the appropriate Register tags.

There are cases where inherited controls are not recognized properly by the higher level classes resulting in type mismatches, especially when loading UserControls on master pages. Removing controls on inherited pages also will cause problems, even if the controls are defined explicitly in lower-level classes.

The overall ASP. NET application and have it running inside of the development environment for testing, the next big step is to deploy the application. When it comes to moving your Web application online there are a number of deployment models available. These are listed in Table 1. Although in-place deployment is the simplest format conceptually, it has a few serious shortcomings.

First and foremost you have to deploy your source files, which is a security issue both for source code protection as well as for security concerns. If you put the source code on the server, the code is potentially accessible to anybody with physical access to the box.

Someone else could look at the code and even change it on the server. Though this may be unlikely, the potential for damage if somebody gains access to the machine is huge. Actual deployment to the server involves simply moving the files from a development directory to the server, but it means that anytime you make a change you have to remember which files to update on the server unless you redeploy the entire site.

The most common deployment scenario is full pre-compilation. The compiler takes the existing Web site and publishes the site to a new directory copying all files that relate to the Web sites including static files like images and CSS files.

The compiler is essentially generating a complete copy of your Web site outputting a large number of compiled files in the BIN directory. Pre-compilation comes in many different flavors. You can choose to compile pages into one assembly per page or compile them into a directory. You can compile with debug mode on or off. You can compile a physical path or an IIS virtual directory etc. The first example compiles the entire site with directory-level assemblies that are as close to a default compilation as it comes:.

The marker file is maintained for one reason only: To support Windows Authentication. If you need to set specific Windows file rights on a directory or specific file, an actual file must exist in order for Windows Authentication to work. The BIN directory itself contains a number of assemblies. Remember that I had one Visual Basic. NET page in the project and this page compiles into a separate assembly.

If you have a global. NET theme used. The theme classes provide ASP. NET with the location of the theme directory and any of the linked style sheets used with a theme.

Themes are hard-coded into a pre-compiled Web application. Whatever value is configured in web. If you need to override themes in a pre-compiled application, you have to explicitly override the theme for each page in the OnPreInit event of the page. These files are not optional as they map the ASPX pages to the appropriate precompiled classes in the precompiled assemblies. If you remove the.

NET error. I already showed the directory-level compilation mode. One really annoying aspect of this mode is that it creates new IDs for all of the files on each build. Notice that each assembly has a generated name, and any time you re-compile the site the names of the assemblies and support files change. For deployment to a Web site this means that you pretty much need to completely redeploy all files in the BIN directory every time!

Directory-level compilation is just one of about 20 compilation combinations available. Here is the command line to use fixed names mode. The one advantage of this approach is that it produces a repeatable install, but the file names still have a randomly generated hashcode at the end.

However, it is possible to update just the pages or controls that have changed, unlike directory-level compilation where every single compiled file and its associated. But there are a lot of assemblies generated. Copying these assemblies to a live server will cause your online application to become unstable and likely fail as the files are updated one at a time. Neither of these two approaches offers a really clean deployment scenario because you have to copy a lot of files. Compared to ASP.

When present this page is displayed whenever any request hits the ASP. NET pipeline. Another option is partial compilation, which compiles only the CodeBeside classes but leaves the ASPX pages to compile at run time on the server. NET Compiler calls this an Updateable site , but this is only partially correct.

When compiled with the -u option, the ASPX pages remain intact. In turn you will no longer need the. So the BIN directory looks a bit cleaner with this approach.

However, now you need to make sure to deploy your ASPX pages and keep them in sync with the server. Note that the Inherits tag includes the dynamically generated assembly name. Unless you use fixed names compilation, the assembly name will change on every build so you still have to update the page along with the assemblies if you use the default directory-level compilation. To figure the compilation process option out, I built a small ASP.

NET compiler for the most common options. The utility, shown in Figure 7 , allows you to play with the various compiler combinations and quickly see the results in the output folder. It can also generate a batch file for you and lets you jump directly to your FTP client to upload the code to the server.

As you can see there are lots of problems and no sensible deployment options with ASP. The best you can hope to do is a full re-deploy of your BIN directory that potentially contains a lot of files. Even before ASP. NET solution Figure 8. NET compiler creates from stock projects. There are actually several other options for compilation including creating directory-level assemblies or page-level assemblies, but WDP adds support for fixed names for each of these options so the compilation of these assemblies is repeatable.

NET compilation of your site and then merging the resulting assembly output into a single assembly, one per directory or one per page. In all cases, use repeatable names for the assemblies created. The tool includes other options such as signing and versioning the resulting assemblies, as well as the ability to create a virtual directory in the output folder.

It even has the facility to change the content of your web. You can run this utility against a compiled ASP. NET project and produce the desired assembly merging. I used this tool to provide the integration into the ASP. NET Compiler Tool shown in Figure 7 , which is compatible with this tool and can merge into a single assembly as well.

If you use stock projects with Visual Studio , WDP is a no-brainer since it produces much more manageable deployment output. The output generated still includes. In most update scenarios you only update the main assembly plus any changed support assemblies. Add to this the fact that stock projects are housed in a project format that is not like other standard Visual Studio projects and you have a recipe for grumbling developers who are used to a fairly straightforward compilation, project, and deployment model from previous versions of Visual Studio.

WAP brings back a more structured project style in Visual Studio that is in many ways more similar to a Visual Studio project, but at the same time embraces all of the new ASP. Because the project is a standard Visual Studio. NET project, it automatically contains built-in support for XML comments, compiler directives, and assembly versioning, as well as MSBuild support, pre- and post-build events etc. All of these things that you take for granted in Visual Studio.

NET projects are missing in stock projects. In this model, Visual Studio is responsible for explicitly compiling all CodeBehind classes, and any other classes defined anywhere in the project. So, all of the code in your project compiles into a single assembly. NET code in the same project any longer and you have to explicitly compile your code in Visual Studio each time you make a change to any of the CodeBehind classes.

Debugging is also more exacting-a code change in most cases requires you to recompile and restart the debugger. However, compilation is fast once again with WAP.

I have one project with roughly 80 pages and control classes and it compiles in a couple of seconds. With stock projects a full compile took a painful seconds. WAP also supports Edit and Continue if and only if you use the built-in Web server to debug your applications. The built-in Web server is required because Visual Studio needs to control the parent process it debugs when you use Edit and Continue. The single assembly compilation for all CodeBehind code does away with a lot of the problems regarding inheritance and control and page referencing, because every page and control of the Web project guarantees that it can freely reference every other control and page of the project, since they all end up in the same assembly.

The page parse tree classes are still separate, but all the base classes exist in a single assembly that defines both the control properties and custom method interface from your user code. The class interface is no longer split across two classes as is the case in the CodeBeside model, which is the root of many of the problems I discussed earlier.

It uses partial classes to separate out the user code and the designer-generated control definitions. So a WAP page consists of three different files:. By not generating code into the CodeBehind class, WAP keeps the user code file clean like stock projects, and also minimizes problems by keeping the designer file and visual designer surface in sync. This seems like a subtle change but it has a big effect.

By creating property definitions on the user code base class, the class interface is fully defined at this level and when Visual Studio compiles these classes into a single assembly, the classes are fully defined and you can reference them from anywhere in the project as the generated CodeBehind assembly is global to the Web application.

The other big benefit of WAP is that it makes deployment much easier than stock projects-easier even than stock projects with WDP. The easiest way to do this is to use Explorer and drag-and-drop the files from the old project directly into the WAP project in Solution Explorer. You have to convert all markup pages that contain any CodeBeside code.

Once you have copied the files to the new project, use the Convert to Web Application option on the Project menu to convert a single file, a folder, or the entire project to a WAP project.

The conversion goes through each of the stock project pages and fixes up the Page directive, converting the CodeFile attribute to a CodeBehind attribute and creating the. Before you move files over for conversion, make sure your project compiles properly in stock projects as the conversion relies on resolving references and dependencies to figure out how to upgrade them.

Any way that you look at it, compilation in ASP. NET has some complexities and I hope this article has given you some deeper insight into the compilation and deployment process. Usually options are good, but I think in this case the choices can be overwhelming. So watch out for the gotchas and start thinking about these issues early on in the project.

I prefer to use Web Application Projects whenever I can for any serious work. To me, Web Application Projects bring sanity to the Web development process with a predictable and consistent project and compilation mode that was lacking with stock projects. The file-based model using its Edit and Go model of running applications is appealing and make stock projects more easygoing and productive.

And even if you use stock projects and run into a dead end you can easily switch to WAP. My Subscriber Account Advertise Write. Training Home State of. Staffing Home Looking for Staff?

Looking for Work? Contact Us. Dark Kimbie. Published in:. Filed under: ASP. This tutorial explores the benefits of precompilation and shows how to precompile a website from within Visual Studio and from the command line. One of the key differences between these project types is that WAPs require explicit compilation whereas WSPs use automatic compilation , by default. With WAPs, you compile the web application's code into a single assembly, which is created in the website's Bin folder.

Deployment entails copying the markup content the. On the other hand, you deploy WSPs by copying both the markup pages and their corresponding code-behind classes to the production environment. The code-behind classes are compiled on demand on the web server. Refer back to the "Explicit Compilation Versus Automatic Compilation" section in the Determining What Files Need to Be Deployed tutorial for more background on the differences between the project models, explicit and automatic compilation, and how the compilation model affects deployment.

The automatic compilation option is simple to use. There's no explicit compilation step and only the files that have been modified need to be deployed, whereas explicit compilation necessitates deploying the changed markup pages and the just-compiled assembly. However, automatic deployment has two potential drawbacks:. If either of the two above shortcomings are deal breakers, you can either switch to the WAP model or precompile the WSP prior to deployment.

This tutorial examines the precompilation options best suited for a hosted website and walks through the precompilation process and deployment of a precompiled website. Before we look at the available precompilation options, let's first talk about the code generation and compilation that occurs when an ASP.

NET page is requested for the first time since it's been created or last updated. As you know, ASP. NET pages are composed of two portions: declarative markup in the. The steps performed by the runtime when an ASP. NET page is requested depends on the application's compilation model. With WAPs, the pages' source code must be explicitly compiled into a single assembly before being deployed. During deployment, this assembly and the various markup pages are copied to the production environment.

When a request arrives to the web server for an ASP. NET page, the runtime creates an instance of the page's code-behind class and invokes its ProcessRequest method, which starts the page lifecycle and, ultimately, generates the page's content, which is returned to the requestor. The runtime can work with the ASP. NET page's code-behind class because the code-behind class was already compiled into an assembly prior to deployment.

With WSPs and automatic compilation, there is no explicit compilation step prior to deployment. Instead, deployment involves copying both the declarative and the source code content to the production environment. NET page for the first time since the page has been created or last updated, the runtime must first compile the code-behind class into an assembly.

Because the assembly is saved to disk, it does not need to be recompiled on subsequent requests to the same page. As you would expect, there is a slight delay when requesting a page for the first time or for the first time since it's been changed in a site that uses automatic compilation as it takes a moment for the server to compile the page's code and save the resulting assembly to disk. In short, with explicit compilation you are required to compile the website's source code before deployment, saving the runtime from having to perform that step.

With automatic compilation the runtime handles the compilation of the pages' source code, but with a slight initialization cost for the first visit to the page since it was created or last updated. But what about the declarative part of ASP. NET pages the. It's obvious that there's a relationship between the. It's also obvious that the content in the.

I don't want to get too sidetracked on the low-level implementation details, which vary between WAPs and WSPs, but in a nutshell the runtime automatically generates a class file that contains the various Web controls as protected members and methods.

This generated file is implemented as a partial class to the corresponding code-behind class. Partial classes allow for the contents of a single class to be spread across multiple files.

Therefore, the code-behind class is defined in two places: in the. NET Files folder. The important take away here is that for an ASP. NET page to be rendered by the runtime both its declarative and source code portions must be compiled into an assembly. With WAPs, the source code is explicitly compiled into an assembly prior to deployment, but the declarative markup must still be converted into code and compiled by the runtime on the web server.

With WSPs using automatic compilation, both the source code and the declarative markup need to be compiled by the web server. It is possible to use explicit compilation with the WSP model. You can explicitly compile the source code portion, like with the WAP model. What's more, you can also compile the declarative markup. This tool was released with the. NET Framework version 2. The compilation tool provides two general forms of compilation: in-place precompilation and precompilation for deployment.

The compilation tool then compiles each ASP. NET Files folder just like if the pages had each been visited for the first time from a browser. In-place precompilation can speed up the first request made to newly deployed ASP. NET pages on your site because it alleviates the runtime from needing to perform this step.



0コメント

  • 1000 / 1000