How to publish an executable with dependencies only? - c#

I would like to create an executable that is framework dependent and will only run on Win x64.
To that end, I execute dotnet publish -c Release -r win10-x64. I am expecting only my exe, dll and direct dependencies.
Instead, the command generates win10-x64 and win10-x64\publish. The win10-x64 folder only contains the app without any dependencies. The win10-x64\publish has 236 files including a ton of api-ms-win-core-*.dll files which seem to be native, not managed.
So how do I generate an executable with direct dependencies only?

From the dotnet publish documentation, take a look at what it says for the --self-contained parameter:
Publishes the .NET Core runtime with your application so the runtime doesn't need to be installed on the target machine. If a runtime identifier is specified, its default value is true.
So since you are specifying a runtime identifier with the -r parameter, it is creating a self contained app by default. Try adding --self-contained:false to your command and see if that produces what you expect.

Related

How to Compile a C# Project for Use on Linux?

I downloaded a C# project from github and compiled it with Visual Studio using the dotnet build command and apparently everything went well. But I can't use it on Centos, not even using Mono . Which command should I use to compile a C# project in VisualStudio for use on Linux? I used dotnet build which generated me a project with .exe file and I tried to run it with Mono and I got this error File does not contain a valid CIL image.
sorry if this is the wrong area to ask
There are different ways to do it and they depend on what you're trying to achieve.
From .NET application publishing overview
Type
Command
framework-dependent executable for the current platform.
dotnet publish
framework-dependent executable for a specific platform.
dotnet publish -r --self-contained false
framework-dependent cross-platform binary.
dotnet publish
self-contained executable.
dotnet publish -r
From the Examples section:
Publish an app cross-platform framework-dependent. A Linux 64-bit executable is created along with the dll file. This command doesn't work with .NET Core SDK 2.1.
dotnet publish -r linux-x64 --self-contained false
You may be interested in Single-file deployment and executable:
Bundling all application-dependent files into a single binary provides an application developer with the attractive option to deploy and distribute the application as a single file. Single-file deployment is available for both the framework-dependent deployment model and self-contained applications.

Is it possible to publish a stand alone c# application to Linux?

I am trying to publish a C# .net core console app I made, to a Linux machine running Centos 7 64 bit. Currently, I am publishing the app using the command:dotnet publish -c release -r contos.7-x64. Unfortunately, it appears as though publishing in this way requires .net core to be installed on the target machine. Is it possible to do this where I do not need to install anything on the machine that I am publishing to?
In .NET Core 2.2 you can create a self-contained app (i.e. not requiring .NET Core runtime installed on host machine) with this command dotnet publish -r centos.7-x64 -c Release --self-contained. It'll produce executable and a lot of dependencies.
In .NET Core 3 you can compress all dependencies into a single file dotnet publish -r centos.7-x64 -c Release /p:PublishSingleFile=true. You can also add flag /p:PublishTrimmed=true to reduce executable size by tree trimming.
More details can be found here and here.
I found that the issue was that the project was not compiling to core 2. Rather, the project was compiling to version 1. After making this change, the published project was able to run on Centos.

How to run .NET core application by double clicking a file like exe in .NET framework

In .NET framework when we build our solution/project, we get an exe in the debug/release folder and we can run our application by double clicking that exe. But how can we do this in .NET core, i know we can't have an exe in .NET core because it is windows specific. When i build my .NET core application i get the following files in the debug folder.
I know how to run this application using command prompt but i want to have a file like exe by clicking which i can run my application.
You can generate exe (self-contained application).
.NET Core 2.0 +
Open Package Manager Console or any other console in your project directory and type:
dotnet publish -c Release -r win10-x64
dotnet publish -c Release -r ubuntu.16.10-x64
When you use previous version of .NET Core you have to add runtime identifier of the target environment in csproj:
<PropertyGroup>
<RuntimeIdentifiers>win10-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>
Runtime Identifiers (RIDs) list:
https://learn.microsoft.com/en-us/dotnet/core/rid-catalog
One simple solution is to create a .bat file with runs the dll via the dotnet cli, then you get "a file like exe"
dotnet ConsoleApp2.dll

.Net core application cannot run on Linux?

I created a .Net core C# console application in Visual Studio and used the following steps to test it on Linux.
Use Visual Studio "Build -> Publish" menu item to create the executable files in ....\bin\Release\netcoreapp2.1\publish.
Copy the "publish" directory to the Linux machine
On Linux, chmod 777 myApp.dll
./myApp.dll
However, executing the app shows the error of
-bash: ./myApp.dll: cannot execute binary file
It looks like you did a Framework-Dependendent Deployment. Essentially, the publish command was:
dotnet publish -c Release
FDD assumes that you are going to have a .NET Core runtime to run your application on the target platform.
Once you copied over the publish directory to another machine (which could be Linux, macOS or Windows), your application still needs a .NET Core runtime to run your application.
Installing the .NET Core runtime depends on the particular Linux distribution that you are using. Once you have it installed, you can run your application by doing:
dotnet /path/to/publish/myApp.dll
An alternative to Framework Dependent Deployment is Self-Contained Deployment. In this mode, the published application will contain your application as well as a copy of the .NET Core runtime. On command line, doing a a SCD publish looks like this:
dotnet publish -r linux-x64 -c Release
For doing this in Visual Studio, see the link above. Then, you should see a bin\Release\netcoreapp2.1\linux-x64\publish\ directory that contains a myApp file. You can copy over this publish dir over to a Linux distribution and just run:
/path/to/linux-x64/publish/myApp

How to publish only required dependencies?

If I create a "Hello World" .NET Core C# Console application in Visual Studio 2017 and run
dotnet publish -c Release -r win10-x64 --self-contained
The resulting publish folder has 215 files in it, totals 62MB and includes the whole of .NET, which the application doesn't use. For example, it has System.Security.Cryptography.OpenSsl.dll.
This is part of the "Microsoft.NETCore.App" dependency which I seem to have no way to edit manually. How can I trim that down to what the application is actually using?
Per the deployment documentation:
Unlike FDD, a self-contained deployment (SCD) doesn't rely on the presence of shared components on the target system. All components, including both the .NET Core libraries and the .NET Core runtime, are included with the application
(emphasis mine)
If you don't want to deploy the whole .NET Core runtime along with your application, then you should use a Framework-dependent Deployment (FDD) instead of a Self-contained Deployment (SCD).
dotnet publish -c Release
In the future, the CoreRT runtime – which is still under development at the time of writing – aims to allow creating a single pre-compiled native executable that is specific to a runtime and does not require any other files.
Reference: Is there a way to make a console application run using only a single file in .NET Core?
There is a 3rd option as well: "Framework-dependent executables (FDE)"
You need to use --self-contained false e.g.:
dotnet publish <xyz.csproj> -c Release -r win-x64 --self-contained false
The 'Publish' folder still contains some Microsoft.*.dlls. However way less. In my case previous publish was folder size was 84MB now it is 12MB only!

Categories