.NET Compiler Roslyn
Errors
You might have seemed something like following, your ASP.NET MVC solution build ok but getting error following compiler error when deploying to IIS local site or cloud web server. First, you know you have C# 6 or higher at your application, but not sure why it causes compile error during runtime. I have to spend some time to figure out what happens in my case, so I've added what I found out at here so hopefully, it is able to help someone else.
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 few different .NET compile exist, the
compiler is,csc.exe
but there's few different version. There are
followings.
- .NET Framework's
csc.exe
-
For example, .NET 4.6 located here.
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe
- Visual Studio
csc.exe
-
Visual Studio 2013 located here.
C:\Program Files (x86)\MSBuild\12.0\Bin\csc.exe
-
Visual Studio 2015 located here.
C:\Program Files (x86)\MSBuild\14.0\Bin\csc.exe
- Roslyn's
csc.exe (example below use 1.0.0)
-
Roslyn inside application bin folder, for example,
/bin/roslyn/csc.exe
So I think that's all the compiler in a different place so far and unfortunately, the version of the CSC.EXE are different. If they're all the same version I don't think I will see the error compiler C# 6 using C# 5 compiler.
In summary, following are the different version when I checked it out.
- .NET Framework's csc.exe - version 4.7.2556.0
- Visual Studio 2013 - version 12.0.21005.1
- Visual Studio 2015 - 1.3.1.60616
- Roslyn (1.0.0) - 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 is fine because it is using different csc.exe, but see error sometimes when deploying to local IIS or cloud. Also, ASP.NET MVC razor use a different compiler than C#, for example, if you have the code as following, target framework is 4.6 and you're not using Roslyn. This will also be built is fine, but when deploying to local you'll see compiler error.
@model WebApplication1.Models.MyModel
@if (string.IsNullOrWhiteSpace(Model.Test?.ToLower()))
{
}
Roslyn
Looks like things are moving to use Roslyn
so make sure
these two NuGet packages are available at your solution.
Microsoft.CodeDom.Providers.DotNetCompilerPlatform
,.Microsoft.Net.Compilers. Also, make sure webconfig changes.
Roslyn should be able to decide what version is good for your
solution.
It should be simple just add following Nuget Package, but just make sure WebConfig is updated, sometimes I have the issue, WebConfig is updated on local but IIS site's WebConfig is not updated, so end up not using Roslyn but using .NET Framework's CSC.exe compiler.
NuGet packages
- Microsoft.CodeDom.Providers.DotNetCompilerPlatform
- Microsoft.Net.Compilers
WebConfig
<system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.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.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> </configuration>