Recently in the study of how to be. NET application to run in a non-Windows operating system, and will gradually write some articles out. There is not much research so far, so most of these articles mainly record some of my experiments.
This article records how I used Nancyfx to write a self-hosted (self-host) application and publish it to a Linux system.
What is NANCYFX?
To put it simply, this is really a magical framework. It defines itself as: Lightweigh Web framework for. NET. I don't know, it's a surprise.
http://nancyfx.org/
Compared to the framework of Microsoft's official ASP. NET MVC or ASP. NET Web API, it is really lightweight. Easy to use, and based on the modular design, it does have its own characteristics. It implements Owin, so it can be hosted in IIS, or any other program.
With less gossip, we can quickly develop an example program.
Create a self-hosted web application
I'm using visual Studio 2013 and created the simplest console application command for Consoleapplicationnancy
Add a reference to two packages
Install-Package Nancy.Hosting.SelfInstall-Package Mono.Posix
在Program.cs文件的Main方法中添加如下代码
usingMono.unix;usingMono.Unix.Native;usingNancy.Hosting.Self;usingSystem;namespaceconsoleapplicationnancy{classprogram {Static voidMain (string[] args) {var uri ="http://localhost:8888"; Console.WriteLine ("Nancy on:"+ URI); var host =NewNancyhost (NewUri (URI)); Host. Start ();if(Type.GetType ("Mono.runtime") !=NULL) {Unixsignal.waitany (New[]{NewUnixsignal (Signum.sigint),NewUnixsignal (Signum.sigterm),NewUnixsignal (Signum.sigquit),NewUnixsignal (Signum.sighup)}); }Else{Console.read (); } Console.WriteLine ("Stopping Nancy"); Host. Stop (); } }}
This code means to start a nancyhost on the 8888 port on the local machine to listen, and to add some features of the special listening signal if the Mono runtime environment is detected.
Next we need to add a concrete clear module, NANCYFX in the module is the most common class file, as long as the inheritance of Nancymodule. And each module can register its own path resolution rules.
using Nancy; namespace consoleapplicationnancy{public class hellomodule:nancymodule {public hellomodule () {get[ "/" ] = parameters = = "hello,nancy ..." ; } }}
You can now press F5 to debug. Note that you want to open VisualStudio as an administrator, as this involves port snooping.
It's magical, isn't it? Wouldn't it be more interesting if we wanted this program to run on a non-Windows operating system like Linux?
Running the Nancy application in Linux
I'm doing an experiment here with Ubuntu 15.04 LTS.
In order to run. NET applications in Linux, it is now necessary to first install mono (very much admire this project team). Microsoft itself is currently developing. NET Core, but not all of it is done yet.
Use the following command to easily install mono (executed in one sentence)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EFecho "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.listsudo apt-get updatesudo apt-get install mono-complete
You can view the currently installed mono version via Mono–v
The next step is to upload our compiled program to my server.
First, I created a directory/var/www/nancydemo on the server and assigned the necessary permissions (writable, executable)
Then, by winscp This tool, upload the compiled results to the directory
Using the Mono ConsoleApplicationNancy.exe directive, we can launch our application based on the Mono framework.
Then we can access it in the following ways
It looks really good, doesn't it? Didn't think. NET applications can be migrated to Linux in such a simple way? This thanks to Mono. Didn't expect the site to be so easy to write it? This is going to be a good study, Nancy.
However, there is a problem with launching the application so that as soon as my console is closed, the site is closed. Is there any way to keep this program running?
Use supervisor to keep this program in the background
First, install supervisor using the following command
apt-get install supervisor
创建一个配置文件 /etc/supervisor/conf.d/nancydemo.conf
在文件中输入如下的内容,并保存
[program:nancydemo]command=mono ConsoleApplicationNancy.exe -duser=www-datastderr_logfile = /var/log/supervisor/nancydemo-err.logstdout_logfile = /var/log/supervisor/nancydemo-stdout.logdirectory=/var/www/nancydemo/
Then, use the following command to switch to the Supervisor command line
Finally, start the Nancydemo application.
In this case, even if our console is closed, the site will always be running in the background.
If you want to stop Nancydemo this application, you can use the following method
Running the Nancy application in Linux