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 ?

driis
  • 161,458
  • 45
  • 265
  • 341
  • 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 ??? – jparthj Dec 03 '12 at 12:58
  • 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. – driis Dec 03 '12 at 13:04
  • You project must be containing Global.asax, right ? – jparthj Dec 03 '12 at 13:22
  • One [blog post about PreApplicationStart](http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx) specifically mentions that it gets called "*way* early", before compilation has started. Admittedly, that one isn't about *pre*compiling. Do you need to use `PreApplicationStart`, or would it be acceptable to move the code you have in there somewhere else? –  Dec 03 '12 at 13:29

2 Answers2

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.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • Just added info about using WebActivator `PostApplicationStartMethod` instead. – David Ebbo Dec 03 '12 at 19:01
  • 2
    Is there a way to actually detect that you're running in compilation mode rather than as a live website? – Bennor McCarthy Dec 04 '12 at 00:44
  • @BennorMcCarthy I just added info on detecting that using `InClientBuildManager` – David Ebbo Dec 04 '12 at 18:09
  • 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 ;P – TheXenocide Nov 25 '14 at 15:27
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 !!

jparthj
  • 1,606
  • 3
  • 20
  • 44
  • "The compiler has to load and run this code in order to begin compilation process." That is just plain wrong. – driis Dec 03 '12 at 13:43
  • you can try the solution though ! – jparthj Dec 03 '12 at 13:43
  • Yes, and putting the code in Application_Start works. The question is whether it can work with PreApplicationStart, though. – driis Dec 03 '12 at 13:49
  • 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. – jparthj Dec 03 '12 at 13:52