2015 年在 Linux 中运行 .NET 应用程序

Running a .NET app in Linux in 2015(2015 年在 Linux 中运行 .NET 应用程序)
本文介绍了2015 年在 Linux 中运行 .NET 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR : 现在有哪些选项可以在 Linux 上运行 VS2015 解决方案?

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

我有一个在 Windows 中运行良好的 .NET 应用程序,并且收到了将其移植到 Linux 的请求.我一直在努力理解 2015 年今天的含义,但我真的对所有指向 Mono 的旧帖子和谈论 vNext 的新帖子感到困惑(并且仍然提到 Mono).此外,我对 .NET 有很好的背景,但我对 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...

我遵循最近的教程并设法在 Linux 中编译和运行 Hello World 控制台应用程序.这需要安装一些我不知道的东西,包括 Mono,然后运行一些k"命令(kpm restore、kpm build、k run).但是现在我不知道如何继续我的真实案例场景.

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.

我有一个 VS2015 解决方案,它有很多项目,但这里只有 4 个值得一提:- 常见的东西- 服务核心- ServiceWindows/ServiceConsole

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

所有代码都在 ServiceCore 项目(类库)中,该项目本身引用了 CommonStuff(另一个类库).ServiceWindows 是一个 Windows 服务项目,它简单地调用 ServiceCore 的 Main 函数,ServiceConsole 做同样的事情,但在控制台应用程序中(出于调试原因,因为无法从 Visual Studio 调试 Windows 服务项目).

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).

CommonStuff 和 ServiceCore 都包含许多 .NET 库以及一些 3rd 方 dll.但是没有特定于 Windows 的代码,没有 pinvoke 或时髦的东西.

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.

如果我们认为 ServiceConsole 成为我想在 Linux 上运行的应用程序 - 我需要做些什么来实现它?编译 Hello World 是一回事,但现在,有了所有这些参考资料……我不知道从哪里开始.另外,我什至无法在 Linux 中编译代码(专有代码),所以我需要在 Windows 中编译代码,并且编译后的 dll/exe 可以直接在 Linux 中运行——这可能吗?我看过一些帖子说 Mono 可以运行已编译的应用程序,但我看到的每一个 Mono 教程都是从编译开始的.我也不确定这个 vNext 是什么.我阅读了有关在 Linux 中运行代码的能力(如果它针对 .net 6)的内容......但我不知道它是如何或意味着什么.

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.

如果有人能花一些时间详细解释一下现在有哪些选项,以及它对于专业应用程序是否现实,或者它是否只对 Hello Worlds 有用,我将不胜感激.

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.

谢谢

好的,看来我可以在 VS 中编译我的代码,并以某种方式使用 Mono 在 Linux 上运行它 - 怎么样?

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

推荐答案

我经常在 Windows 上使用 Visual Studio 2015 开发多平台或仅限 linux 的复杂应用程序.您可以在 VS 中编译它,复制到 linux 并运行 - 几乎总是这样就可以了.如果您进行认真的开发,您可能希望在单声道下编译以发现一些缺失的方法不同的签名,但这不是必需的.

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.

现在,Windows 服务可以更改为仅控制台应用程序.基本上它只是由外部工具管理的控制台应用程序.在 linux 上有不同的工具可以让你管理(启动停止失败重启等等)你的服务"应用程序.

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.

对于 Web 应用程序,我通常使用 ServiceStack,它在单声道上运行没有问题.您甚至可以在 nginx 后面再次将其作为控制台应用程序自行托管.您还可以在 apache ginx 下托管 asp.net 应用程序,只需很少或无需更改代码.

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 ginx, with little or no code changes.

半年前,我将开发了 2 年的大型项目从 windows 服务器移植"到 CentOS 7 服务器,主要是通过将内容复制到 centos 并运行(几乎没有什么变化,没有在 mono 下重新编译).当然,该项目有许多第三方依赖项,这些依赖项的一些开发人员甚至不知道 Mono 的存在.然而,他们只是工作.该解决方案有大约 90 个 VS 项目.

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.

当然,并不是每个复杂的项目都那么容易移植,尤其是如果您经常使用本机库,但通常情况并非如此.此外,如果您使用 sql server,请注意,根据我的经验,单声道中的 sql server 驱动程序非常糟糕.如果可以选择,我会使用 postgre 并避免使用带有 mono 的 sql server.以单声道处理图像也不是很好,有问题且不稳定.我不惜一切代价避免在那里使用 Bitmap 类,而是使用 imagemagick C api.但是,只有在进行认真而广泛的成像时才需要这样做,对于基本任务来说就可以了.

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.

长话短说 - 2015 年在 Linux 中运行 .NET 应用程序现在已经不是问题了.

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

更新.如果您已经是 2018 年了 - 除了使用 Xamarin 进行移动应用程序开发、使用 Unity 进行游戏开发或 UI 应用程序等特定情况之外,几乎没有理由在 linux 上使用 mono.如果您只需要常规的控制台服务应用程序(包括 Web 服务器)- 使用 .NET Core.它已经足够稳定且足够快,可以用于生产,而且根据我的经验 - 已经比单声道少得多.在大多数情况下,将完整的 .NETmono 应用程序移植到 .NET Core 相对简单,尽管某些库可能还没有在新平台上可用.但是,如果您要开始新项目 - 毫无疑问选择 .NET Core.

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 consoleservice 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 .NETmono 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.

这篇关于2015 年在 Linux 中运行 .NET 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)