c# - 避免 aspnet_compiler 运行 PreApplicationStart 方法

标签 c# asp.net .net aspnet-compiler

所以,我的一个网站有一个 PreApplicationStartMethod,它应该在应用程序启动之前运行:

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

此方法进行一些引导并依赖于设置的一些配置。

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

如何避免aspnet_compiler.exe在编译网站时调用PreApplicationStartMethod?

最佳答案

简短的回答是您无法阻止它运行,这是设计使然但是您可以解决该问题。

为什么 PreApplicationStart 方法在 aspnet_compiler.exe 期间运行

原因是 PreApplicationStartMethod(或构建在它之上的 WebActivator)可用于实际影响编译的事情,如果您省略它,站点可能无法编译。

举个例子,您可以在 PreAppStart 方法中将命名空间添加到编译中,然后影响 Razor 页面的编译。

显然,当您使用 aspnet_precompiler 时,并不是每个 PreAppStart 方法都需要运行,但我们会运行所有这些方法以备不时之需。

检测是否运行在aspnet_compiler.exe下

如果其中的代码在 aspnet_compiler 下中断,可能需要在 PreAppStart 方法中添加条件逻辑以检测情况并省略运行代码。

您可以查看 System.Web.Hosting.HostingEnvironment.InClientBuildManager 属性以确定您的 PreAppStart 方法是否在 aspnet_compiler.exe 的上下文中运行(它将在其中运行为 true),或在运行时(它将为 false)。 InClientBuildManager也适用于在VS中建站,其使用的代码路径与aspnet_compiler.exe基本相同。

尝试改用 PostStart 方法

请注意,WebActivator 还支持 PostApplicationStartMethod,但这些不会在 aspnet_compiler 下运行。该代码在 Application_Start之后 运行,因此它可能适合也可能不适合您的场景。但它可能是适合您的解决方案。

aspnet_compiler.exe 调试提示

没有直接关系但对调试有用:您可以将 -errorstack 传递给 aspnet_compiler.exe,以便在出现错误时获得更完整的堆栈。

关于c# - 避免 aspnet_compiler 运行 PreApplicationStart 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13642691/

相关文章:

c# - 在服务中注入(inject)模拟的 dbContext - 集成测试

c# - 在控制台应用程序 c# 中使用时间小时执行查询

c# - 在 .NET 中使用属性的性能开销

c# - 我应该如何强制加载引用的程序集?

.net - 如何在没有大选择/开关的情况下返回特定类类型

c# - 从哪儿开始? .net 应用程序的美学更改(无源代码)

c# - 有没有一种灵活的方式来部署我的 silverlight 应用程序并以编程方式更改设置?

c# - 如何使用 C# 从远程位置将 Excel csv 或 xls 文件的行读取到 ASP.NET 应用程序中?

c# - 该方法或操作未实现。在 C# 中停止 IIS 网站时

c# - 字符串长度不能为零。参数名称 : oldValue