【问题标题】:How to use C# 7 within Web Application ASPX code-before pages?如何在 Web 应用程序 ASPX 代码前页面中使用 C# 7?
【发布时间】:2017-07-30 17:39:06
【问题描述】:

(这是对我的问题“How to use C# 6 with Web Site project type?”的跟进)

我正在尝试在 Visual Studio 2017 中使用 C# 7 功能。

为此,我在现有的 ASP.NET WebForms 4.6.2 应用程序中更新了 Microsoft.Net.Compilers NuGet package to v2.0.0-rc4

它似乎在 Visual Studio 2017 中运行良好。

当部署到我的网站时,我看到的只是一个黄屏死机:

编译器错误,错误代码为 255。

[详细的编译器输出]

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1590.0

完整的编译器源代码太长,无法在此处发布(超过 30k 个字符)I've uploaded it to Pastebin

我的问题:

是否有机会在 ASP.NET WebForms Web 应用程序中使用最新的 Roslyn 编译器?

更新 1:

当然,我已经包含了Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet 包和 web.config 条目,as described here

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>

我什至尝试将上述 XML 中的 /langversion:6 参数更改为 /langversion:7,但没有不同的结果。

更新 2:

对于在 .cshtml Razor 文件中的 ASP.NET MVC 视图中使用 C# 7 功能,问题和答案可能类似。

更新 3:

我翻阅了事件日志并找到了这些(德国系统,抱歉):

Anwendung: csc.exe    
Frameworkversion: v4.0.30319    
Beschreibung: Der Prozess wurde aufgrund einer unbehandelten Ausnahme beendet.    
Ausnahmeinformationen: System.IO.FileLoadException    
   bei Microsoft.CodeAnalysis.CommandLine.BuildClient.GetCommandLineWindows(System.Collections.Generic.IEnumerable'1<System.String>)    
   bei Microsoft.CodeAnalysis.CommandLine.DesktopBuildClient.Run(System.Collections.Generic.IEnumerable'1<System.String>, System.Collections.Generic.IEnumerable'1<System.String>, Microsoft.CodeAnalysis.CommandLine.RequestLanguage, Microsoft.CodeAnalysis.CommandLine.CompileFunc, Microsoft.CodeAnalysis.IAnalyzerAssemblyLoader)    
   bei Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[], System.String[])    
   bei Microsoft.CodeAnalysis.CSharp.CommandLine.Program.Main(System.String[])    

和:

Exception information:     
   Exception type: HttpCompileException     
   Exception message: Eine externe Komponente hat eine Ausnahme ausgelöst.    
  bei System.Web.Compilation.AssemblyBuilder.Compile()    
  bei System.Web.Compilation.BuildProvidersCompiler.PerformBuild()    
  bei System.Web.Compilation.CodeDirectoryCompiler.GetCodeDirectoryAssembly(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories, Boolean isDirectoryAllowed)    
  bei System.Web.Compilation.BuildManager.CompileCodeDirectory(VirtualPath virtualDir, CodeDirectoryType dirType, String assemblyName, StringSet excludedSubdirectories)    
  bei System.Web.Compilation.BuildManager.CompileResourcesDirectory()    
  bei System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()    
  bei System.Web.Compilation.BuildManager.CallAppInitializeMethod()    
  bei System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException)    

【问题讨论】:

  • 错误代码 255 是一个常见错误,here 是一些建议
  • @UweKeim 啊,这个更新很有帮助。
  • 如果您想使用 C# 7 功能,可能是时候考虑从 WebForms 迁移到 MVC 6 了。什么时候 C#7 的好处大于尝试使用 WebForms 进行这项工作的维护开销?你的努力可能最好花在切换堆栈上。
  • @UweKeim 完全可以理解 :)。我没有在任何 Razor 文档中看到对 C#7 的任何明确支持,我怀疑对 aspx 页面的支持会更少。
  • 能否查看packages\Microsoft.Net.Compilers.2.0.1\tools 下的文件是否全部复制到WebApplication\bin\roslyn 文件夹(包括csc.exe.config)?跨度>

标签: c# asp.net .net visual-studio-2017 c#-7.0


【解决方案1】:

我遇到了类似的问题,因为网站上有 targetFramework="4.5.2"。 我将它更新到 4.8,重新安装了 Microsoft.CodeDom.Providers.DotNetCompilerPlatform,它现在可以工作了。

【讨论】:

    【解决方案2】:

    终于解决了。

    Sysinternals Process Monitor 的帮助下,我发现 csc.exe 尝试加载一个名为“csc.exe.config”的文件,该文件不存在。

    因此,我将本地网站的“bin\roslyn”文件夹中的“csc.exe.config”文件复制到了实时网站的“bin\roslyn”文件夹中。

    之后,一切都按预期进行。

    为了安全起见,我也以同样的方式复制了“csi.exe.config”,尽管这似乎不是必需的,至少在我的测试中是这样。

    (配置文件最初不存在的原因是我的发布脚本通常故意忽略配置文件以不覆盖现有配置)。

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2014-06-26
      • 2018-05-17
      • 1970-01-01
      • 2012-09-26
      相关资源
      最近更新 更多