6

I understand targeting .NET Standard 2 means that the frameworks between .net core, mono and the full .net framework, but I'd like to understand what that means for the binaries created by the different frameworks.

So if I create a console program targeting .NET Standard 2 and compile using .NET Framework, can only the full .NET Framework run this file? If they can all run the files, how would I run the .NET Core .dll executable console using the full framework or Mono?

So basically are the binaries created by each framework (targeting .NET Standard) able to run using the other frameworks?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Mladen Mihajlovic
  • 6,095
  • 7
  • 40
  • 55
  • 3
    You *can't* create a .NET Standard console program. It's only meant for libraries. Executables have to target a runtime. Executables means console apps, web sites *and* unit test class libraries. Those executables can reference the same .NET Standard library – Panagiotis Kanavos Feb 16 '18 at 10:15
  • @PanagiotisKanavos wow I never even considered that - didn't see any info regarding a difference between classlib and executable. So far the closest info I can see is https://github.com/dotnet/corefx/issues/18217#issuecomment-293394403 and some answers there are interesting, but it's a but weird this isn't specified a bit more when explaining .net standard (I at least have not seen this anywhere) – Mladen Mihajlovic Feb 16 '18 at 11:14
  • 1
    Actually, you **can** create a .NET Standard console application, but it won't run because .NET Standard does not have a runtime. Which means you have to scratch your head until you've figured out what you have done wrong. – NightOwl888 Feb 16 '18 at 16:16
  • @NightOwl888 the thing is that I find it quite logical to be able to compile an exe as .net standard, but have to explicitly run it using a runtime eg: `dotnet exec.dll` but compiling it with .net standard does not complain but it will not run. Confusing if you ask me. Also kind of shows that it probably will be allowed in the future or was planned at some stage (re the issue I linked to earlier) – Mladen Mihajlovic Feb 18 '18 at 11:39
  • @MladenMihajlovic - No, that is not possible. The `dotnet` application runs on .NET Core. The assembly that it loads must be **executable** on .NET Core - it must target .NET Core (and can depend on .NET Standard DLLs). You can also create a [Self-contained deployment](https://learn.microsoft.com/en-us/dotnet/core/deploying/#self-contained-deployments-scd) for .NET Core that has an `.exe` file and doesn't require `dotnet.exe` to run. – NightOwl888 Feb 18 '18 at 11:47
  • @MladenMihajlovic - The part that I think is confusing is that .NET Standard is often advertised as being "closely related to .NET Core". It is not. .NET Core is a runtime. .NET Standard is a portability platform that can work with multiple runtimes, including .NET Core. The features of .NET Core (such as being executable via `dotnet.exe`) have nothing to do with .NET Standard. – NightOwl888 Feb 18 '18 at 11:52

1 Answers1

14

I believe the Comparison to Portable Class Libraries section probably says it best:

.NET Standard is the replacement for Portable Class Libraries (PCL).

.NET Standard is not itself a runtime it is a type forwarding mechanism for multiple different runtimes. Therefore, it is not possible to create anything but a non-executable class library in .NET Standard, just as was the case with PCLs.

This enables the class library to be consumed by executable assemblies that target specific runtimes (.NET Framework, .NET Core, Xamarin.iOS, Mono, etc).

It is helpful to think of this in terms of classes and interfaces. In pseudo-code, .NET Standard is an interface that .NET Framework and .NET Core implement.

public interface INetStandard
{
    // Only has API definitions
}

public class NetFramework : INetStandard
{
    // .NET Framework Runtime implemented here
}

public class NetCore : INetStandard
{
    // .NET Core Runtime implemented here
}

This makes it possible to use .NET Standard with either .NET Framework or .NET Core, but .NET Standard itself has no runtime, only a set of APIs that can be shared between runtimes. You can target any one of the three with your project, but you can't execute .NET Standard any more than you can instantiate an interface.

Unfortunately, you are not the first to have asked about this and unless Microsoft makes the documentation more clear that .NET Standard does not actually execute, you likely won't be the last.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212