无法构建.Net core 2.2项目?找不到类型或名称空间名称“系统”

时间:2019-07-12 21:46:53

标签: c# .net .net-core

此刻我只安装了最新的.Net内核。

PS C:\temp> dotnet --version
2.2.301

然后我创建了一个新的控制台项目。

PS C:\temp> dotnet new console

  

欢迎使用.NET Core!

     

详细了解.NET Core:https://aka.ms/dotnet-docs   使用'dotnet --help'查看可用命令或访问:https://aka.ms/dotnet-cli-docs

     

遥测

     

.NET Core工具收集使用情况数据,以帮助我们改善您的体验。数据是匿名的,不包含命令行参数。数据由Microsoft收集并与社区共享。您可以通过使用自己喜欢的shell将DOTNET_CLI_TELEMETRY_OPTOUT环境变量设置为“ 1”或“ true”来退出遥测。

     

了解有关.NET Core CLI工具遥测的更多信息:https://aka.ms/dotnet-cli-telemetry

     

ASP.NET Core

     

成功安装了ASP.NET Core HTTPS开发证书。   要信任证书,请运行“ dotnet dev-certs https --trust”(仅限Windows和macOS)。要在其他平台上建立信任,请参阅平台特定的文档。   有关配置HTTPS的更多信息,请参见https://go.microsoft.com/fwlink/?linkid=848054。   准备...   模板“控制台应用程序”已成功创建。

     

正在处理创建后的操作...   在C:\ temp \ temp.csproj上运行'dotnet restore'...     C:\ temp \ temp.csproj在190.58毫秒内完成还原。

     

还原成功。


但是,dotnet run遇到以下错误?

PS C:\temp> dotnet run
C:\Users\wangyi\AppData\Local\Temp\1\.NETCoreApp,Version=v2.2.AssemblyAttributes.cs(4,20): error CS0400: The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(10,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(11,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(12,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(13,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(14,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(15,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(16,12): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
C:\Users\wangyi\AppData\Local\Temp\1\.NETCoreApp,Version=v2.2.AssemblyAttributes.cs(4,71): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
C:\Users\wangyi\AppData\Local\Temp\1\.NETCoreApp,Version=v2.2.AssemblyAttributes.cs(4,99): error CS0246: The type or namespace name 'FrameworkDisplayName' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
C:\Users\wangyi\AppData\Local\Temp\1\.NETCoreApp,Version=v2.2.AssemblyAttributes.cs(4,122): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(10,55): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(11,61): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(12,59): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(13,68): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(14,55): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(15,53): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(16,55): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
Program.cs(1,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
C:\Users\wangyi\AppData\Local\Temp\1\.NETCoreApp,Version=v2.2.AssemblyAttributes.cs(2,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
C:\Users\wangyi\AppData\Local\Temp\1\.NETCoreApp,Version=v2.2.AssemblyAttributes.cs(3,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(7,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
obj\Debug\netcoreapp2.2\temp.AssemblyInfo.cs(8,7): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?) [C:\temp\temp.csproj]
Program.cs(5,11): error CS0518: Predefined type 'System.Object' is not defined or imported [C:\temp\temp.csproj]
Program.cs(7,26): error CS0518: Predefined type 'System.String' is not defined or imported [C:\temp\temp.csproj]
Program.cs(7,16): error CS0518: Predefined type 'System.Void' is not defined or imported [C:\temp\temp.csproj]

The build failed. Please fix the build errors and run again.

运行dotnet restore并没有出现任何错误。

PS C:\temp> dotnet restore
Restore completed in 41.15 ms for C:\temp\temp.csproj.

我尝试“删除C:\ Program Files \ dotnet \ sdk \ NuGetFallbackFolder”(https://github.com/dotnet/core/issues/1006),但仍然无法解决问题。

但是,在Visual Studio中,我将项目的.net核心版本切换为 2.1 后,该项目才能编译并运行。

3 个答案:

答案 0 :(得分:0)

尝试运行:

dotnet restore

然后它应该起作用,或者至少为您提供有关无法解决依赖关系的原因的信息。

答案 1 :(得分:0)

在命令提示符下,您可以编写:dotnet --version,您将看到当前的版本。我建议您从here下载核心框架,而不要通过Visual Studio安装程序进行安装。从该站点安装.net core可以解决我的问题。

答案 2 :(得分:0)

今天问题解决了。我尝试安装最新的.Net核心SDK,但仍然无法解决问题。

然后,我启动了VS 2019,并在VS中创建了一个新的.Net核心项目。并编译。我回到Powershell,发现dotnet run现在可以工作了。