Uwe*_*eim 5 .net c# asp.net c#-7.0 visual-studio-2017
(这是我的问题“如何将 C# 6 与网站项目类型一起使用? ”的后续内容?)
我正在尝试在 Visual Studio 2017 中使用 C# 7 功能。
为此,我已将现有 ASP.NET WebForms 4.6.2 应用程序中的Microsoft.Net.CompilersNuGet 包更新为 v2.0.0-rc4。
它似乎在 Visual Studio 2017 中运行良好。
当部署到我的网站时,我看到的只是一个黄色的死亡屏幕:
编译器错误,错误代码为 255。
【详细编译输出】
版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.6.1590.0
完整的编译器源代码太长,无法在此处发布(超过 30k 个字符),我已将其上传到 Pastebin。
我的问题:
是否有机会在 ASP.NET WebForms Web 应用程序中使用最新的 Roslyn 编译器?
更新 1:
当然,我已经包含了Microsoft.CodeDom.Providers.DotNetCompilerPlatformNuGet 包和 web.config 条目,如此处所述。
<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=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
Run Code Online (Sandbox Code Playgroud)
我什至尝试将/langversion:6上述 XML 中的参数更改为/langversion:7没有不同的结果。
更新 2:
对于在 .cshtml Razor 文件中的 ASP.NET MVC 视图中使用 C# 7 功能,问题和答案可能类似。
更新 3:
我翻阅了事件日志,发现了这些(德国系统,抱歉):
Run Code Online (Sandbox Code Playgroud)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[])
和:
Run Code Online (Sandbox Code Playgroud)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)
最后我解决了。
在Sysinternals Process Monitor的帮助下,我发现 csc.exe 尝试加载一个不存在的名为“ csc.exe.config ”的文件。
因此,我将“csc.exe.config”文件从本地网站的“bin\roslyn”文件夹复制到我的实时网站的“bin\roslyn”文件夹中。
之后,一切都按预期进行。
为了安全起见,我也以同样的方式复制了“csi.exe.config”,尽管这似乎不是必需的,至少在我的测试中是这样。
(配置文件最初不存在的原因是我的发布脚本通常故意忽略配置文件,以免覆盖现有配置)。