简体   繁体   English

避免运行PreApplicationStart方法的aspnet_compiler

[英]Avoid aspnet_compiler running PreApplicationStart method

So, one of my websites has a PreApplicationStartMethod that should run before the application starts: 因此,我的一个网站有一个应该在应用程序启动之前运行的PreApplicationStartMethod:

[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: 现在,我想编译网站作为我的自动构建过程的一部分 - 所以我调用aspnet_compiler.exe,因为它运行PreApplicationStartMethod而失败:

AfterBuild: 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 = "..." C:\\ Windows \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\ aspnet_compiler.exe -v temp -p C:\\ Projects \\ error ASPRUNTIME:预应用程序启动初始化方法Start on type RedactedNameSpace.ServiceStackAppHost引发了以下异常错误消息:依赖项的解析失败,type =“...”

How do I avoid aspnet_compiler.exe invoking the PreApplicationStartMethod when compiling the website ? 在编译网站时,如何避免aspnet_compiler.exe调用PreApplicationStartMethod?

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 为什么在aspnet_compiler.exe期间运行PreApplicationStart方法

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. 原因是PreApplicationStartMethod (或构建在它之上的WebActivator )可以用于实际影响编译的事情,这样如果你省略了它,那么网站可能无法编译。

To give you an example, you can add namespaces to the compilation in a PreAppStart method, which then affects compilation of your Razor pages. 举个例子,您可以在PreAppStart方法中为编译添加名称空间,然后影响Razor页面的编译。

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. 显然,并不是每个PreAppStart方法都需要在使用aspnet_precompiler时运行,但我们会在需要时运行所有这些方法。

Detecting whether you're running under aspnet_compiler.exe 检测您是否在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. 如果aspnet_compiler中的代码中断,则可能需要在PreAppStart方法中添加条件逻辑以检测情况并省略运行代码。

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 ). 您可以查看System.Web.Hosting.HostingEnvironment.InClientBuildManager propery,以确定您的PreAppStart方法是在aspnet_compiler.exe的上下文中运行(它将是true ),还是在运行时(它将为false )。 InClientBuildManager also applies to building a web site within VS, which uses basically the same code path as aspnet_compiler.exe. InClientBuildManager也适用于在VS中构建网站,该网站使用与aspnet_compiler.exe基本相同的代码路径。

Try using PostStart methods instead 请尝试使用PostStart方法

Note that WebActivator also supports PostApplicationStartMethod , and those will not run under aspnet_compiler. 请注意,WebActivator还支持PostApplicationStartMethod ,并且这些不会在aspnet_compiler下运行。 That code runs after Application_Start , so it may or may not be appropriate to your scenario. 该代码 Application_Start 之后运行,因此它可能适合您的方案,也可能不适合您的方案。 But it may be a solution for you. 但它可能是你的解决方案。

aspnet_compiler.exe Debugging tip aspnet_compiler.exe调试提示

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. 不直接相关但对调试很有用:您可以将-errorstack传递给aspnet_compiler.exe,以便在出现错误时获得更完整的堆栈。

PreApplicationStartMethod is an assembly attribute for loading code extremely early in the build process. PreApplicationStartMethod是一个程序集属性,用于在构建过程中尽早加载代码。 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. PreApplicationStartMethod类似于构建过程而不是初始化过程。 So you can try putting you code into Application_Start method in global.asax !! 所以你可以尝试将代码放入global.asax中的Application_Start方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM