Sean
Sean

Reputation: 75

Referencing a .Net Core 3.1 project from a .Net Standard Class Library

So I have a .NET Core 3.1 project. I am trying to create a unit testing project to go along with it. I have created a .NET Standard 2.1 Class Library for this purpose. I'm then trying to add a reference to the main project, in order to be able to reference code from there. This isn't working, as I get the following compilation errors:

'..\MyProj\MyProj.csproj' targets 'netcoreapp3.1'. It cannot be referenced by a project that targets '.NETStandard,Version=v2.1'. Project MyProj is not compatible with netstandard2.1 (.NETStandard,Version=v2.1). Project MyProj supports: netcoreapp3.1 (.NETCoreApp,Version=v3.1) Test (test\Test)

So this does make sense to me, as I understand why .NET Core wouldn't be able to reference a .NET Standard project.
But then how can I create a Unit Testing project? Is it possible to create a .NET Core 3.1 Class Library? From everything I have read, it seems like that doesn't exist.

How can I set up a unit test project that references the main project?

Upvotes: 0

Views: 10254

Answers (2)

Cameron Tinker
Cameron Tinker

Reputation: 9789

.NET Standard was created as an interface for different .NET implementations. For example, this allows you to share code between .NET Core and the full .NET framework. I would create your library as a .NET Standard 2.0 project and then you can reference it in your .NET Core 3.1 application and your unit testing project.

I only suggest .NET Standard as it is generally more portable than .NET Framework or .NET Core libraries. You'd be able to target more platforms such as Xamarin, desktop, or web with .NET Standard.

Upvotes: 4

Ilya Chernomordik
Ilya Chernomordik

Reputation: 30225

I think you just need to use .Net Core 3.1 in your project, not NetStandard. I use it this way and there is no troubles. I guess you don't really need your unit testing to be portable?

Upvotes: 0

Related Questions