在ASP.NET WSP模式中,啟用C# 7.0(Roslyn)編譯器

安裝好Windows Server 2016以及.NET Framework 4.7後,滿心期待預設的C#會因此變革成新的Roslyn編譯器,但是一套用到新的C#寫法,依舊破功。對此還存有幻想的人,勸你放棄這個念頭吧!

以下是編譯器最後版本資訊(C# 5.0),以及測試編譯C#新語法糖時的錯誤資訊,看到讓大家死了這條心(which is no longer the latest version),基本上微軟就是叫你直接去參考Roslyn編譯器了:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319>csc test.cs
Microsoft (R) Visual C# Compiler version 4.7.2046.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

test.cs(9,25): error CS1056: 未預期的字元 '$'

將ASP.NET強制上C# 7.0(Roslyn)編譯器的方法

基本上跟這篇「在ASP.NET WSP模式中,啟用C# 6.0(Roslyn)編譯器」差不了多少,依然是用「nuget install Microsoft.Net.Compilers」去安裝新的Roslyn編譯器,但有一些不一樣之處將在下方說明。

Step 1. 在下載完後,你的nuget.exe所在的目錄下會多出一個類似「Microsoft.Net.Compilers.2.3.1」的資料夾,你必須進入這個資料夾中的tools目錄,將所有的檔案複製起來,貼到你的ASP.NET伺服器下的「C:\Windows\Microsoft.NET\Framework64\v4.0.30319」。這些檔案列計如下:

csc.exe
csc.exe.config
csc.rsp
csi.exe
csi.exe.config
csi.rsp
Microsoft.Build.Tasks.CodeAnalysis.dll
Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.CSharp.Scripting.dll
Microsoft.CodeAnalysis.dll
Microsoft.CodeAnalysis.Scripting.dll
Microsoft.CodeAnalysis.VisualBasic.dll
Microsoft.CSharp.Core.targets
Microsoft.DiaSymReader.Native.amd64.dll
Microsoft.DiaSymReader.Native.x86.dll
Microsoft.VisualBasic.Core.targets
Microsoft.Win32.Primitives.dll
System.AppContext.dll
System.Collections.Immutable.dll
System.Console.dll
System.Diagnostics.FileVersionInfo.dll
System.Diagnostics.Process.dll
System.Diagnostics.StackTrace.dll
System.IO.Compression.dll
System.IO.FileSystem.dll
System.IO.FileSystem.DriveInfo.dll
System.IO.FileSystem.Primitives.dll
System.IO.Pipes.dll
System.Reflection.Metadata.dll
System.Security.AccessControl.dll
System.Security.Claims.dll
System.Security.Cryptography.Algorithms.dll
System.Security.Cryptography.Encoding.dll
System.Security.Cryptography.Primitives.dll
System.Security.Cryptography.X509Certificates.dll
System.Security.Principal.Windows.dll
System.Text.Encoding.CodePages.dll
System.Threading.Thread.dll
System.ValueTuple.dll
System.Xml.ReaderWriter.dll
System.Xml.XmlDocument.dll
System.Xml.XPath.dll
System.Xml.XPath.XDocument.dll
vbc.exe
vbc.exe.config
vbc.rsp
VBCSCompiler.exe
VBCSCompiler.exe.config

※ 請注意,這些我列舉出的檔案、數量,以及編譯器的版本號,都是我在撰寫文章當下的產出物,不代表你在日後看到並且當下實作之時的最‵終結果。

Step 2. 在C# 7.0時代,當我們將上述的檔案放進去資料夾時,不若C# 6.0時代這麼簡單,應該是說,系統會出現無法取代檔案的問題。會發生這個問題是因為檔案權限被指派成TrustedInstaller而非Administrator(感覺是微軟的疏忽所致),因此必須進行權限的修正後,方能取代(覆蓋)之。

下方以System.Xml.ReaderWriter.dll這個衝突檔案當作範例,請觀看圖片內的解法,親自且逐一實作,將所有的衝突檔案排除即可。

我們可以看到這個dll權限屬於TrustedInstaller,而非我們自己的Administrator,請點選變更。

改回後,我們就可以發現這個dll屬於Administrator了。

最後請在確認一下Administrators是否擁有這個檔案的存取權限,如果沒有存取權限的話,一定要賦予,否則依然無法覆寫檔案。

一旦替換成功,到此資料夾下面輸入CSC,如果看到下方的資訊(沒有出現for C# 5.0),就代表你成功替換為Roslyn編譯器啦!

Microsoft (R) Visual C# Compiler version 2.3.1.61919 (57c81319)
Copyright (C) Microsoft Corporation. All rights reserved.

※ 最後在小小的補充說明,在StackOverflow中,有許多人認為csc.exe是depends upon the .NET Framework,我認為這種說法很模糊且無濟於事(Roslyn編譯器事實上並沒有隨著新的.Net安裝檔案推送出去),而對一般使用者來說,這種說法甚至可以視為是錯誤的。

相關連結:

Enable Initial .NETCompiler ASP.NET C#7.0 Csc.exe System.CodeDom.Compiler