Compilation error only in Visual Studio Code - c#

I downloaded the .NET SDK for the Froala WYSIWIG HTML Editor. The download link is available here: https://www.froala.com/wysiwyg-editor/docs/sdks/dotnet
Simply download the zip, extract it and open it.
When compiling the solution inside Visual Studio 2019, I don't have any Errors/Warnings. 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. And no problem at runtime.
The errors are mainly Reference type 'HttpContext' claims defined 'System.Web', but not found
As stated here it may be a problem of targetting 2 different .NET framework versions.
The solution (.sln) is composed as below:
src project
demo-core project (referencing src project) <--- set as default project
demo project (referencing src project) <---- not used because I use demo-core
I was not able to find a solution for this annoying error warning at compilation.
UPDATE 1
src project is targetting the net472;netstandard2.0
demo-core project is targetting the .NET Core 2.0
UPDATE 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.
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.

None of the original sources downloaded from that site (and also verified in github commit history) reference net472; they instead reference 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.
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. In this case, the first found for the src project is net472 (which is important). 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. If we look for example in src/Image.cs, we see the following using statements:
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. The demo-core project in Controllers/FoalaApiController.cs compiled for netcore2.0 instead references 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. It didn't so you see these errors reported by Omnisharp. You don't see these errors from dotnet build or VS2019 because they properly resolve the correct target framework for the src project.
Here's what I first experienced when I downloaded the source. It's not clear what .NET Core SDK version you're using. When I build the source I see the following error with .NET Core SDK 2.2.100.
error NU1003: PackageTargetFallback and AssetTargetFallback cannot be used together. Remove PackageTargetFallback(deprecated) references from the project environment.
The fix for this error is simple: remove the PackageTargetFallback declaration from the .csproj files. After this, the project compiles without errors / warnings using 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. Consequently, Omnisharp could only compile the src project using netstandard2.0.

Related

WebGrid .getHtml issue. Reference to type 'IHtmlString' claims it is defined in 'System.Web', but it could not be found

I'm working on an ASP.NET application in Visual Studio 2019 using NET 5.0 and attempting to display database entries using a webgrid that i have named 'grid' but am unable to use 'grid.GetHtml'. Whenever I use it I receive the following error:
Reference to type 'IHtmlString' claims it is defined in 'System.Web', but it could not be found.
I have tried reinstalling all my NuGet packages and rebuilding my solution multiple times but no luck.
I feel like there may be some inconsistencies between my target framework and my packages but I'm not sure how to fix this. I had to create a web.config file to establish my connection string to the DB but other than that my project is pretty bare bones and straight forward so far.
[Code with 'GetHtml' error]
[1]: https://i.stack.imgur.com/BvfJ7.png
[List of NuGet Packages]
[2]: https://i.stack.imgur.com/nkqf8.png
I found the solution for my particular issue. When creating the project in Visual Studio 2019 I selected the template 'ASP.NET Web App (Model View Controller)'. Apparently this template is not compatible with .NET 4.x, which is what some of the packages I was using required. In fact, I couldn't even find a way to switch the targeted framework to something besides 5.0 or 3.1.
So I created a new project from scratch using the template 'ASP.NET Web Application (.NET Framework)' which allowed me to select frameworks from a larger list, including the .NET 4.x frameworks (I used 4.8, this included additional files by default like the web.config file) which was necessary for my project.
Add the following to your file
using System.Web;
Check here also: The type 'System.Web.IHtmlString' is defined in an assembly that is not referenced

In Linux , The reference assemblies for framework ".NETFramework,Version=v4.5" were not found

I have made a setup of Visual studio to compile C# code in my Ubuntu Machine .
I loaded the workspace/my code to VS and I could see the below error.
The reference assemblies for framework ".NETFramework,Version=v4.5" were not found. To resolve this, install the SDK or Targeting Pack for this framework version or retarget your application to a version of the framework for which you have the SDK or Targeting Pack installed. Note that assemblies will be resolved from the Global Assembly Cache (GAC) and will be used in place of reference assemblies. Therefore your assembly may not be correctly targeted for the framework you intend.
Please help me on resolving this issue as am a beginner in Visual studio.
Thanks
If project is heavy, you can follow
this procedure
If your project is lightweight, create a new .netcore project using VS and move your code (and dependencies references)into that new project. then let VS telling you potential errors and correct them.
Looking at the procedure, you can firstly retargeting your actual project in dotnet 4.6.2 framework in order to "ensures that you can use API alternatives for .NET Framework-specific targets in the cases where .NET Core can't support a particular API."
I would recommend running the portability tool in Visual Studio 2017 if you have it. This will give you an idea if you will have a hard time moving it over.
As for your error with the csproj, that's because that file has paths within it, which are pointing to locations using windows paths, instead of Linux paths.

Why is Referenced Component not be found in Visual Studio 2017 when it is found in Visual Studio 2015

I have a solution with two projects - a primary project, and a unit test project. When opening in Visual Studio 2015 all references are found and project builds successfully. When opening in Visual Studio 2017 several but not all NuGet package references are not found and compile fails. Some of the failed references are...
System.Data.Common
System.Net.Http
System.Net.Http.WebRequest
System.Security.Cryptography.Algorithms
System.Security.Cryptography.Encoding
System.Security.Cryptography.Primitives
System.Security.Cryptography.X509Certificates
... but other NuGet references are found with no problem. This solution was created with VS2015. When viewing the .csproj file nothing out of the ordinary is jumping out.
I am thinking of rebuilding it from scratch in VS2017 to try to identify the problem.
Has anyone else experienced this problem, and/or has anyone any suggestions on why this is occurring and what should be done to facilitate a fix?
Update:
I created a brand new VS2017 WebApi project referencing .NET 4.7.1, and compiled successfully. I then added NuGet package System.Data.Common 4.3.0. The NuGet install process appears to have completed with no errors, but still I am left with an invalid reference. That was pretty easy to replicate.
OK, answering my own question.
I found what I believe is the answer. This particular project was originally developed in VS2015 using .NET 4.6.2. When changing to VS2017 we elected to upgrade .NET to 4.7.1. The problem is with the .NET version, not the VS version.
The newer version of .NET has many of these NuGet assemblies added to standard libraries. The NuGet packages were in conflict with the native .NET 4.7.1 namespaces. For example, in .NET 4.7.1, the namespace System.Data.Common is found in the assembly System.Data.dll. No longer is it required to add a NuGet assembly System.Data.Common.dll. In fact, if I do add System.Data.Common NuGet package assembly, I now have two assemblies having the namespace System.Data.Common - one in System.Data.dll and another in System.Data.Common.dll - hence the reference problem.
The solution is to use the .NET 4.7.1 version and remove the extra NuGet assembly. This was also true for System.Security conflicts. The conflict with System.Net.Http was actually moved into a NuGet assembly called Microsoft.AspNet.WebApi.Client.
I hope all of this helps someone else...(uhhhgggg)....
BTW - it appears that when using VS2015 with .NET 4.7.1, these conflicts are suppressed and never display. This feels like a shortcoming of VS2015. Glad VS2017 shows them to reveal the true problem...
Check your packages.config file to make sure the Nuget packages are actually listed as dependencies for your project.
Also, open the solution in VS 2015 and double check the file path for the references in question. Make sure the DLLs are not referenced from a file path unique to Visual Studio 2015.
I had to create a new project and transfer all the necessary files from the old project to new project to fix this.

Trying to compile the Microsoft as-is provided .NET source code, getting hundreds of errors

I downloaded Microsoft .NET 4.5.1 from https://referencesource.microsoft.com/,
tried to build it in visual studio using the provided ndp.sln solution, and got about 600 errors and 400 warnings. Is this resource not actually for compilation? If so, is there a microsoft .NET source project that is compilable?
The reference source is not compilable (that's why it's called reference source); it's missing lots of internal resources and build steps.
You're looking for .Net Core, which is.

Visual Studio loses reference when attempting to build

I started using Thrift about a week ago. I got the latest source from SVN and built the C# library (Thrift.dll). I used the library to prototype a TCP client/server with the data types that my team will be using. This worked with no problem, I just followed the example code in the SVN repo.
Yesterday, I wanted to run the same test but over HTTP instead. I searched around online some and found out that from the time I got the code last week until yesterday patch "THRIFT-322" https://issues.apache.org/jira/browse/THRIFT-322
Was applied to the trunk. So I updated my thrift source from SVN and saw the new THttpHandler class. I rebuilt the Thrift.dll so I can use the THttpHandler class but now whenever I reference the new dll Visual Studio 2010 gives me an error at compile time: "The type or namespace "Thrift" could not be found (are you missing a using directive or an assembly reference?)". When I replace the reference with the old Thrift.dll that I have I do not get this error.
I can't figure out what I'm doing wrong, I just recompiled the Thrift.dll from the SVN source and I can no longer link to the Thrift.dll. When I first add the reference it seems like it's going to work fine. Visual Studio gives me the correct intellisense, etc. Once I try to build it's like there is no longer a reference to the Thrift.dll. Any ideas?
I also submitted this to the Thrift Users Mailing List, I just thought maybe this was more general and someone here may be able to help.
Thanks,
-Sean
Since you mentioned using VS 2010... I have seen this error/behavior sometimes when I am compiling a .NET 4.0 project and my target framework for the project is set to ".NET Framework 4.0 Client Profile" instead of ".NET Framework 4.0". So you might want to check that and see if it helps.
I had a similar problem I had to switch from '.NET Framework 4.0 Client Profile' to '.NET Framework 4.0' and it worked because a referenced dll is compiled using the '.NET Framework 4.0'
One Suggestion I have is to check for a file with 'refresh' in the extension E.g. Thrift.dll.refresh or something similar. This file is usually added on adding a refernce. Try removing this file, replace the dll and try builtign and running again.

Categories

Resources