找不到部分路径... bin\roslyn\csc.exe

Could not find a part of the path ... bin\roslyn\csc.exe

我正在尝试 运行 Asp.net 从 TFS 源代码管理中检索 MVC 项目。我已经添加了所有程序集引用,并且能够成功构建和编译,没有任何错误或警告。

但是我在浏览器中收到以下错误:

Could not find a part of the path 'C:\B8akWorkspace\B8akProject\B8akSolution\B8AK.Portal\bin\roslyn\csc.exe'.

这是错误页面的完整屏幕截图。

经过几天的研究,我了解到Roslyn 是提供高级编译功能的.Net 编译器平台。但是,我不明白为什么我的构建试图找到 \bin\roslyn\csc.exe,因为我没有配置与 Roslyn 相关的任何内容,也不打算在我的项目中使用 Roslyn。

默认 VS2015 模板的问题是编译器实际上并未复制到 tfr\bin\roslyn\ 目录,而是复制到 {outdir}\roslyn\ 目录

在您的 .csproj 文件中添加此代码:

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" Condition="!$(Disable_CopyWebApplication) And '$(OutDir)' != '$(OutputPath)'">
    <ItemGroup>
      <RoslynFiles Include="$(CscToolPath)\*" />
    </ItemGroup>
    <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
    <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>

这是一种更 MSBuild 的方法。

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" Condition="!$(Disable_CopyWebApplication) And '$(OutDir)' != '$(OutputPath)'">
    <ItemGroup>
      <RoslynFiles Include="$(CscToolPath)\*" />
    </ItemGroup>
    <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
    <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>

但我注意到 roslyn 文件也在我的 bin 目录中(不在文件夹中)。不过,该应用程序似乎可以正常工作。

在我的例子中,我只需要转到 Visual Studio 解决方案资源管理器(Web 应用程序项目)中的 bin 目录并直接包含 roslyn 项目。通过右键单击文件夹并选择包含在项目中。并再次签入解决方案以触发构建过程。

默认情况下不包括 roslyn 文件夹。

为了防止构建也将 Roslyn 文件复制到 bin 目录,您还必须注释掉位于 Web 应用程序项目顶部的这一行:

<!--  <Import Project="..\..\..\DAS\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.1\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\..\..\DAS\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.1\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" /> -->

TL;博士

运行 在程序包管理器控制台中:

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

更多信息

此问题与 Visual Studio 本身无关,因此建议添加构建步骤以复制文件的答案是一种解决方法。与手动将编译器二进制文件添加到项目相同。

Roslyn 编译器来自 NuGet 包,is/was 该包的某些版本中存在错误(我不知道具体是哪些)。解决方案是 reinstall/upgrade 那个包到一个没有错误的版本。最初在我于 2015 年写下答案之前,我通过在特定版本安装以下软件包来修复它:

  • Microsoft.Net.Compilers 1.1.1
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.1

然后我查看了 .csproj 并确保包的路径是正确的(在我的例子中是 ..\..\packages\*.*)在顶部和 <Target> 名称在底部 "EnsureNuGetPackageBuildImports"。这是在 MVC 5 和 .NET Framework 4.5.2 上。

所以, 基本上对我有用,但我不得不调整一些选项。具体来说,我必须删除目标上的条件,并更改 Include 属性,因为在我们的构建服务器上构建项目时 $CscToolPath 为空。奇怪的是,当 运行 在本地时 $CscToolPath 不为空。

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" >
  <ItemGroup>
    <RoslynFiles Include="$(SolutionDir)packages\Microsoft.Net.Compilers.1.1.1\tools\*" />
  </ItemGroup>
  <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
  <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>

在我的例子中,与 Basim 类似,有一个 NuGet 包告诉编译器我们需要 C# 6,但我们不需要。

我们必须删除 NuGet 包 Microsoft.CodeDom.Providers.DotNetCompilerPlatform,然后删除:

  1. <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" /> 来自 packages.config 文件
  2. <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>

system.codedom节点中,可以看到为什么要引入roslyn:compilerOptions="/langversion:6

默认 VS2015 模板的问题是编译器实际上并未复制到 {outdir}_PublishedWebsites\tfr\bin\roslyn\ 目录,而是复制到 {outdir}\roslyn\ 目录。这可能与您的本地环境不同,因为 AppHarbor 使用输出目录构建应用程序而不是构建解决方案 "in-place"。

要修复它,请在 xml 块 <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">...</Target>

之后的 .csproj 文件末尾添加以下内容
<PropertyGroup>
  <PostBuildEvent>
    if not exist "$(WebProjectOutputDir)\bin\Roslyn" md "$(WebProjectOutputDir)\bin\Roslyn"
    start /MIN xcopy /s /y /R "$(OutDir)roslyn\*.*" "$(WebProjectOutputDir)\bin\Roslyn"
  </PostBuildEvent>
</PropertyGroup>

参考:https://support.appharbor.com/discussions/problems/78633-cant-build-aspnet-mvc-project-generated-from-vstudio-2015-enterprise

如果您要添加 ASPNETCOMPILER 以在 MVC 中编译 Razor 视图,例如 this Whosebug question,则将 PhysicalPath 更改为 Roslyn nuget 包所在的位置(通常通过 $CscToolPath变量):

<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(CscToolPath)" />

打开项目文件并删除 所有引用 Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0....

打开web.config并删除所有system.codedom编译器属性

将 PropertyGroup 添加到您的 .csproj 文件

<PropertyGroup>
  <PostBuildEvent>
    if not exist "$(WebProjectOutputDir)\bin\Roslyn" md "$(WebProjectOutputDir)\bin\Roslyn"      
    start /MIN xcopy /s /y /R "$(OutDir)roslyn\*.*" "$(WebProjectOutputDir)\bin\Roslyn"
  </PostBuildEvent>
</PropertyGroup>

问题

请注意,NuGet PM 破坏了 Rosalyn 行为。单击 Tools > NuGet Package Manager > Manage NuGet Packages for Solution 如果存在 Microsoft.CodeDom.Providers.DotNetCompilerPlatformMicrosoft.Net.CompilersMicrosoft.Net.Compilers.netcore 的更新,请更新它们,解决方案将会中断!发生这种情况是因为 ASP 站点模板设置为在项目创建时使用特定版本。要查看问题,请单击解决方案资源管理器中的显示所有文件。

修复

在项目创建时 $(WebProjectOutputDir)\bin 不存在,因此当 Rosalyn 被 NuGet 添加为依赖项时,它会正确安装它。更新解决方案包后,$(WebProjectOutputDir)\bin 目录如下所示:

$(WebProjectOutputDir)\bin\bin\rosalyn

最简单的解决方法是将 rosalyn 剪切并粘贴到正确的位置,然后删除多余的 bin 文件夹。您现在可以刷新页面,网站将加载。

您的构建正在尝试查找 \bin\roslyn\csc.exe,因为以下包已添加到您的项目中。只需查看您的 packages.config 文件,您就可以同时拥有它们

Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Microsoft.Net.Compilers

What is Roslyn and Who added them(packages) in the project : If you’re using .net Framework 4.5.2 to create projects using VS2015, you might have noticed that the project templates use Roslyn by default. Actually, Roslyn is one of open-source compilers for .NET languages from Microsoft.

Why should we delete Roslyn : If your project has Roslyn references and you are interested to deploy it on server, you will get unwanted errors on the website as many hosting providers still have not upgraded their servers and hence do not support Roslyn. To resolve this issue, you will need to remove the Roslyn compiler from the project template.

如果您对使用 Roslyn 不感兴趣, 按照以下步骤删除它

1. 删除 NuGet 包,从 Nuget 包控制台使用以下命令

PM> Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
PM> Uninstall-package Microsoft.Net.Compilers

2. 执行此操作后,您的 web.config 文件应该会自动更新。如果不是,请在 web.config 文件中查找以下代码,如果找到,请删除这段代码。

<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>
      <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+"></compiler>
    </compilers>
</system.codedom>

Microsoft.CodeDom.Providers.DotNetCompilerPlatform 从 1.0.0 升级到 1.0.1 为我解决了这个问题。

我不得不更改 WebAPI 和 MVC 项目文件以不构建视图:

<MvcBuildViews>false</MvcBuildViews>

这解决了我使用 roslyn 的 TFS 2015 构建服务器错误。仍然不确定为什么 csc.exe 被复制到 \bin\csc.exe,但发布过程正在寻找 \bin\Roslyn\csc.exe...找不到导致该差异的转换。

更新 nuget 包对我有用 右键单击解决方案 > 管理解决方案的 NuGet 包 并更新所有包,特别是: Microsoft.Net.CompilersMicrosoft.CodeDom.Providers.DotNetCompilerPlatform

删除解决方案资源管理器中的 Bin 文件夹并重新生成解决方案。那将解决问题

我在 Jenkins 构建服务器 运行ning MSBuild 上遇到了这个错误,它将构建文件输出到一个单独的文件夹位置 (_PublishedWebsites)。完全一样 - roslyn 文件夹不在 bin 目录中,所有 roslyn 文件都与 bin 文件放在一起。

是唯一对我有用的东西(因为我使用的是 C# 6 语言功能,我不能简单地按照其他答案卸载 nuget 包),但因为我也是 运行ning CodeAnalysis,我在部署目标服务器上遇到另一个错误:

检测到尝试覆盖名称为“”的类型 Microsoft.CodeAnalysis.ICompilationUnitSyntax 的现有映射,当前映射到类型 Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax,到类型 Microsoft.CodeAnalysis.VisualBasic.Syntax.CompilationUnitSyntax。

原因是因为 roslyn 文件被转储到主 bin 目录中,当您 运行 xcopy 在嵌套的 roslyn 文件夹中重新创建它们时,您现在有这些文件的 2 个副本正在编译,它们之间存在冲突。在非常沮丧之后,我决定使用 'hack' 修复 - 一个额外的 post-build 任务来从 bin 目录中删除这些文件,从而消除冲突。

我的违规项目的 .csproj 现在看起来像:

.....................更多信息......................

 <PropertyGroup>
   <PostBuildEvent>
   if not exist "$(WebProjectOutputDir)\bin\Roslyn" md "$(WebProjectOutputDir)\bin\Roslyn"
   start /MIN xcopy /s /y /R "$(OutDir)roslyn\*.*" "$(WebProjectOutputDir)\bin\Roslyn"
 </PostBuildEvent>
</PropertyGroup>
<Target Name="DeleteDuplicateAnalysisFiles" AfterTargets="AfterBuild">
   <!-- Jenkins now has copies of the following files in both the bin directory and the 'bin\rosyln' directory. Delete from bin. -->
   <ItemGroup>
     <FilesToDelete Include="$(WebProjectOutputDir)\bin\Microsoft.CodeAnalysis*.dll" />
   </ItemGroup>
   <Delete Files="@(FilesToDelete)" />
</Target>

.....................更多信息......................

清理和重建对我有用!

我在服务器上安装我的应用程序时遇到了同样的问题,但在本地主机上一切正常。

None 这些解决方案都成功了,我总是遇到同样的错误:

Could not find a part of the path 'C:\inetpub\wwwroot\myApp\bin\roslyn\csc.exe'

我最终这样做了:

  • 在我的安装项目上,右键单击,查看 > 文件系统
  • 创建一个 bin/roslyn 文件夹
  • select 添加 > 文件并添加来自 packages\Microsoft.Net.Compilers.1.3.2\tools
  • 的所有文件

这解决了我的问题。

我 运行 通过发布管道(生成 _PublishedWebsites 目录)解决了这个问题,并将其用作项目中的目标:

<Target Name="CopyRoslynCompilerFilesToPublishedWebsitesDirectory" AfterTargets="AfterBuild" Condition="Exists('$(OutDir)\_PublishedWebsites$(TargetName)')">
    <Copy SourceFiles="@(RoslyCompilerFiles)" DestinationFolder="$(OutDir)\_PublishedWebsites$(TargetName)\bin\roslyn" ContinueOnError="true" SkipUnchangedFiles="true" />
</Target>

缺点是输出中会有两个 Roslyn 文件副本。

我在重命名解决方案和一些包含的项目并尝试删除 nuget 包后遇到此错误。我将新项目与上一个工作项目进行了比较,发现缺少以下几行,需要重新添加:

  <Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
  <Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)\Microsoft.Common.props')" />

这样做解决了我的问题。

我在 运行 项目时也遇到了同样的问题。以下是我遵循的步骤。

  1. 在解决方案中右击
  2. select 清洁解决方案
  3. 清理成功后,再次构建你的项目
  4. 运行再次项目

这次我没有看到同样的错误。这按预期工作。

根据上面 Daniel Neel 的评论:

Microsoft.CodeDom.Providers.DotNetCompilerPlatform Nuget 包的 1.0.3 版本适用于我,但 1.0.6 版本导致此问题出现错误

降级到 1.0.3 为我解决了这个问题。

这是 known issue with Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.6。降级到 1.0.5 为我解决了这个问题。

我 运行 在通过 NuGet 更新了一些包后遇到了这个问题。重建(而不是正常构建)对我有用。

我在更新 DotNetCompilerPlatform 后遇到了同样的问题。 通过重新启动解决 Visual Studio > 清理项目 >​​ 构建项目。

仅供参考...

自 2017 年 8 月 31 日起升级到 Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.7 即可。

这可以通过以下简单的方式完成-:

  • 在您系统的任意位置创建一个相似类型的新项目。构建它并将 roslyn 文件夹复制到您的 bin 目录。

我的解决方案是使用 Nuget 将以下项目更新到最新版本: - Microsoft.Net.Compilers - Microsoft.CodeDom.Providers.DotNetCompilerPlatform 然后重建项目。由于我的项目是一个网站,所以没有 *.csproj 文件。 当我尝试在浏览器中查看 cshtml 时出现上述错误。

以上两项更新到最新版本后修复了错误。 我在 VS2015 和 windows7 SP1

就我而言,当 Jenkins 尝试将其部署到 Octopus 中时出现以下错误:

MSBUILD : OctoPack error OCT-1676060969: Failed to build the path for '\bin\roslyn\csc.exe' relative to 'T:\workspace\machine.engine\Machine.engine.Test': Invalid URI: The format of the URI could not be determined.. See the inner exception for more details. [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969: System.Exception: Failed to build the path for '\bin\roslyn\csc.exe' relative to 'T:\workspace\machine.engine\Machine.engine.Test': Invalid URI: The format of the URI could not be determined.. See the inner exception for more details. ---> System.UriFormatException: Invalid URI: The format of the URI could not be determined. [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at System.Uri..ctor(String uriString) [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.Util.OctopusPhysicalFileSystem.GetPathRelativeTo(String fullPath, String relativeTo) in Z:\buildAgent\workDirba9f2e0d5e4022\source\OctoPack.Tasks\Util\OctopusPhysicalFileSystem.cs:line 211 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    --- End of inner exception stack trace --- [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.Util.OctopusPhysicalFileSystem.GetPathRelativeTo(String fullPath, String relativeTo) in Z:\buildAgent\workDirba9f2e0d5e4022\source\OctoPack.Tasks\Util\OctopusPhysicalFileSystem.cs:line 224 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.CreateOctoPackPackage.AddFiles(XContainer nuSpec, IEnumerable`1 sourceFiles, String sourceBaseDirectory, String targetDirectory, String relativeTo) in Z:\buildAgent\workDirba9f2e0d5e4022\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 443 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.CreateOctoPackPackage.Execute() in Z:\buildAgent\workDirba9f2e0d5e4022\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 190 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
Done Building Project "T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj" (default targets) -- FAILED

原因

花了一些时间后,我使用了一个使用 Microsoft.Net.Compilers 的内部开发组件。内部组件使用 Microsoft.Net.Compilers 的原因是为了解决这个问题 () and was solved this way ()。这导致,当我在主程序上安装组件时,Microsoft.Net.Compilers 会自动添加它。

解决方案

我的解决方法是,通过(按照@malikKhalil 的回答)

从我们的内部组件中卸载以下内容
PM> Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
PM> Uninstall-package Microsoft.Net.Compilers

并在 Jenkins 中选择了 C# 7 编译器而不是 C# 6 并重新构建,这是为了确保一切正常并正确构建。

最后在我的主程序中我尝试更新我的内部组件。而一切都比重新构建。它的构建没有任何问题。

我在 Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.06 中遇到了这个错误,但在 1.0.7 中也遇到了这个错误,它适用于@PrisonerZERO。然而,当微软发布 1.0.8 2017-10-18 时,它终于再次开始为我工作,我不必降级。

https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/

除了从解决方案中的所有项目中删除 Bin 目录外,还删除 obj 文件夹。

在主解决方案目录中删除文件夹 .vs

在尝试将已完成的项目带入在 git 上创建的空白解决方案时为我工作。

我有没有 csproj 文件的 webproject,这里提到的解决方案对我不起作用。

更改目标 .NET 框架、重新安装包 (Update-Package -reinstall) 然后构建项目对我来说很有效。您甚至可以在此操作后更改目标框架(让您在之后再次重新安装 nuget 包)。

您需要安装Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix, 专为该错误而创建

在没有雪茄的情况下尝试所有修复后,我通过在 Visual Studios 中更新此 Nuget 包来修复它:

Microsoft.CodeDom.Providers.DotNetCompilerPlatform

我的是1.0.0到2.0.0供参考(错误不再显示)

我按照这些步骤操作,效果很好

  • 删除所有bin和obj文件夹
  • 清理解决方案并重建
  • 运行 powershell 中的这个命令

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

就我而言,当我同时 运行 两个 visual studio IDE 时遇到了这个问题。所以解决方案是清理项目并关闭另一个实例。

  • 右键单击您的项目并select管理 Nuget 包
  • 查找"Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
  • 只需更新到较旧或较新的版本(无关紧要),然后再次更新回您的原始版本。

这将重新安装包的所有依赖项和文件(如 csc.exe)

网站项目和Web应用程序项目的答案不同。 根本问题是一样的 NuGet 包在不同的机器上表现不同。可能是权利问题或某些执行策略阻止它复制到 Bin 文件夹 如您所知,Roslyn 是新的编译器。 你应该把它放在这些项目的 Bin 文件夹中 转到您的网站 NuGet Packages 检查此文件夹 Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0 \code\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0 你看到了吗? 你能看到里面有code\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\tools\RoslynLatest 现在,作为编译此文件夹的一部分,应该像这样在 bin 下复制到您的网站。 \code\WebSite1\Bin\Roslyn 有些事为什么不会发生在你身上。 尝试 运行 Visual studio 作为管理员。 手动复制 Roslyn 文件夹。 尝试卸载并安装 NuGet 包。 记住这个包编译你的文件夹,如果它不在那里你就不能编译任何东西,所以你也不能添加任何东西。 尝试将此包复制到离线版本 工具 -> 选项 -> nuget 包管理器 -> 包源 -> Microsoft Visual Studio 离线包 C:\Program Files (x86)\Microsoft SDKs\NuGetPackages

  1. 清洁解决方案
  2. 重建解决方案 ,这两个步骤对我有用。

其中很多答案都涉及 Nuget 包 and/or 清理和重新加载您的项目。

如果您有 WCF 服务引用和无效端点,您也会收到此错误消息。确保您的端点正确,并在 .config 中以及从 GUI 配置服务引用时使用正确的端点更新服务配置。

an issue in the Roslyn project on GitHub 中所述,一个解决方案(对我有用)是简单地卸载并重新加载 Visual Studio 中的项目。

在我重新加载项目之前,"bin\roslyn" 文件夹未在构建或重建时创建。

在我的例子中,只需删除 bin 文件夹中的所有内容并重新编译就可以完成所有工作。

重启Windows。

这是我尝试重建后唯一有效的解决方案,删除 bin 的内容并重建,重新启动 Visual Studio。

这是 C#/.NET 构建工具多么糟糕的另一个例子。

我认为(阅读了很多答案后),总体结论是这个问题的原因和解决方案在很大程度上取决于设置和项目,所以如果一个答案不起作用,就尝试另一个。在弄乱 NuGet 包或重新安装开发工具之前,首先尝试 non-intrusive/destructive 解决方案,例如重新启动 Visual Studio、重新引导、重建等。祝你好运!

(注意:使用 Visual Studio 2019,项目文件最初创建于 Visual Studio 2015。也许这有助于有人调查问题)

(编辑:这可能是由于 installing/modifying 安装 Visual Studio 后未重新启动或在安装程序提示重新启动时更新 Visual Studio 造成的吗?)

在我的情况下,我们的团队不想保留 'packages' 文件夹,所以我们将所有 dll 放在其他目录中,例如 'sharedlib'.

我用构建事件解决了这个问题。

if "$(ConfigurationName)" == "Release" (
goto :release
) else (
goto:exit
)

:release
if not exist $(TargetDir)\roslyn mkdir $(TargetDir)\roslyn

copy /Y "$(ProjectDir)..\..\Shared Lib\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\tools\Roslyn45\*" "$(TargetDir)\roslyn"
goto :exit

:exit

我在部署到的服务器上遇到了这个问题,并确定我不需要

Microsoft.CodeDom.Providers.DotNetCompilerPlatform

所以,我通过 nuget 卸载了它,并删除了网络配置中的引用。没有更多问题。

我最初尝试将目标节点添加到 .proj 文件中,如其他一些答案中所述,但这只会导致另一个错误,其中 msbuild 无法复制 pagefile.sys从我读到的内容来看,这似乎是 nuget 包中的错误。

0。快速修复

中所述, 快速修复是使用包管理器,Tools > Nuget Package 管理器 > 包管理器控制台,至运行

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

1。重现错误的代码

这是重现错误的代码:
https://user.it.uu.se/%7Ehesc0353/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)

考虑尝试上面 zip 文件中提供的示例代码。
如果不做任何改动,Ctrl+F5会重现 错误。

2。更强大的解决方案

另一种解决方案是从项目的 Web.config 文件。
Web.config.csproj 文件位于同一目录中。)
如果它们 都不见了。

在文本编辑器或 Visual Studio 中打开 Web.config 文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings></appSettings>
  ...
  <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>

标签里配置>system.codedom>编译器> compiler language="c#;cs;csharp", 完全删除 type 属性。 – 简而言之,删除以 type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, 1

Visual Studio 会处理剩下的事情。 – 不再 Server Error in '/' Application.

3。 HTTP 错误 403

在上面提供的示例中,按 Ctrl+F5 现在将导致 HTTP 错误 403.

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


作为挑衅,我尝试从中删除整个 package 目录 Visual Studio 项目。
项目一完成,它就会自动安静地重新创建 重建。

参考资料


1 据推测,同样的修复也适用于 Visual Basic 至于C#,我没试过。

None 其他答案对我有用。在将 before/after 与我预期提交的文件进行文件夹比较后,我发现 GIT 忽略了一个必需的文件夹。如果您正在跟踪存储库中的编译器,请确保跟踪 BUILD 文件夹。如果不是,编译器将永远不会被构建,并且会在发布后抛出这个确切的错误。我将这一行添加到我的 .gitignore 文件中:

!**/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1/build/

现在它正在正确部署到其他计算机。

对于 VS 2019,完全删除以下节点:

<system.codedom>
</system.codedom>

以下为我解决了这个问题:

  • 正在更新到最新版本 Visual Studio 2017(使用安装程序)

  • 清理并重建解决方案

来不及回答,但仍然发帖以防对任何人有帮助。
按照以下步骤为我修复了错误:

  1. 删除包文件夹
  2. 开VS
  3. 重建
  4. 观察到 NuGet 包已恢复,但 bin\roslyn 未创建
  5. 卸载项目
  6. 重新加载项目
  7. 重建
  8. 观察 bin\roslyn 现在已经创建。

就我而言,在尝试任何其他解决方案之前,我切换到“发布”配置,重建(文件夹已创建)然后切换回“调试”,同时文件夹保持不变。

这是从旧解决方案的源代码管理中检出的,显然原始(自动)包恢复和构建项目没有在 bin 目录中创建该文件夹。

请注意,在撰写本文时,受指责的组件已达到 v.2。

我尝试了多个最佳答案,直到以下步骤奏效(ASP.NET 项目针对 .NET Framework 4.6.2,Visual Studio 2019 年在具有疯狂限制性组策略的系统上,2021 年 3 月)。

我需要:

  • 运行 VS 作为 Admin

  • 在包管理器控制台中 运行

    Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -v 2.0.1
    
  • 清理和重建解决方案

没有 运行宁 VS 作为管理员,组策略阻止了 ps1 Update-Package 需要 运行 的脚本。

PS。在此之前,我尝试了许多其他答案(并且 运行 git reset --hard 在他们失败后)。我不知道他们中的任何一个是否为最终的工作做出了贡献。我试过了:

安装 nuget 包:Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix/1.0.0

对于那些在构建服务器(TFS 或 Bamboo)上编译时苦苦挣扎的人,我能够通过从“/t:”msbuild 选项中删除“clean”选项来解决这个问题。

我也遇到了同样的问题,通过 运行 nuget 控制台中的以下命令解决了

安装包 Microsoft.Net.Compilers - 版本 3.3.1

就我而言,我注意到“packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0”路径中不存在“build”文件夹。这就是为什么没有创建 roslyn 文件夹的原因。

所以我的解决方案是:

  1. 清洁溶液
  2. 转到 Nuget 包管理器
  3. 卸载微软。CodeDom.Providers.DotNetCompilerPlatform
  4. 清洁溶液
  5. 从 Nuget 包管理器再次安装 Microsoft。CodeDom.Providers.DotNetCompilerPlatform。
  6. Clean/Rebuild

好了。 None 以上解决方案对我有用。希望这对任何人都有帮助