What effect does the new precompile during publishing option have on MVC4 applications?
Asked Answered
L

2

93

So I recently updated Visual Studio 2012 to Update 2. Lo and behold, the next time I go to publish my application (via File Publish in this case) I notice that there are three new options:

  1. Delete all existing files prior to publish
  2. Precompile during publishing (with a link to Configure)
  3. Exclude files from the App_Data folder

The first and third options are pretty self-explanatory, but I can't find any documentation on the second option as it applies to MVC. When I check it, there doesn't seem to be any change in the files produced on the site and I don't see any real change in performance.

Laos answered 23/4, 2013 at 21:30 Comment(1)
The location of the function may be new, but the function itself is not. Even if it didn't regard compiling MVC, WebForms are interoperable in an MVC project.Noaccount
D
91

Using the ASP.NET precompiler can have the following impact on your MVC app:

  • If you have anything in App_Code, it will be precompiled into a DLL before deployment. Without precompiling, this would happen on the fly by the ASP.NET runtime.
  • If you choose the option to not make your pages updateable (i.e. uncheck the first checkbox in the advanced settings dialog), it will also precompile your views (ASPX and Razor) instead of compiling those dynamically at runtime as well. The default (checked) setting of "Allow precompiled site to be updateable" allows you to update your view content without needing to rebuild the entire project.

If you don't have any files in App_Code and you want your site to remain updateable, it doesn't seem to do much.

Durward answered 23/4, 2013 at 22:46 Comment(9)
so I don't have any files in App_Code, but even with the checkbox unchecked my views all show as .cshtml files. I did notice that when I had it checked, it found a partial view I had written awhile ago that had syntax errors so I can see that it at least touches those views. In theory I guess that should make some performance difference, I just don't see it because my views are relatively small.Laos
@Laos Your .cshtml files will still exist, but if their contents are precompiled, you'll find their contents replaced with "This is a marker file generated by the precompilation tool, and should not be deleted!".Durward
Also, the perf gains are mostly on the initial site launch, when the compiler is invoked dynamically for each page. Once the page has been hit once (and compiled), that compilation result will be used on subsequent page loads even if you haven't precompiled.Durward
An added benefit of NOT having your pages updateable is that VS will find type errors in your views before they make it to your live site.Tymbal
@Tymbal you should reconsider your testing strategy if such errors would go uncaught without the precompile option.Dalliance
It also compiles your global resources and global.asax into the Bin folder. (Anybody know why would one want that to happen..?)Ileus
@AndersLindén: Just test the actual rendered response of your controller actions. That will invoke Razor and if it generates an exception, that will fail your test. However, this is also why your views should be very light on code. You can't really do fine-grained unit testing on view code like you can on a class. It's pretty much just either pass or fail, and even then, passing just means it didn't generate any exceptions, not that it actually did what it was supposed to.Langer
@Stijn You shouldn't bother testing before you even compiled.Glass
I had a MasterPage per each theme and the theme engine select the appropriate one during runtime. Checking precompile option caused the compiler thinks that the are multiple definitions for a class.Spacious
T
1

It is an old question, but I just encounter similar issue and feel something worth sharing.

My error message is same in this post. My project is MVC5, build with Visual Studio 2013 professional. Compilation Error: The type 'ASP.global_asax' exists in both DLLs

In my case, with precompile option, there is a file, App_global.asax.dll, in bin folder, and cause above error message. First, I remove App_global.asax.dll on server, restart application pool, issue is gone. Then I tried another approach, uncheck precompile and republish, redeploy to server, issue is gone.

Topmast answered 18/9, 2020 at 14:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.