NET Core Application Types
We can create two types of applications in .NET Core.
- Portable Application
- Self-contained application
Portable Application
Portable applications are applications which expect .NET Core runtime on the deployment machines. It cannot be run on a machine which does not have .NET Core runtime installed.
.NET Core Portable Application
Self-contained Application
Self-contained applications are applications which include .NET Core runtime when we publish it. It can run on a machine which does not have .NET Core runtime installed.
.NET Core Self-contained Application
Configure Application Type
We can configure ASP.NET Core application as portable or self-contained application using type property of Microsoft.NETCore.App dependency in project.json. The “type”:”platform” indicates that this application expects .NET Core on the machine. This makes it a portable application.
For the self-contained application, remove type-platform from the dependency. This makes it a self-contained application which means .NET Core will be included when you build and publish an application.
Code Sharing in .NET Core
With .NET Core, we can currently develop applications with three different .NET frameworks for different platforms. The traditional or standard .NET Framework is for Windows, Mono framework for iOS, OSx and Android and .NET Core for Windows, Mac and Linux.
These frameworks use different framework class libraries. It means code written in one framework cannot be used with other frameworks. For example, a console application developed with .NET Framework cannot run on .NET Core or vice-versa. Thus, code-sharing is not allowed.
It would be nice to write code once and share with other applications with different .NET frameworks. Isn’t it?
To solve this problem of code sharing, we can use the following three approaches:
- Create Portable Class Library
- Target Multiple Frameworks ASP.NET Core app
- Target .NET Standard
Creating portable class library to share code with other .NET frameworks is not a new thing in .NET. Learn about it here.
Learn about how to target multiple frameworks in ASP.NET Core application for code sharing in the next chapter.
Target Multiple Frameworks in .NET Core 2.x App
As mentioned in the previous chapter, creating a .NET Core application which targets multiple frameworks is one of the approaches for code sharing.
We can create .NET Core application and configure multiple target frameworks for it so that it can run with all the configured target frameworks. To demonstrate this, let’s create .NET Core 2.0 console application which can run with .NET Core as well as traditional .NET framework in Visual Studio 2017.
The first step is to create a new project in Visual Studio 2017 by clicking on File -> New Project.. This will open New Project popup as shown below.
Create .NET Core 2.x Console Application
In the New Project popup, select Console Application (.NET Core), provide the appropriate name and click OK. This will create new console project as shown below.
Now, we can configure multiple frameworks by editing .csproj file. So, right click on the project in solution explorer and select Edit <project-name>.csproj as shown below.
The .csproj will look like below.
.csproj:
Copy
<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>
Exe
</OutputType>
<TargetFramework>
netcoreapp2.0
</TargetFramework>
</PropertyGroup>
</Project>
As you can see above, <TargetFramework>
is netcoreapp2.0. It means currently this application can run on .NET Core 2.0 framework. We can include multiple monikers for multiple frameworks here, in order to target multiple frameworks.
To target multiple frameworks, change <TargetFramework>
to plural <TargetFrameworks>
and include monikers for different frameworks you want to target separated by ;
.
Here, we will support two more frameworks .NET Framework 4.0 & 4.6. So include net40 and net46 monikers respectively as shown below. Look at TFMs for all supported target frameworks here.
.csproj:
Copy
<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>
Exe
</OutputType>
<TargetFrameworks>
netcoreapp2.0;net45;net46
</TargetFrameworks>
</PropertyGroup>
</Project>
As soon as you save the above .csproj file, Visual Studio will load and include the references for .NET 4.5 and .NET 4.6 into Dependencies section as shown below.
Now, open program.cs and let’s add framework specific code using preprocessor conditions #if and #elif as shown below.
Program.cs
Copy
using System;
namespace MultiFrameworkConsole
{
public
class
Program
{
public
static
void
Main(
string[] args)
{
#if NET40
Console.WriteLine(
"Target framework: .NET Framework 4.0");
#elif NET45
Console.WriteLine(
"Target framework: .NET Framework 4.5");
#else
Console.WriteLine(
"Target framework: .NET Core 2.0");
#endif
Console.ReadKey();
}
}
}
As you can see above, to write framework specific code, use symbol with condition for .NET framework moniker and replace the dot with an underscore and change lowercase letters to uppercase.
To run the application for specific framework, click on the run dropdown and select a targeted framework as shown below.
Now, run the application and you will see the following output.
Framework Specific References
Sometimes you may need to include specific references for a particular framework. For example, .NET Core 2.0 meta package already includes System.Net
reference which is not included in .NET 4.0 and 4.5. So, we need to include it in .csproj file using conditional reference as shown below.
.csproj:
Copy
<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>
Exe
</OutputType>
<TargetFrameworks>
netcoreapp2.0;net45;net46
</TargetFrameworks>
</PropertyGroup>
<ItemGroupCondition=" '$(TargetFramework)' == 'net40' ">
<Reference
Include="System.Net"
/>
</ItemGroup>
<ItemGroup
Condition=" '$(TargetFramework)' == 'net45' ">
<Reference
Include="System.Net"
/>
</ItemGroup>
</Project>