Avoid aspnet_compiler running PreApplicationStart method
Asked Answered
O

2

24

So, one of my websites has a PreApplicationStartMethod that should run before the application starts:

[assembly: PreApplicationStartMethod(typeof(ServiceStackAppHost), "Start")]

This methods does some bootstrapping and relies on some configuration being set.

Now, I want to compile the website as part of my automated build process - so I invoke aspnet_compiler.exe, which fails because it runs the PreApplicationStartMethod:

AfterBuild:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe -v temp -p C:\Projects\ error ASPRUNTIME : The pre-application start initialization method Start on type RedactedNameSpace.ServiceStackAppHost threw an exception with the following error message: Resolution of the dependency failed, type = "..."

How do I avoid aspnet_compiler.exe invoking the PreApplicationStartMethod when compiling the website ?

Ophthalmitis answered 30/11, 2012 at 9:51 Comment(4)
What is the point of bypassing this method ? If the configuration being set on "PreApplicationStart" cannot be set on start up while invoking from aspnet_compiler.exe, how will you be able to perform other operations ???Viral
aspnet_compiler should compile the website, not run it. I want to be able to verify that there are no compile-time errors, before moving the application to an environment where it can actually run.Ophthalmitis
You project must be containing Global.asax, right ?Viral
One blog post about PreApplicationStart specifically mentions that it gets called "way early", before compilation has started. Admittedly, that one isn't about precompiling. Do you need to use PreApplicationStart, or would it be acceptable to move the code you have in there somewhere else?Moncton
B
32

The short answer is that you can't prevent it from running, and that this is by design but you can workaround the problem.

Why PreApplicationStart methods run during aspnet_compiler.exe

The reason is that PreApplicationStartMethod (or WebActivator, which builds on top of it) can be used for things that actually affect compilation, such that if you omitted it the site may not compile.

To give you an example, you can add namespaces to the compilation in a PreAppStart method, which then affects compilation of your Razor pages.

Obviously, not every PreAppStart method needs to run when you use aspnet_precompiler, but we do run all of them in case they are needed.

Detecting whether you're running under aspnet_compiler.exe

If the code in there breaks under aspnet_compiler, it may be necessary to add conditional logic in the PreAppStart method to detect the situation and omit running the code.

You can look at System.Web.Hosting.HostingEnvironment.InClientBuildManager propery to determine whether your PreAppStart method is running under the context of aspnet_compiler.exe (where it will be true), or at runtime (where it will be false). InClientBuildManager also applies to building a web site within VS, which uses basically the same code path as aspnet_compiler.exe.

Try using PostStart methods instead

Note that WebActivator also supports PostApplicationStartMethod, and those will not run under aspnet_compiler. That code runs after Application_Start, so it may or may not be appropriate to your scenario. But it may be a solution for you.

aspnet_compiler.exe Debugging tip

Not directly related but useful for debugging: you can pass -errorstack to aspnet_compiler.exe to get a more complete stack when there is an error.

Banerjee answered 3/12, 2012 at 18:41 Comment(4)
Just added info about using WebActivator PostApplicationStartMethod instead.Banerjee
Is there a way to actually detect that you're running in compilation mode rather than as a live website?Harri
@BennorMcCarthy I just added info on detecting that using InClientBuildManagerBanerjee
Just noting that this happened to me because of IoC initialization in PreStart because I have loose references that are resolved via reflection. Since the MSBuild/MSDeploy targets I use to copy those references happen after build I was getting this error while trying to publish to IIS instances. I had to track through a couple other steps narrowing this down before I got here and hope this mention of web deploy publishing tightens up the google results ;PFlannery
V
0

PreApplicationStartMethod is an assembly attribute for loading code extremely early in the build process. The compiler has to load and run this code in order to begin compilation process. PreApplicationStartMethod is similar to building process not the Initializing process. So you can try putting you code into Application_Start method in global.asax !!

Viral answered 3/12, 2012 at 13:40 Comment(4)
"The compiler has to load and run this code in order to begin compilation process." That is just plain wrong.Ophthalmitis
you can try the solution though !Viral
Yes, and putting the code in Application_Start works. The question is whether it can work with PreApplicationStart, though.Ophthalmitis
Agree ! Then my answer would be "NO". this can not be by passed. If you are getting an error, you can avoid it. But PreApplicationStartMethod will run anyway.Viral

© 2022 - 2024 — McMap. All rights reserved.