. net core application types (portable apps & self contained apps)

Time:2021-11-19

introduce

There are many ways to consider the type of application to build. Usually, the type is used to describe a specific execution model or an application based on it. For example: console application, web application, etc. All these types of applications can be created with. Net core Net core itself is a general development platform. However, to give. Net core cross platform features,. Net core still needs a corner to explore the types of applications, which is called application’s portability. Portability essentially means where your application can run and what prerequisites need to be met to run on a particular machine. Next, we will describe the portable types of the two main. Net cores.

Portable applications

Portable application is the default application type of. Net core. This requires that the. Net core be pre installed on the target machine where the program runs. This means that as a developer, your program is portable between different. Net core devices. This type of application only needs to carry and deploy its own code and dependencies (. Outside the net core library). In order to create a portable application, all you need to do is set the class library of the current. Net core in project.json, and then change the frameworks to the following:


"dependencies": {
 "Microsoft.NETCore.App": { 
  "version": "1.0.0",
  "type": "platform"
 }
},
"frameworks": {
 "netcoreapp1.0": {}
}

[Microsoft. NETCORE. App] is a “metadata package” that shows you your target. Net core class library. The [type: platform] attribute in the dependency means that when publishing, the publishing tool will omit publishing these dependent. Net core class library files, because these dependent class library files have been installed on the target server with the. Net core.

Portable applications using native dependencies

Portable applications that use native dependencies are a subset of the above portable applications. These portable applications have some native dependencies specified in specific places on the dependency chain, so that these native dependencies can run directly on the target platforms, and our portable programs can also run directly on these platforms. The most typical example is our kestrel server (asp.net cross platform web server), which is built based onlibuv(native dependency). When you publish a portable application with native dependencies, all publishing outputs are consistent with the above. As for native dependencies, the publishing output will generate a folder for each rid (runtime identifier). The following project.json file shows a portable application using native dependencies:


"dependencies": {
 "Microsoft.NETCore.App": { 
  "version": "1.0.0",
  "type": "platform"
 },
 "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-*"
},
"frameworks": {
 "netcoreapp1.0": {}
}

Self-contained applications

Unlike portable applications, stand-alone applications do not rely on any shared components on the target machine where you are ready to deploy the application. As its name implies, it means that the whole dependency is closed-loop, and the runtime will be released with the whole program. This will make the whole distribution package change a little more, but it also allows the program to run on any platform supported by the. Net core using the correct native dependencies (regardless of whether the. Net core has been installed on the target server in advance). This makes it easier to deploy your application to the target server. Because the launch of the application now carries the runtime itself, you must specify which platforms your application will run on in advance. For example, if you want to release an independent program to windows 10, but you are not ready to run to Mac OS and Linux, you must add or remove some platforms during development. There are many steps to complete a stand-alone program, but the first step needs to delete any “type”: “platform”

Properties. The second step is to retain theMicrosoft.NETCore.AppIt will pull in some of the remaining things we need. Finally, we need to add a runtime node in project. JSON to indicate which platforms our program will run on.


"dependencies": {
 "Microsoft.NETCore.App": "1.0.0"
},
"frameworks": {
 "netcoreapp1.0": {}
},
"runtimes": {
 "win10-x64": {},
 "osx.10.11-x64": {}
}

When we restore the project, if the project.json contains the runtimes node, nuget will restore all the specified runtimes. Next, when we need to publish an application for a given platform, we use the following command line:

dotnet publish –runtime <RID>

Rid must be the rid specified in project.json, such as win10-x64 or osx.10.11-x64, otherwise an error will be reported. If the operating system you are about to publish to is the operating system of your current. Net core, you can directly use the following command:

dotnet publish

However, you still need to specify the corresponding rid in project. JSON.

But it should be noted that: within the time limit of. Net core tools preview 2, independent applications are published from the local nuget cache, which means that all dependencies of the application, including. Net core runtime and class library, are not optimized. Therefore, the overall performance of independent applications is lower than that of portable applications. The reason is that the. Net core runtime and class library relied on by portable applications can be run at any time.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.