When there are too many pages in a project, IIS starts up and the website is very slow when you open it for the first time, because the project is not pre-compiled at the time of release, but is dynamically compiled when the user visits the web page. If you want to improve the performance of your existing site and perform error checking on your site, it is necessary to select "Precompile during release" when you publish your project.
introduction
For small projects, publishing according to the default settings can basically meet normal operation, the first page is opened in 56 seconds (depending on the server configuration), and the first opening of other pages is basically completed in 12 seconds, not the first instantaneous opening.
Once the project functions become complex and the number of files increases, it will take more than 30 seconds to open the first page for the first run after publication, and about 10 seconds for the first opening of other pages, not the first instantaneous opening.
This is because the project is not pre-compiled at the time of release, but is dynamically compiled when the user accesses the web page, and once the application pool is recycled, or the project files are changed, it will be recompiled and go through a slow "first time" again, which is intolerable.
Benefits of precompilation
- Performance. Compiled code is executed much faster than scripting languages such as ECMAScript or VBScript because it is a representation closer to machine code and does not require additional analysis.
- Security. Compiled code is more difficult to reverse engineer than uncompiled source code because it lacks the readability and abstraction that high-level languages have. Additionally, fuzzing tools enhance the compiled code's ability to resist reverse engineering processing.
- Stability. Check your code for syntax errors, type safety issues, and other issues at compile time. By catching these errors at build time, many errors can be eliminated in the code.
- Interoperability. Since MSIL code supports any .NET language, it is possible to use assemblies originally written in other languages in the code. For example, if you are writing ASP.NET web page in C#, you can add a reference to a .dll file written in Visual Basic.
ASP.NET Core precompiled
Precompiled
Precompilation is the default way for ASP .Net Core. At publishing time, all Razor views in the system are precompiled by default. The compiled view DLL is uniformly named xxx.PrecompiledViews.dll or xxx.Views.dll
Dynamic compilation
It is easy to configure the entire project to dynamic compilation, just add a configuration project MvcRazorCompileOnPublish with a value of false
ASP.NET Pre-compilation of the website
We use Visual Studio to publish a website in the following ways:
The meaning of the "Allow updates to this precompiled site" option When we publish a .Net web project, in general, all . CS file, which will automatically generate a DLL dynamic link library, which can protect the source code of the website very well, because the server-side code is generally placed in . Since the DLL files in the CS file are all generated, then upload them to the server, others cannot easily open them!
However, other files, such as ashx, aspx and other files, what is in it, is what it is, others can open these files to view, although others cannot see the CS code, but can still see the HTML code or some server controls and related attributes in the ASPX file; A file like ashx is equivalent to a CS file, and the code in it can be easily seen;
Therefore, . CS files are safe, but ASPX, ashx and other files are not safe; So, is there a way to make web files uploaded to the server safe? There is a way, that is, when publishing, do not check "Allow updates to this precompiled site";
Check Allow updates to this precompiled site
If you check "Allow to update this precompiled site" when publishing the web, then the result is like this: The entire website file, except for all CS files compiled into DLL files, other files, and the original one has no changes, what's inside, or what, as long as others open it through Notepad, the code, HTML code, etc. inside can be seen by others at a glance.
In addition, when users first visit a certain page, they need to be compiled to find bugs, and then if there are no errors, they can be accessed normally, so the speed will become relatively slow. Visits after that are normal;
Uncheck "Allow updates to this precompiled site"
If you do not check "Allow to update this precompiled site" when publishing the web, then the result is as follows: 1. All CS files in the website are compiled into DLL files; 2. In addition to the cs file, other files, such as ASPX, ASHX and other files, are also compiled together, and each file generates a corresponding *.compiled file in the BIN directory;
After that, if you view ASPX, ASHX and other files through the notepad, you will not see any code in them, even the HTML code markup is not visible, open such a file, there is only one line of text in it, the content is "This is a markup file generated by the precompiled tool, it should not be deleted!", and the size of these files is 1kb;
If you try to open a website page, you will find that except for the first page after the project starts, which still takes 1~2 seconds (no EF), the first time of each other page opens instantaneously (EF's first slowness is beyond the scope of this article). This makes me feel like I'm late to see precompiled!
Here I secretly tell you that deleting the Views directory will not affect the normal opening of the web page~ Why don't you let it be deleted, we don't dare to ask, and we don't dare to delete it.
The goal was achieved, and there were some aftereffects that needed to be solved, such as the clutter in the bin directory.
Select "Do not merge." Create separate assemblies for each page and control", and the result is a lot more App_Web_*.dll files in bin.
At the time of release, the project root generates a PrecompiledApp.config file. The content is as follows:
The PrecompiledApp.config file is used to track how the application is deployed and whether ASP.NET needs to compile any files at the time of request. |