I\'m getting the following exception when building a website project in VS 2010:
The pre-application start initialization method Run on type WebActi
To solve this, you need to supply the -errorstack
argument to the aspnetcompiler. Then when it fails, you'll get not only the stack trace of the exception you're seeing now, but also the InnerException that it's wrapping. For instance, here's the output when the build error is caused by a problem with a Cassette dll not being loaded:
error ASPRUNTIME: The pre-application start initialization method Run on type We
bActivator.ActivationManager threw an exception with the following error message
: Exception has been thrown by the target of an invocation..
[TypeLoadException]: Could not load type 'Cassette.Configuration.ICassetteConfig
uration' from assembly 'Cassette, Version=0.8.1.0, Culture=neutral, PublicKeyTok
en=null'.
at Cassette.Web.StartUp..cctor()
[TypeInitializationException]: The type initializer for 'Cassette.Web.StartUp' t
hrew an exception.
at Cassette.Web.StartUp.PreApplicationStart()
[TargetInvocationException]: Exception has been thrown by the target of an invoc
ation.
at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Ob
ject target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAt
tributes, RuntimeType typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Obj
ect target, Object[] arguments, Signature sig, MethodAttributes methodAttributes
, RuntimeType typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisib
ilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at WebActivator.BaseActivationMethodAttribute.InvokeMethod()
at WebActivator.ActivationManager.RunActivationMethods[T]()
at WebActivator.ActivationManager.RunPreStartMethods()
at WebActivator.ActivationManager.Run()
[InvalidOperationException]: The pre-application start initialization method Run
on type WebActivator.ActivationManager threw an exception with the following er
ror message: Exception has been thrown by the target of an invocation..
at System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`
1 methods)
at System.Web.Compilation.BuildManager.CallPreStartInitMethods()
at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appMan
ager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, Host
ingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception a
ppDomainCreationException)
[HttpException]: The pre-application start initialization method Run on type Web
Activator.ActivationManager threw an exception with the following error message:
Exception has been thrown by the target of an invocation..
at System.Web.Compilation.ClientBuildManager.EnsureHostCreated()
at System.Web.Compilation.ClientBuildManager.PrecompileApplication(ClientBuil
dManagerCallback callback, Boolean forceCleanBuild)
at System.Web.Compilation.Precompiler.Main(String[] args)
Increase the verbosity level of the msbuild task or command line to get more detailed error messages.
For example, use /verbosity:detailed
.
See http://msdn.microsoft.com/en-us/library/ms164311.aspx
Are you using Ninject?
If so, see this: http://groups.google.com/group/ninject/browse_thread/thread/1934e9260ba07ecd?pli=1 and this: http://www.techques.com/question/1-6173194/Strange-behaviour-when-using-Ninject-to-resolve-a-service-within-a-controller-factory
Are you using RavenDB?
If so, see this: http://groups.google.com/group/ravendb/browse_thread/thread/437738caf4760ce1/b7e92fd6cfeaaa17?#b7e92fd6cfeaaa17
I'd like to up-vote Xoltar's answer, but this is my first contribution to SO so I don't have the rep!
I had a very similar problem on our build-server (TeamCity), here's a clip from the build-log:
[00:10:39]: (MvcBuildViews target) ->
[00:10:39]: error ASPRUNTIME : The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation.. [C:\TeamCity\buildAgent\work\1ee8c94c32e676ad\source\Project.Site\Project.Site.csproj]
[00:10:39]: 0 Warning(s)
[00:10:39]: 1 Error(s)
[00:10:39]: Time Elapsed 00:00:27.93
To diagnose the problem I ran ASPNET_COMPILER, using the -errorstack argument against what MSBUILD had generated for site. In this case:
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_compiler -p "C:\TeamCity\buildAgent\work\1ee8c94c32e676ad\source\Project.Site" -v anything -errorstack
Utility to precompile an ASP.NET application
Copyright (C) Microsoft Corporation. All rights reserved.
error ASPRUNTIME: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message : Exception has been thrown by the target of an invocation..
[FileNotFoundException]: Could not load file or assembly 'msshrtmi, Version=1.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeEnvironment()
at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor()
error ASPRUNTIME: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message : Exception has been thrown by the target of an invocation..
In my case a dependency on one of the Azure assemblies (which wasn't installed on the build server) was the root of the problem.
In my case, I was seeing the message [AspNetCompiler] error ASPRUNTIME error message: Value cannot be null.
in TeamCity.
In the full build logs, the more useful message at the bottom was:
error ASPRUNTIME : The pre-application start initialization method InitializePreStart on type MyApplication.MvcApplication threw an exception with the following error message: Value cannot be null.
As it turns out I had an exception being thrown by a method I had defined using PreApplicationStartMethod:
[assembly: System.Web.PreApplicationStartMethod(typeof(MyApp.MvcApplication), "InitializePreStart")]
namespace PrintFleet.PFE.RestService
{
public class MvcApplication : System.Web.HttpApplication
{
//....
public static void InitializePreStart()
{
// exception thrown here
}
}
}
The cause in my case was my prestart method was loading settings from the registry, and they didn't exist on the build server (but did on my machine, so it worked when I built it). I have no clue why webdeployment projects run this code.
I wrapped the entire thing in a try..catch that logs a fatal message (which in my app, goes to both the log file and windows event log). In builds, I don't really care if it fails since it doesn't need to do anything. In actual installations, if the settings are missing the app won't work (and so the log explains why).
Now, to the OP the problem seems to be in web activator. I would not be surprised if there is a method using webactivor in App_Start folder that is throwing the error, which is making it look like it's coming from WebActivator. Check there first, wrap things in try..catch if needed. It's also possible there is a bug in webactivator itself.
So because I came across this post while researching the problem, hopefully this information is useful to at least someone else.
TL;DR: Web deployment projects run the ApplicationPreStart methods when they compile. Ensure that you have try..catch blocks around any code run at prestart time or can otherwise handle the situation where the code is executed during compile time on your build server(s), outside of the IIS/hosting environment.