王朝网络
分享
 
 
 

Running .Net applications on Linux with Mono

王朝c#·作者佚名  2006-04-28
宽屏版  字体: |||超大  

Imagine the fate of your company rests on your completing your new Linux project on time. You have a crack team of first-class developers, but they're all .Net programmers. What are you going to do? Admit that Windows is better that Linux? Cry? Resign? No, you're going to install Mono and save the world!

Mono is an open source project (sponsored by Novell) that allows you to run .Net applications on Linux (as well as Unix, Mac OS X, Solaris and even Windows). To obtain it, go to the Mono download page and find the version you need for your distro.

Once you've installed Mono, get one of your .Net programmers to create and compile a simple Microsoft Visual Studio C# console application. Just something easy, such as:

using System;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Hello World");

}

}

}

If you don't have your own tame .Net programmer, you're going to need a Windows machine with Microsoft Visual Studio installed. (Stop making faces like that!) Download the free Microsoft Visual C# Express Edition.

Transfer the compiled application from the Windows machine by using FTP or Samba, then log on to your Linux box and run the application:

$ ConsoleApplication1.exe Hello World

Surely it can't be as simple as that? Amazingly, it is. If you don't believe that it's Mono that's allowing you to do this, try transferring the application to a Linux box that hasn't got Mono installed. You'll get a result something like:

$ ConsoleApplication1.exe

-bash: ./ConsoleApplication1.exe: cannot execute binary file

You may need to run chmod +x on the file to get it to run. Also, I found that on Debian at this stage I got an error: The assembly mscorlib.dll was not found or could not be loaded. It should have been installed in the '/usr/lib/mono/2.0/mscorlib.dll' directory. I cured that by executing:

$ cd /usr/lib/mono

$ sudo ln -s 1.0 2.0

The application name ends in .exe because it has been compiled as a Windows application. Once you've got it on Linux you can of course call it whatever you want:

$ mv ConsoleApplication1.exe HelloWorld

$ HelloWorld

Hello World

You've just seen just how easy is to use a Microsoft Visual Studio application on Linux. However, you're also probably thinking that you have absolutely no intention of using Windows at all; your .Net programmers are just going to have to have to learn to program in something that runs solely on Linux. That's a good idea -- if you've got the time for them to learn something new. If not, don't despair. Mono comes with its own .Net compiler, mcs. To us it, paste the code for the 'Hello World' application above into a file, compile it (using mcs), then run the new application from the command line.

$ mcs -out:HelloWorld.exe Program.cs

$ HelloWorld.exe

Hello World

If you've still got that Windows machine fired up, you may find it interesting to transfer the newly compiled application to it and running your app in Windows. You'll find that you've created something on Linux that will also run on Windows.

Of course, by the time you've explained all this to your .Net programmers they'll be up in arms. "How can you expect us to program without a pretty GUI?" they'll cry.

"With Monodevelop," you'll say, referring to Mono's integrated development environment (IDE). With Monodevelop your developers can happily work with Glade#, Gnome#, and GTK#, all within a .Net-type environment. Check the software's download page for your distro's version and any dependencies.

When you install Glade, make sure that you obtain Glade2 and not Glade1. On Debian sudo apt-get install glade will grab Glade1, which will not work with Monodevelop. The correct installation command is sudo apt-get install glade-2.

The developers should leave you in peace for a while, but they'll be back. "Where's the GUI designer?" they'll ask. This is where you introduce them to building interfaces with Glade. When you start a new Monodevelop Glade# application, use a file called gui.glade. You can edit this with Glade and then do the programming in Monodevelop. If your developers have any further questions, point them in the direction of the Mono GTK# Beginner's Guide.

With your .Net programmers happily producing applications for Linux, you can turn your attention to ASP .Net for building Web applications. You're going to need a Web server, which can be either Apache (with mod_mono installed) or Mono's own Web server, XSP. Have a look at the Mono Handbook page for Web sites and Web services to find out how to set up the XSP server. On Debian, for example, its just a matter of installing the software:

sudo apt-get install mono-xsp

Then running it:

$ cd mono

$ xsp

xsp

Listening on port: 8080

Listening on address: 0.0.0.0

Root directory: /home/bainm/mono

Hit Return to stop the server.

XSP will run in the directory where you start it, will use this as its home directory, and will use port 8080 by default. If you don't want to have to change into that directory every time you start the server, you can use its --root option. To change the port, use the --port option:

$ xsp --root ~/mono --port 8081

When you've got your server up and running, go to Mono's Web Services page, where you'll find some good examples of the types of services that you can start running. However, if you're desperate to see the server in action, go to the directory you're running XSP from and cat the following into index.aspx:

<%@ Page Language="C#" %>

<html><body>

<%

Response.Output.Write("Hello World!");

%>

</body> </html>

Now open up a Web browser and type in the URL of your host (plus the port number) -- e.g. http://hector:8080. Granted, this isn't the most exciting example in the world, but it proves that you can run ASP .Net on Linux. A quick search on Google will give you a host of other examples that you can use.

With Mono, Monodevelop, and XSP in place, you can throw away Microsoft Visual Studio and you can throw away Windows, and you don't have to throw away the valued experience of your .Net programmers.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有