Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 767
MStudley Avatar asked Jan 23 '17 04:01

MStudley


People also ask

Why is C not working in VS Code?

You have to change it so it runs in the terminal. Go to the menu Code > Preferences > Settings. In the User tab on the left panel, expand the Extensions section. Find and select Run Code Configuration.

How do I Debug a .NET core code in Visual Studio?

Open the Debug view by selecting the Debugging icon on the left side menu. Select the green arrow at the top of the pane, next to . NET Core Launch (console). Other ways to start the program in debugging mode are by pressing F5 or choosing Run > Start Debugging from the menu.

How do I fix VS Code not showing errors?

Navigate to View | Command Palette, enter and select C/C++ Build and debug active file: Select Project, and then select the correct project that you want to work with. This will help you see the problems as you create your code without requiring you to run it.

How do I create a .NET core application in Visual Studio Code?

You can run an ASP.NET Core application from Visual Studio Code directly. To accomplish this, open the Command Palette, type dnx and press Enter. Next, select dnx: Run Command and press Enter. Then click dnx web.


3 Answers

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.

like image 112
Shaun Luttin Avatar answered Oct 10 '22 21:10

Shaun Luttin


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.

like image 25
Daniel Martínez Sarta Avatar answered Oct 10 '22 20:10

Daniel Martínez Sarta


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.

like image 1
MStudley Avatar answered Oct 10 '22 22:10

MStudley