7

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?

6
  • 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 Commented Feb 16, 2018 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 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) Commented Feb 16, 2018 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. Commented Feb 16, 2018 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) Commented Feb 18, 2018 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 for .NET Core that has an .exe file and doesn't require dotnet.exe to run. Commented Feb 18, 2018 at 11:47

1 Answer 1

15

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.