2

I have read many, many, many StackOverflow posts on this subject and many Internet articles but none answer this simple question:

Is it possible to build a single file macOS console application written in C# that depends upon .NET Core 3.1?

I am coding using Visual Studio (Community Edition) on macOS Catalina. I have the simplest test console app project created (in VS) using the Console Application template:

Template

Here is the code for the app:

using System;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

If I run this in Visual Studio it works.

If I build the app, set to Release:

Release Build

Then I get this in the output folder:

Output

I can run TestApp.dll from the Terminal using dotnet TestApp.dll. This obviously requires the end user to have the .NET framework installed on their computer (otherwise they can't call the dotnet tool).

I have tried publishing the project using the dotnet CLI to build a standalone app. I used this command:

dotnet publish -c Release --self-contained -r osx-x64

taken from this post.

Whilst this generates a TestApp binary, it also generates 192 other files like so:

Crazy amount of files

Is there any way that these DLLs can be bundled into the TestApp binary? Any solutions I have come across are either Windows only or are 5 years out of date (and no longer work).

I feel sure this must be a solved problem. I don't care if I have to build the app via the command line (if the Mac VS doesn't support it).

Garry Pettet
  • 8,096
  • 22
  • 65
  • 103
  • 3
    You are just one step away https://github.com/lextm/restructuredtext-antlr/blob/v1.0.1/dist.server.ps1#L6 – Lex Li Jun 13 '20 at 18:00
  • I think you've cracked it! 73MB for a Hello World app but at least it works :). Thank you. As a follow up question, is it possible to build a single file binary that includes any third party libraries in the binary but assumes .NET is installed on the user's end machine? That should significantly reduce the file size but not require a big fat binary. – Garry Pettet Jun 13 '20 at 19:05
  • 1) If you want a single file, then asking your end users to install a separate runtime is pretty bad experience for them. 2) To reduce size, enable trimming (which has side effects) https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#assembly-linking For whatever reason you shouldn't really care much about size. macOS apps are never small based on my personal experience. – Lex Li Jun 13 '20 at 20:23

0 Answers0