'Could not find file ... bin\roslyn\csc.exe'

'Could not find file ... bin\roslyn\csc.exe'

在 Visual Studio 2017 年,当按 Ctrl+F5 到 运行 我的 ASP.NET Framework Web API 服务器,我得到:

Could not find file ... bin\roslyn\csc.exe:

运行 Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r 从某种意义上说,在包管理器控制台中不是永久修复 当包文件是服务器错误再次出现 丢失的。 我怎样才能一劳永逸地摆脱这个错误,以便需要 一旦我安装包,它们就会自动(并且静默地)重新安装 重新打开、构建和 运行 我的 Visual Studio 解决方案?


重现错误的代码: https://user.it.uu.se/~hesc0353/SrvrErr-reproduce.zip
(最初是从 https://github.com/aspnet/AspNetDocs/tree/master/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client/sample/server/ProductsApp)

正如你已经提到的, 快速修复是使用包管理器, Tools > Nuget Package Manager > Package Manager Console, 到 运行

Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r

正如所指出的

但另一种解决方案(我认为更可靠)是删除项目 Web.config 文件的一个属性。 (Web.config 与您的 .csproj 文件位于同一目录中。)

在文本编辑器(或在 Visual Studio 中)打开 Web.config 文件。
- 在标签 configuration | system.codedom | compilers | compiler language="c#;cs;csharp" 中,完全删除 type 属性。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- ... -->
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /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.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

简而言之,删除以type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft开头的行。

(据推测,相同的修复程序适用于 Visual Basic 和 Csharp,但我没有尝试过。)

Visual Studio 会处理剩下的事情。没有更多 Server Error in '/' Application.

在我在上面的 zip 文件中提供的示例代码中,您现在将获得 HTTP Error 403 当你按下 Ctrl+F5.

尝试将网络浏览器中的 http://localhost:64195 替换为 http://localhost:64195/api/products
网络 API 现在显示正常:

作为挑衅,我什至尝试删除 Visual Studio 解决方案的整个 package 目录。
当我(重新)构建它时,它会自动并静默地重新创建。