Error in Visual Studio Code Dotnet Core C#: "The type or namespace name 'System' could not be found", but build succeeds

C#WindowsVisual Studio-Code.Net CoreOmnisharp

C# Problem Overview


When trying to work with Visual Studio Code on a C# DotNet Core MVC application, I am having a lot of trouble getting visual studio code to work. It is having trouble finding anything related to C#, marking even 'Using System;' as invalid, saying it can't find it.

However, when I run a Dotnet build, it succeeds with no warnings or errors and the project runs.

My project.json:

 {
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.1.0",
      "type": "platform"
    },
    "Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
    "Microsoft.EntityFrameworkCore.Design": {
      "version": "1.1.0",
      "type": "build"
    },
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0"
  },
  
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

Any ideas? I'm really pulling my hair out with this one.

C# Solutions


Solution 1 - C#

Sometimes C# for Visual Studio Code (powered by OmniSharp) becomes confused.

Try restarting OmniSharp. Here are two ways:

  1. Close and re-open Visual Studio Code, or
  2. 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.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

If you get this error, you could be missing a package.

> “The type or namespace name 'System' could not be found”

To add a package, you can run this command in the terminal:

dotnet add package

Example: Add Newtonsoft.Json NuGet package to a project:

dotnet add package Newtonsoft.Json

After that, go to the squiggly line, and add the missing reference by clicking on the lightbulb.

You can also install the Nuget Package Manager extension to find out what packages you need. See this answer to find out more:

https://stackoverflow.com/questions/40675162/install-a-nuget-package-in-visual-studio-code

Solution 5 - C#

I'm using VS Code in a mac with OmniSharp and mono and the issue was gone after doing the following:

For MacOS and Linux users who have Mono installed, this means you will need to set omnisharp.useGlobalMono to never until a version of Mono ships with MSBuild 16.7.

Solution 6 - C#

In my case vscode showed this error only for one file. The problem was solved by adding the missing file to the Assembly-CSharp.csproj I'm not sure when files are automatically added and when I do have to do it manually, but it solved this issue.

...
<ItemGroup>
    <Compile Include="Assets/Scripts/data/MissingFile.cs" />
...

Solution 7 - C#

Another possible cause of getting errors regarding "using System" is storing the .vscode directory in the git repository (my not adding it to .gitignore).

Opening the solution folder with VSCode can create cross version problems like the above and can be solved simply by deleting the .vscode directory.

Solution 8 - C#

This issue has been answered at: https://github.com/OmniSharp/omnisharp-vscode/issues/2147. (It's a long thread. Scroll all the way to the bottom.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMStudleyView Question on Stackoverflow
Solution 1 - C#Shaun LuttinView Answer on Stackoverflow
Solution 2 - C#Daniel Martínez SartaView Answer on Stackoverflow
Solution 3 - C#MStudleyView Answer on Stackoverflow
Solution 4 - C#live-loveView Answer on Stackoverflow
Solution 5 - C#DRovedaView Answer on Stackoverflow
Solution 6 - C#ChrisRobView Answer on Stackoverflow
Solution 7 - C#esj51View Answer on Stackoverflow
Solution 8 - C#Zlatko Michailov - MSFTView Answer on Stackoverflow