Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a .NET app in Linux in 2015

Tags:

.net

linux

mono

TL;DR : what are the options nowadays to run a VS2015 solution on Linux?


I have a .NET application that runs fine in Windows and have received requests to port it to Linux. I have been trying very hard to understand what it meant today in 2015 but I am really confused with all the old posts pointing to Mono and the new posts talking about vNext (and still mention Mono). Also I have an okay background with .NET but I don't know a lot about Linux in general...

I followed a recent tutorial and managed to compile and run a Hello World console app in Linux. That required some installation of stuff I don't know, including Mono, and then running some "k" commands (kpm restore, kpm build, k run). However now I don't know how to move on to my real-world case scenario.

I have a VS2015 solution which has a lot of projects but only 4 are worth mentioning here: - CommonStuff - ServiceCore - ServiceWindows / ServiceConsole

All the code is in the ServiceCore project (class library), which itself has a reference to CommonStuff (another class library). ServiceWindows is a Windows Service project that simply calls the Main function of ServiceCore, and ServiceConsole does the same thing but in a console app (for debugging reasons, since it's not possible to debug a Windows Service project from Visual Studio).

Both CommonStuff and ServiceCore include a lot of .NET libraries as well as some 3rd party dlls. No Windows specific code however, no pinvoke or funky stuff.

If we consider ServiceConsole to become the app I want to run on Linux - what do I need to do to make it happen? Compiling Hello World was one thing but now, with all thoese references... I don't know where to start. Also, I can't even have the code to be compiled in Linux (proprietary code) so I need the code to be compiled in Windows and the compiled dll/exe to be run directly in Linux - is that something that is possible? I have seen a few posts saying that Mono can run compiled applications, but each and every Mono turorial I have seen starts with compiling. Also I'm not sure what this vNext is. I read stuff about the ability to run code in Linux if it's targetted for .net 6... but I have no idea how or what it means.

I would greatly appreciate if somebody could take some time to explain in some details what the options are nowadays and if it's realistic for a professional application or if it's only good for Hello Worlds yet.

Thanks

EDIT: Ok so it seems I can compile my code in VS and somehow run it on Linux using Mono - how?

like image 326
Nicolas Avatar asked Sep 18 '15 15:09

Nicolas


People also ask

Can you run .NET apps on Linux?

Now there's an alternative that's maturing and gaining popularity--you can run . NET applications on Linux, using the open source Mono runtime. And that's it--Mono will run your . NET binaries without requiring any conversion.

Can I use ASP net in Linux?

With ASP.NET Core, you can build and run . NET applications not only on Windows but also macOS and Linux. This guide shows you how to install ASP.NET Core on your Linux server and how to use it to create a web application.


1 Answers

I often develop multiplatform or linux-only complex applications, using Visual Studio 2015 on windows. You can just compile it in VS, copy to linux and run - almost always this will just work. If you do serious development you may want to compile under mono to spot some missing methods \ different signatures, but that is not required.

Now, windows service can be changed to just console application. Basically it is just console application which is managed by external tool. On linux there are different tools which allows you to manage (start \ stop \ restart on failure and so on) your "service" applications.

For web applications I usually use ServiceStack, which runs on mono without problems. You can even self-host it again as console application, behind nginx. You can also host asp.net applications under apache\nginx, with little or no code changes.

Half an year ago I "ported" big project which was developed for 2 years from windows server to CentOS 7 server, mostly by just copying stuff to centos and running (with very little changes, without recompilation under mono). Of course that project had many third party dependencies, and some developers of those dependencies did not even know mono exists. Nevertheless they just worked. And that solution had about 90 VS projects.

Of course, not every complex project is so easy to port, especially if you use native libraries often, but usually that is not the case. Also, if you work with sql server, note that sql server driver in mono is quite bad from my experience. I use postgre and avoid sql server with mono if I have a choice. Working with images in mono is also not very good, buggy and unstable. I avoid using Bitmap class there at all costs and pinvoke into imagemagick C api instead. But you need to do there only if you do serious and extensive imaging, for basic tasks it's fine.

Long story short - running a .NET app in Linux in 2015 is not a problem nowadays.

Update. If you are in 2018 already - there is almost no reason to use mono on linux, besides specific cases like mobile application development with Xamarin, game development with Unity, or UI application. If you just need regular console\service application (including web server) - use .NET Core. It's already stable and fast enough for production use, and from my experience - already much less buggy than mono even been. Porting your full .NET\mono application to .NET Core is in most cases relatively straightforward, though certain libraries might not yet be available on new platform. But if you are starting new project - choose .NET Core no doubt.

like image 195
Evk Avatar answered Oct 20 '22 23:10

Evk