.NET Compiler Roslyn
Table of Contents
Introduction
You might have seen something like the following: your ASP.NET MVC solution builds fine but you get a compiler error when deploying to a local IIS site or a cloud web server. You know you have C# 6 or higher in your application, but you're not sure why it causes a compile error during runtime. I had to spend some time figuring out what was happening in my case, so I've documented what I found here to hopefully help someone else.
Errors
Microsoft (R) Visual C# Compiler version 4.7.2556.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
warning CS2008: No source files specified
error CS1541: Invalid reference option: 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET
Files\root\442dd867\' -- cannot reference directories
error CS1562: Outputs without source must have the /out option specified
Compilers
It is confusing, but .NET development has several different .NET compilers. The compiler is csc.exe, but there are different versions:
-
.NET Framework's
csc.exe- For example, .NET 4.6 is located here:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
- For example, .NET 4.6 is located here:
-
Visual Studio's
csc.exe-
Visual Studio 2013 is located here:
C:\Program Files (x86)\MSBuild\12.0\Bin\csc.exe -
Visual Studio 2015 is located here:
C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe
-
-
Roslyn's
csc.exe(example below uses version 1.0.0)- Roslyn inside application bin folder, for example:
/bin/roslyn/csc.exe
- Roslyn inside application bin folder, for example:
So there are multiple compilers in different locations, and unfortunately, the versions of csc.exe are different. If they were all the same version, I don't think I would see the error of compiling C# 6 code using a C# 5 compiler.
In summary, the following are the different versions when I checked:
-
.NET Framework's csc.exe - version 4.7.2556.0
-
Visual Studio 2013 - version 12.0.21005.1
-
Visual Studio 2015 - version 1.3.1.60616
-
Roslyn (1.0.0) - version 1.0.0.50618
C:\>"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe"
Microsoft (R) Visual C# Compiler version 4.7.2556.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
warning CS2008: No source files specified
error CS1562: Outputs without source must have the /out option specified
C:\>"C:\Program Files (x86)\MSBuild\12.0\Bin\csc.exe"
Microsoft (R) Visual C# Compiler version 12.0.21005.1
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
warning CS2008: No source files specified
error CS1562: Outputs without source must have the /out option specified
C:\>"C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe"
Microsoft (R) Visual C# Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.
warning CS2008: No source files specified.
error CS1562: Outputs without source must have the /out option specified
C:\>"C:\projects\WebApplication1\WebApplication1\bin\roslyn\csc.exe"
Microsoft (R) Visual C# Compiler version 1.0.0.50618
Copyright (C) Microsoft Corporation. All rights reserved.
warning CS2008: No source files specified.
error CS1562: Outputs without source must have the /out option specified
That's why when I build the solution with Visual Studio 2015, it works fine because it uses a different csc.exe, but errors sometimes appear when deploying to local IIS or the cloud. Also, ASP.NET MVC Razor uses a different compiler than C#. For example, if you have the following code, with a target framework of 4.6 and you're not using Roslyn, it will build fine but you'll see a compiler error when deploying locally.
@model WebApplication1.Models.MyModel
@if (string.IsNullOrWhiteSpace(Model.Test?.ToLower()))
{
}
Roslyn
It looks like things are moving toward using Roslyn, so make sure these two NuGet packages are available in your solution: Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers. Also, make sure the web.config changes are applied. Roslyn should be able to determine what version is appropriate for your solution.
It should be simple — just add the following NuGet packages. But make sure the web.config is updated. Sometimes I've had the issue where the web.config is updated locally but the IIS site's web.config is not updated, so it ends up not using Roslyn and instead using the .NET Framework's csc.exe compiler.
NuGet Packages
-
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
-
Microsoft.Net.Compilers
WebConfig
Conclusion
When deploying ASP.NET MVC applications that use C# 6 or later features, you may encounter compiler errors because the default .NET Framework csc.exe only supports up to C# 5. The solution is to install the Roslyn NuGet packages (Microsoft.CodeDom.Providers.DotNetCompilerPlatform and Microsoft.Net.Compilers) and ensure the web.config is properly updated to use Roslyn as the compiler.
References
-
Command-line build with csc.exe
-
How to add dynamic compilation to your C# projects?
-
Github - roslyn
-
Removing roslyn from ASP.NET 4.5.2 project template
-
Publish website without roslyn