问题
(This is kind of a follow-up to my question "How to use C# 6 with Web Site project type?")
I'm trying to use C# 7 features with Visual Studio 2017.
For this, I've updated the Microsoft.Net.Compilers NuGet package to v2.0.0-rc4 in my existing ASP.NET WebForms 4.6.2 application.
It seems to work well from inside Visual Studio 2017.
When deployed to my website, all I see is a yellow screen of death:
Compiler error with error code 255.
[Detailed compiler output]
Version information: Microsoft .NET Framework version: 4.0.30319; ASP.NET version: 4.6.1590.0
The full compiler source is too long to be posted here (more than 30k chars), I've uploaded it to Pastebin.
My question:
Is there any chance to use the latest Roslyn compiler within an ASP.NET WebForms Web Application?
Update 1:
Of course, I've included the Microsoft.CodeDom.Providers.DotNetCompilerPlatform
NuGet package and web.config entries, 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=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
I've even tried to change the /langversion:6
parameter in above's XML to /langversion:7
with no different result.
Update 2:
Probably the question and answer would be similar for using C# 7 features from within ASP.NET MVC views inside .cshtml Razor files.
Update 3:
I've dug through the Event Log and found these (German system, sorry):
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[])
and:
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)
回答1:
Finally, I solved it.
With the help of Sysinternals Process Monitor I discovered that csc.exe tries to load a file called "csc.exe.config" which was not present.
So I copied the "csc.exe.config" file from my local website's "bin\roslyn" folder into my live website's "bin\roslyn" folder.
After that, everything works as expected.
Just to be safe, I also copied "csi.exe.config" the same way, although it seems that this is not required, at least in my tests.
(The reason that the config files were initially not present is that my publish scripts usually intentionally ignore configuration files to not overwrite existing configurations).
来源:https://stackoverflow.com/questions/42700380/how-to-use-c-sharp-7-within-web-application-aspx-code-before-pages