Error in Visual Studio Code Dotnet Core C#: "The type or namespace name 'System' could not be found", but build succeeds
Sometimes C# for Visual Studio Code (powered by OmniSharp) becomes confused.
Try restarting OmniSharp. Here are two ways:
- Close and re-open Visual Studio Code, or
- Open the Command Pallet and type
Restart Omnisharp
.
OmniSharp tends to become confused if we restore dependencies from the command line instead of from within Visual Studio Code.
In VS Code on Fedora 30 with .NET Core 3.0 I had the same issue after create a worker project with dotnet new worker
First issue was that OmniSharp server didn't find the Sdks folder and the solution was include this line to the ~/.bashrc:
export MSBuildSDKsPath="/usr/share/dotnet/sdk/$(dotnet --version)/Sdks"
then restart VS Code, but C# extension show me some messages like:
The type or namespace name 'Collections' does not exist in the namespace 'System' (are you missing an assembly reference?)
the solution was, first, in the terminal run:
dotnet build
then restart the OmniSharp server using the command palette (Ctrl+Shift+P):
OmniSharp: Restart OmniSharp
then I restart VS Code and the C# extensions and all dependencies are working fine.
Ok, I've figured out what was causing the issue. I was referencing the wrong imports for the framework part of the project.json file.
This:
"frameworks": { "netcoreapp1.1": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.1.0" } }, "imports": "dnxcore50" } }
Should be this:
"frameworks": { "netcoreapp1.1": { "imports": [ "dotnet5.6", "portable-net45+win8" ] } }
I'm on a windows 8 machine, and for some reason "dnxcore50" isn't valid, but "dotnet5.6" and "portable-net45+win8" is. I'm going to keep looking at the why for this question, but I am posting this answer now in case someone else is dealing with this problem.