在VS 2010中构建网站项目时,我遇到以下异常:
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..
我该如何调试?当我使用VS.NET和命令行构建时(例如,通过NAnt / MSBuild),就会发生这种情况。
答案 0 :(得分:23)
要解决此问题,您需要向aspnetcompiler提供-errorstack
参数。然后当它失败时,你不仅会得到你现在看到的异常的堆栈跟踪,而且还会得到它包装的InnerException。例如,这是构建错误是由未加载Cassette dll的问题引起的输出:
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)
答案 1 :(得分:5)
我想对Xoltar的答案进行投票,但这是我对SO的第一次贡献,所以我没有代表!
我的构建服务器(TeamCity)上有一个非常类似的问题,这是构建日志中的一个剪辑:
[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
要诊断问题,我运行了 ASPNET_COMPILER ,使用 -errorstack 参数来对抗 MSBUILD 为网站生成的内容。在这种情况下:
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..
在我的情况下,依赖于其中一个Azure程序集(未在构建服务器上安装)是问题的根源。
答案 2 :(得分:2)
增加msbuild任务或命令行的详细级别以获取更详细的错误消息。
例如,使用/verbosity:detailed
。
请参阅http://msdn.microsoft.com/en-us/library/ms164311.aspx
你在使用Ninject吗?
如果是,请参阅:http://groups.google.com/group/ninject/browse_thread/thread/1934e9260ba07ecd?pli=1和此:http://www.techques.com/question/1-6173194/Strange-behaviour-when-using-Ninject-to-resolve-a-service-within-a-controller-factory
您使用的是RavenDB吗? 如果是这样,请参阅:http://groups.google.com/group/ravendb/browse_thread/thread/437738caf4760ce1/b7e92fd6cfeaaa17?#b7e92fd6cfeaaa17
答案 3 :(得分:1)
就我而言,我在TeamCity中看到了[AspNetCompiler] error ASPRUNTIME error message: Value cannot be null.
消息。
在完整的构建日志中,底部更有用的消息是:
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.
事实证明,我使用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
}
}
}
我的情况中的原因是我的预启动方法是从注册表加载设置,并且它们在构建服务器上不存在(但在我的机器上执行,所以它在我构建它时起作用)。我不知道为什么webdeployment项目会运行此代码。
我将整个事情包装在try..catch中,记录了一条致命的消息(在我的应用程序中,它会转到日志文件和Windows事件日志中)。在构建中,我并不关心它是否失败,因为它不需要做任何事情。在实际安装中,如果缺少设置,应用程序将无法工作(因此日志解释了原因)。
现在,对OP而言,问题似乎是在web激活器中。如果在App_Start文件夹中有一个使用webactivor的方法抛出错误,我会不会感到惊讶,这使得它看起来像来自WebActivator。首先检查那里,如果需要,在try..catch中包装东西。 webactivator本身也可能存在错误。
因为我在研究问题时遇到过这篇文章,希望这些信息至少对其他人有用。
TL; DR:Web部署项目在编译时运行ApplicationPreStart方法。确保您在启动时运行的任何代码周围都有try..catch块,或者可以处理在IIS /托管环境之外的构建服务器上编译期间执行代码的情况。