18

I've just created a fresh project using dotnet new web. My Google-foo may be failing me, but I didn't find anything relating to my answer (please link to another SO answer, or relevant documentation if I've missed something obvious).

If I want to ensure this new project is .NET Standard 2.0 compliant, what do I now do?

Geesh_SO
  • 2,156
  • 5
  • 31
  • 58
  • you want to change the .NET Standard framework in which is you created web apps using .Net Core 2.0 ? – Santhakumar Munuswamy Oct 07 '17 at 14:19
  • I may be misunderstand something. I thought .NET Standard was just a "contract". So if I created a .NET 4.5 Framework, it could contain Windows-only references such as methods that deal with the registry, thus making it not .NET Standard compliant. Similarly, if I create a Xamarin app, it could contain GPS standard library calls that make it not compliant with .NET Standard. I was under the impression I had to do something more to "lock it into" the contract of .NET Standard. – Geesh_SO Oct 07 '17 at 14:23
  • .NET Standard is for platform vendors (like Unity, Xamarin, and Mono), and NuGet package authors mainly. If you are simply an end user, use the target platform directly, and forget about "compatibility". https://blog.lextudio.com/which-class-library-project-to-go-in-visual-studio-2015-2017-a48710cf3dff – Lex Li Oct 07 '17 at 14:27
  • .Net Standard is a set of APIs that have to be implemented by any .Net framework or .Net core implementations. That way you can have any .Net framework or .Net core applications can use any .Net standard dll. – jetstream96 Oct 07 '17 at 15:11
  • https://stackoverflow.com/questions/42939454/what-is-the-difference-between-net-core-and-net-standard-class-library-project – jetstream96 Oct 07 '17 at 15:11
  • You have created an application and applications have to target `netcoreappx.y` or `net4xy` or both (two binaries will be generated for each target). `netstandardx.y` is for class libraries only. When you create class libraries, then you should target `netstandardx.y`, but for applications you need to decide for one. `netcoreappx.y` targets a specific platform/runtime and decides if your app runs as .NET Core or on .NET Framework – Tseng Oct 07 '17 at 15:47
  • @LexLi That may be somewhat true, but with platforms like Azure and deployment to devices through Xamarin it's more important than ever that your code has maximum compatibility. For instance with Azure Functions the project it creates is .NET Standard so you MUST reference .NET Standard DLLs - even though Azure Functions actually runs on .NET Core. – Simon_Weaver Aug 23 '18 at 03:22
  • Azure Function is a very very special case, which I rather not discuss in details. Microsoft asks you to reference only .NET Standard assemblies, so that it can possible run your function code with any runtime (.NET Core is just one of the possibilities, and they might use a completely different thing under the hood to cut the cost). I left that comment one year ago, because most developers should be well aware of the target platform(s) and they can fully explore the maximum platform API surface. It only makes sense to target .NET Standard when you really have to. – Lex Li Aug 23 '18 at 04:01

2 Answers2

17

It is not inherently possible to run a netstandard project as an executable. Since netstandard was designed to be used for libraries.

In order to develop your web application entirely in netstandard2.0, you would have to create a separate project that targets either .NET Core or .NET Framework to execute your library that contains your web app (developed using .NET Standard).

1. Executable Project (ex: console app)
   -- Target Framework: netcoreapp2.0 / net462

2. Web Application Project (library)
   -- Target Framework: netstandard2.0

You can use the following steps to change the target framework of your project.

Step 1. Target the desired framework

  1. Right-click on your project and select Edit *****.csproj

  2. In the .csproj file, you need to replace the target framework to the .NET Framework.

Example .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web"> //<-- note the .Web for the web template
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

For a list of the Target Framework Moniker (TFM) (ie, net47, netstandard2.0, netcoreapp2.0, etc.*) you can check this link out: https://learn.microsoft.com/en-us/dotnet/standard/frameworks

Step 2. Run dotnet restore

Go to your output window and run dotnet restore.

Note: Sometimes Visual Studio may misbehave (depending on which update you have installed), so you may have to close and re-open your Visual Studio. Otherwise, sometimes a clean/re-build may do the trick.


Targeting both frameworks

You can pick one or the other, or even target both frameworks.

<TargetFrameworks>netcoreapp2.0; net47</TargetFrameworks> //<-- note the plural form!
Svek
  • 12,350
  • 6
  • 38
  • 69
  • `netstandardx.y` is for class libraries. They typically don't have an entry point and do not produce an executable file. The OP obviously wants to create an application (that's what `dotnet new web` does). If he were to create a class library he'd used `dotnet new classlib` instead – Tseng Oct 07 '17 at 15:49
  • @Tseng -- although you could technically create a basic console project and reference a `netstandard` library that has the entire code for the webhost... – Svek Oct 07 '17 at 16:00
  • `dotnet new web` already creates an console application, that's why it targets `netcorex.y` in the first place. You can modularize the application (having controllers/Views split between multiple PCLs), but the main application still has to be `netcorex.y` or `net4xy` and this is where you also configure the Dependency Injection and where the `Startup.cs` resides. It makes little sense to make the whole application as PCL and then have a thin bootstrapping from this PCL, since the new csproj tooling allows to target multiple platforms from within the same project (unlike in the past) – Tseng Oct 07 '17 at 16:06
  • If in doubt create a new project with what you want to target, and compare the csproj file created with yours. These instructions worked great for me, but MS never seems to be able to stop changing things! – Simon_Weaver Aug 23 '18 at 09:55
13

NET Standard is for class libraries. Applications must target netcoreapp* where * is a version number. The following shows the compatibility matrix: https://learn.microsoft.com/en-us/dotnet/standard/net-standard

For example, .NET Core 2 can consume .NET Standard version 2 and below.

Matt H
  • 7,311
  • 5
  • 45
  • 54