Search code examples
visual-studionugetasp.net-core-2.0mstest

.NET Core MSTest project can't find VisualStudio type or namespace in Microsoft namespace


Microsoft Visual Studio Professional 2019 Version 16.0.0 VisualStudio.16.Release/16.0.0+28729.10

I added a .NET Core 2.2 MSTest project and it added dependencies :

  • Microsoft.NET.Test.Sdk v16.0.1 ( I updated to v16.2.0 )
  • Microsoft.NET.Test.Sdk v1.3.2 ( I updated to v1.4.0 )
  • MSTest.TestFramework v1.3.2 ( I updated to v1.4.0 )

I compile and get this error :

CS0234  The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) ...\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs

Microsoft.NET.Test.Sdk.Program.cs

// <auto-generated> This file has been auto generated. </auto-generated>
using System;
[Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode]
class AutoGeneratedProgram {static void Main(string[] args){}}

Couple of observations :

  1. Project is .NET Core 2.2 but it's using package from netcoreapp1.0 folder
  2. NuGet package manager successfully added package Microsoft.NET.Test.Sdk v16.0.1 but on build it has an unresolved reference

Solution

  • The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) ...\packages\microsoft.net.test.sdk\16.2.0\build\netcoreapp1.0\Microsoft.NET.Test.Sdk.Program.cs

    If we consume the Microsoft.NET.Test.Sdk package, when we compile and build our project, the Microsoft.NET.Test.Sdk.Program.cs will also be compiled by the csc.exe during the build.

    So the error message indicates the Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode attribute can't be recognized well because something is wrong with the reference to Microsoft.TestPlatform.ObjectModel.dll.

    Note:

    1.Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode comes from assembly Microsoft.TestPlatform.ObjectModel.dll.

    2.Microsoft.NET.Test.Sdk package depends on Microsoft.TestPlatform.TestHost package, and Microsoft.TestPlatform.TestHost package depends on Microsoft.TestPlatform.ObjectModel package.

    If those packages are installed successfully, the compile and build should work. So actually I think something is wrong in your installed packages.

    Some simple suggestions you can try:

    1.Go Tools=>Nuget Package Manager=>Package Manager settings to make sure two options of Package Restore are enabled. And then delete the bin, obj folders and run a rebuild.

    2.If #1 not works, try clean the nuget cache by UI in VS IDE, and then again delete the bin, obj folders, then run a rebuild.

    3.Please update your VS IDE to latest version, 16.0.0 is too old, and latest version has fixed some issues.

    Project is .NET Core 2.2 but it's using package from netcoreapp1.0 folder

    If you open the package in path ...\packages\microsoft.net.test.sdk\16.2.0\build you can find this package only have three version of assemblies, net40, netcoreapp1.0 and uap10.0. Since your project is .net core 2.2, it will reference the assembly in netcoreapp1.0 folder. It's expected behavior.