简体   繁体   English

仅在Visual Studio代码中编译错误

[英]Compilation error only in Visual Studio Code

I downloaded the .NET SDK for the Froala WYSIWIG HTML Editor . 我下载了.NET SDK for the Froala WYSIWIG HTML Editor.NET SDK for the Froala WYSIWIG HTML Editor The download link is available here: https://www.froala.com/wysiwyg-editor/docs/sdks/dotnet 下载链接可在此处获取: https//www.froala.com/wysiwyg-editor/docs/sdks/dotnet

Simply download the zip, extract it and open it. 只需下载zip,解压缩并打开它。

When compiling the solution inside Visual Studio 2019, I don't have any Errors/Warnings. 在Visual Studio 2019中编译解决方案时,我没有任何错误/警告。 No problem either at runtime. 在运行时也没问题。

When opening the exact same solution inside Visual Studio Code, I got some errors when compiling ( dotnet build ) even if compilation succeed. 在Visual Studio Code中打开完全相同的解决方案时,即使编译成功,编译时也会遇到一些错误( dotnet build )。 And no problem at runtime. 在运行时没问题。

The errors are mainly Reference type 'HttpContext' claims defined 'System.Web', but not found 错误主要是引用类型'HttpContext'声明定义'System.Web',但未找到

As stated here it may be a problem of targetting 2 different .NET framework versions. 如前所述这里可能是靶向2个不同.NET framework版本的问题。

The solution (.sln) is composed as below: 解决方案(.sln)的组成如下:

  • src project src项目
  • demo-core project (referencing src project) <--- set as default project demo-core项目(引用src项目)<---设置为默认项目
  • demo project (referencing src project) <---- not used because I use demo-core 演示项目(引用src项目)<----未使用因为我使用了demo-core

I was not able to find a solution for this annoying error warning at compilation. 我无法在编译时找到这个恼人的错误警告的解决方案。

UPDATE 1 更新1

src project is targetting the net472;netstandard2.0 src项目的目标是net472;netstandard2.0

demo-core project is targetting the .NET Core 2.0 demo-core项目针对的是.NET Core 2.0

UPDATE 2 更新2

I finally succeed get rid of compilation errors. 我终于成功摆脱了编译错误。 I simply removed net472 from targetted framework of the src projet and immediately compilation errors disappeared. 我只是从src projet的目标框架中删除net472 ,并立即编译错误消失。

What did I have ton conclude ? 我得到的结论是什么? How this modification impacts Visual Studio Code and why this is not necessary under Visual Studio 2019. Sorry but this is strange to me. 此修改如何影响Visual Studio Code以及为什么在Visual Studio 2019下不需要这样做。抱歉,这对我来说很奇怪。

None of the original sources downloaded from that site (and also verified in github commit history) reference net472; 从该站点下载的所有原始源(并且还在github提交历史中验证)都没有引用net472; they instead reference net471. 他们改为引用net471。 I suspect the solution was first tried in Visual Studio 2019 and, not having the .NET SDK 4.7.1 targeting pack installed, an attempt was made to retarget SDK 4.7.2. 我怀疑该解决方案首先在Visual Studio 2019中尝试过,并且没有安装.NET SDK 4.7.1目标包,尝试重新定位SDK 4.7.2。

Once you do this and re-open the source in VS Code, the Omnisharp extension attempts compilation for the first target framework found due to poor support for multiple target frameworks . 执行此操作并在VS Code中重新打开源代码后,Omnisharp扩展会尝试编译第一个目标框架,因为对多个目标框架的支持不足 In this case, the first found for the src project is net472 (which is important). 在这种情况下,src项目的第一个发现是net472(这很重要)。 Note that flipping the order of target frameworks doesn't have any effect. 请注意,翻转目标框架的顺序没有任何影响。

The error "Reference type 'HttpContext' claims defined 'System.Web', but not found" is now straight-forward to explain. 错误“引用类型'HttpContext'声明定义'System.Web',但未找到”现在是直接解释。 If we look for example in src/Image.cs, we see the following using statements: 如果我们在src / Image.cs中查找示例,我们将看到以下using语句:

using System;
using System.IO;
using System.Collections.Generic;
#if netcore
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.StaticFiles;
#else
using System.Web;
#endif

If the src project is "compiled" by the Omnisharp server using net472, it will import System.Web.HttpContext . 如果使用net472由Omnisharp服务器“编译”src项目,它将导入System.Web.HttpContext The demo-core project in Controllers/FoalaApiController.cs compiled for netcore2.0 instead references Microsoft.AspNetCore.Http.HttpContext . 针对netcore2.0编译的Controllers / FoalaApiController.cs中的演示核心项目改为引用Microsoft.AspNetCore.Http.HttpContext These obviously don't match. 这些显然不匹配。

Had Omnisharp selected the proper netcore2.0 target for the src project, then these would have matched. 如果Omnisharp为src项目选择了正确的netcore2.0目标,那么这些目标就会匹配。 It didn't so you see these errors reported by Omnisharp. 它没有让你看到Omnisharp报告的这些错误。 You don't see these errors from dotnet build or VS2019 because they properly resolve the correct target framework for the src project. 您没有从dotnet build或VS2019中看到这些错误,因为它们正确地解析了src项目的正确目标框架。


Here's what I first experienced when I downloaded the source. 这是我下载源代码时的第一次经历。 It's not clear what .NET Core SDK version you're using. 目前尚不清楚您正在使用的.NET Core SDK版本。 When I build the source I see the following error with .NET Core SDK 2.2.100. 当我构建源代码时,我看到.NET Core SDK 2.2.100出现以下错误。

error NU1003: PackageTargetFallback and AssetTargetFallback cannot be used together. 错误NU1003:PackageTargetFallback和AssetTargetFallback不能一起使用。 Remove PackageTargetFallback(deprecated) references from the project environment. 从项目环境中删除PackageTargetFallback(不建议使用)引用。

The fix for this error is simple: remove the PackageTargetFallback declaration from the .csproj files. 此错误的修复很简单:从.csproj文件中删除PackageTargetFallback声明。 After this, the project compiles without errors / warnings using dotnet build . 在此之后,项目使用dotnet build编译时没有错误/警告。

I didn't see any other errors in VS Code initially because I too did not have the .NET SDK 4.7.1 targeting pack installed. 我最初没有在VS Code中看到任何其他错误,因为我也没有安装.NET SDK 4.7.1目标包。 Consequently, Omnisharp could only compile the src project using netstandard2.0. 因此,Omnisharp只能使用netstandard2.0编译src项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio编译错误UWP - Visual Studio compilation error UWP Visual Studio将编译警告转换为生成错误 - Visual Studio convert Compilation Warning into Build Error .Net 4,Web API和Visual Studio 2010的自定义MediaTypeFormatter上的编译错误 - Compilation error on Custom MediaTypeFormatter with .Net 4, Web API and Visual Studio 2010 MS Visual Studio 子项目编译预建事件 IF 条件错误 - MS Visual Studio subproject compilation prebuilt events IF condition error Visual Studio编辑器的行为很奇怪! 显示每个实例的编译错误 - Visual Studio editor is behaving weird! Showing compilation error with every instance 仅在使用 Visual Studio 调试时执行代码 - Execute code only while debugging with Visual Studio os的Visual Studio条件编译 - Visual studio conditional compilation for os Web API返回JSON错误,但Visual Studio仅将其视为状态码500 - web api returns a json error but visual studio only sees it as status code 500 Visual Studio - 代码片段错误/不工作 AgumentNullException - Visual Studio - Code Snippet Error / Not Working AgumentNullException 尝试在 Visual Studio Code 中创建 webapi 时出错 - Error trying to create a webapi in Visual Studio Code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM