How to compile mono projects from command line on mac - c#

I'm required to compile a C# project from the command line (not from within the IDE). It works just fine under linux (to be exact under Fedora and Ubuntu). I just install the mono-xbuild package and the build script is capable of executing the xbuild command. The problem is that there's no xbuild or mdtool inside the home-brew repositories and installing xamarin studio while building isn't the solution because it places the binary in different places depending on a whole range of conditions.
How do I get my build/install script to work out of the box under MacOSX?

You need to install .NET for macOS, then use a dotnet command to run compile and execute a .NET project.
Here are the commands to run a Hello World's app:
dotnet new console -o myApp
cd myApp
dotnet run
For Mono apps, you need to install Mono framework (available as a Mac .pkg file)
Consider also installing MSBuild (The Microsoft Build Engine). It requires OpenSSL (install via: brew install openssl). See: Building Testing and Debugging on .Net Core MSBuild.

Related

Trying to install MonoGame for mac and VS Code

I'm trying to install MonoGame for mac and VS code. I followed the instructions at https://docs.monogame.net/articles/getting_started/1_setting_up_your_development_environment_macos.html, but I am stuck at one place. I have the monogame editor .mpack file, but can't upload it to VS code for some reason. I did extensive research but can't find any answers. What should I do?
The "normal" Mac install instructions and files are only for use with Visual Studio.
Install using CLI
Please note these instructions are designed for Intel Macs(x64), but should work on Apple Silicon devices(ARM) with .Net6.
You can roughly follow the Linux install instructions.
Install Dot Net Core 3.1 for the Mac. or .Net6
Install VS Code.
You may need to reference this question on Terminal paths for code. Something similar may need to be done below for the dotnet command.
a. Install the C# extensions: code --install-extension ms-dotnettools.csharp
(Optional) Install Mono. Required for some consoles and Android targets(Frameworks 4.5 and 4.7). See here for compatability.
Open a terminal window. Run the following line, either the OS or from VS Code:
dotnet new --install MonoGame.Templates.CSharp
The next two lines may be Linux specific so ignore any uncorrectable errors.
dotnet tool install --global dotnet-mgcb-editor
mgcb-editor --register
At this point, you should be able to create a new CLI project folder from the templates:
cd /path/togame/parent
dotnet new mgdesktopgl -o MyGame
Open the folder in VS Code.
Review the projectname.csproj file:
Note the TargetFramework line:
<TargetFramework>netcoreapp3.1</TargetFramework> or
<TargetFramework>net6.0</TargetFramework>
Some targets require a different TargetFramework, like net47 or net45 as provided by Mono.
See Microsoft's .Net versioning page for more information.
A couple of additional helpful CLI commands to run from the folder with the .proj file; Taken from this list:
dotnet restore Update all NuGet Packages
dotnet clean Remove all output files
dotnet build compile
dotnet run execute the program

How to install .NET Core on aarch64 architecture Debian

I'm new to Linux and want to code some C# on Linux for school. The problem is I don't know how to install C#. I already read the instructions from Microsoft, but I couldn't get it to work.
'arm64' is the Debian port name for the 64-bit Armv8 architecture, referred to as 'aarch64' in upstream toolchains. The snapd daemon and tooling that enables snap packages is available for arm64 architecture in Debian.
To install snapd:
sudo apt update
sudo apt install snapd
Either log out and back in again or restart your system to ensure that snap's paths are updated correctly. After this install the core snap in order to get the latest snapd:
sudo snap install core
C# is open source and cross platform now that Microsoft has released a version of .NET Core. To install .NET Core in Debian open the terminal and type:
sudo snap install dotnet-sdk --classic
sudo snap alias dotnet-sdk.dotnet dotnet # to run dotnet-sdk type dotnet
The instructions from Microsoft seem to assume that you are running Visual Studio Code on Windows, so they frequently don't work at all on Linux. The following instructions were tested on a Debian-based system (Ubuntu 20.04). I ran a few C# console apps for test purposes, but let's start with a simple one-line C# console app. Run these commands to build and run an example C# Hello World console app from the terminal:
cd ~
mkdir C#_Projects
cd C#_Projects
mkdir HelloWorld
cd HelloWorld
dotnet new console
dotnet build --output ./build_output
dotnet ./build_output/HelloWorld.dll
Results of dotnet ./build_output/HelloWorld.dll
Hello, World!

Dotnet commands not working even after installing .NET core SDK

I am trying to setup visual studio code for c# on Manjaro Linux
I have installed VSC and .NET core 5.0 SDK through the package manager.
The c# add-on is installed in VSC
With all of these installed correctly, why can't I use the dotnet command in the terminal?
Every time I try I get this error; sh: dotnet: command not found
By default, dotnet is installed to usr/share/dotnet. Check if this path is added to the env var $PATH.
If you can't find it there, you could run on a terminal:
whereis dotnet
If no dotnet is found, the tool wasn't properly installed.
Also, did you close and reopen the terminal window after installing the SDK, as the instructions stated?

How to run an ASP.NET Web Application (.NET Framework) outside IDE?

I used to make REST APIs using the "ASP.NET Core Web Application" template in Visual Studio 2019, but I don't want to have to install .NET Core to use my server application, so I used the .NET Framework template.
However, the former always created an executable file for me to run even outside the IDE. The server would run in a console window. The .NET Framework template however only generates the assembly as a DLL. There is no use in changing to "Console Application" in project settings since the template has no Main() function.
How would I make my server run outside the IDE? I'd love to know how to get the server started from a Main() function. I could then implement a ServiceBase class to turn my program into a service, which is ultimately what I want.
I know that there is a way to "deploy" the server to run with issexpress, but I'd rather have it run itself from an executable.
dotnet run
.NET Core produces a DLL that can be run using dotnet run. For example:
dotnet run MyProgram.dll
Please note: You need the .NET Core runtimes installed if you use this method.
dotnet publish
You can get an EXE stub that will do this all for you by publishing your project. For example:
dotnet publish MyProgram.sln -c Release -r win-x64
That will create a release-mode Windows x64 compilation that contains a file named MyProgram.exe that you can execute normally.
Please note: You do not need the .NET Core runtimes installed if you use this method.
single-file publish
You can even make it easier to distribute by compiling to a single file:
dotnet publish MyProgram.sln -r win-x64 -p:PublishSingleFile=true --self-contained true
That will produce one file: MyProgram.exe.
Please note: You do not need the .NET Core runtimes installed if you use this method.

.NET core CLI -> run ERROR

I have a little bit of problem with dotnet core CLI.
When I am in a project directory and type:
dotnet restore
dotnet publish
It creates published version of my code. And it says that my project is in **PROJECT NAME** -> /Users/**NAME**/hwapp/bin/Debug/netcoreapp1.1/hwapp.dll.
After typing dotnet run it runs my code but when I step to that directory with my dll file compiled and run that dll with dotnet run command i get the following error.
Can some please explain me what I am doing wrong?
dotnet run is a development tool meant to run msbuild projects (e.g. csproj), not execute built applications.
Use dotnet hwapp.dll to run a built application.

Categories

Resources