5

(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=\&quot;Web\&quot; /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)    
Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    The error code 255 is a popular error, [here](http://stackoverflow.com/questions/33989211/another-strange-asp-net-error-the-compiler-failed-with-error-code-255) are a couple suggestions – maccettura Mar 09 '17 at 16:24
  • 1
    @UweKeim ahh, that update helps quite a bit. – David L Mar 09 '17 at 16:26
  • 1
    It may be time for you to consider moving from WebForms to MVC 6 if you want to use C# 7 features. At what point is the benefit from C#7 larger than the maintenance overhead of trying to make this work with WebForms? Your efforts would probably be better spent switching stacks. – David L Mar 09 '17 at 16:38
  • Thanks, @DavidL actually most of our apps are already MVC . I just cannot justify the efforts and costs to move this _internal_ application to MVC. I'm rather sure that the answer to this question will also help to solve the same thing (C# 7 in MVC Razor views) for MVC. – Uwe Keim Mar 09 '17 at 16:41
  • 1
    @UweKeim completely understandable :). I haven't seen any explicit support for C#7 called out in any Razor documentation and I would suspect that there would be even less support for aspx pages. – David L Mar 09 '17 at 16:46
  • 2
    Can you check if all the files under packages\Microsoft.Net.Compilers.2.0.1\tools are copied to the WebApplication\bin\roslyn folder(including csc.exe.config)? – mattfei Mar 10 '17 at 06:28
  • @mattfei I was on **2.0.0-rc4** just to discover that **2.0.1** seems to have been released after I posted my question here. Will try to update and see the results. – Uwe Keim Mar 10 '17 at 06:37

2 Answers2

3

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).

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
1

I had a similar issue because the Website had targetFramework="4.5.2". I updated it to 4.8, reinstalled Microsoft.CodeDom.Providers.DotNetCompilerPlatform and it's working now.