今天嘗試了下Ubuntu上運行NET程序,按照 https://github.com/aspnet/Home 的指引,一步一步來:
1.安裝DNVM(原名KVM)
Linux控制台下輸入
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
2.安裝DNX(原名KRE)
dnvm upgrade
3.下載github上的實例代碼
git clone git@github.com:aspnet/Home.git
4.進入Console示例,准備運行示例代碼
dnx . run
結果看到提示:
/root/.dnx/runtimes/dnx-mono.1.0.0-beta5-11394/bin/dnx: line 14: exec: mono: not found
看樣子是必須依賴於Mono:此處做以解釋,在oschina博客中看到一段話(如下):
在非windows系統上運行ASP.NET vNext應用有如下兩種方式.
首先需要安裝Mono. 這塊基本上沒啥難度,但由於目前還有發布安裝包,可能會碰到一些問題,很多補丁都是打在mono的源碼里.如此,我們需要從gitHub上下載最新的代碼然后構建出含有所有補丁的版本.
另一種方法就是安裝 “K運行環境” 或簡稱KRE. 這是些命令行運行並構建(這塊沒什么區別)新生成的project.json文件.
KRE的安裝過程是由 “K Version Manager” (KVM)管理的. 這個可以安裝多個版本的KRE 並可以方便的在不同版本間切換.
但在https://github.com/aspnet/Home中,文檔上明確說明了,在linux中,依賴於Mono3.4.1 Or later
Minimum Requirements
These are the current minimum requirements for the latest preview release. They do not necessarily represent what the final minimum requirements will be.
Windows
Windows 7 or Windows Server 2008 R2.
.NET 4.5.1 for hosting in IIS
OS X/Linux
Mono 3.4.1 or later (Note: On OS X use the Homebrew formula specified below to install the required version of Mono)
bash or zsh and curl
有些同學可能會問,不是可以使用coreclr嗎? github主頁上也給出了明確說明(今天時間2015-3-28),注意加粗字體部分,目前在OSX/LINUX上沒有可用的Core CLR,只支持Mono45和x86(32位)。
不過OSX/LINUX可用的Core CLR會很快到來。
Switching to Core CLR
By default when running ASP.NET 5 applications on the Windows platform you are running on the full .NET Framework. You can switch to use the new Cloud Optimized runtime, or Core CLR, using the DNVM command.
Run dnvm upgrade -runtime CoreCLR This command gets the latest Core CLR version of the DNX and sets it as your default. The -runtime CoreCLR switch tells it to use Core CLR. You can use -r CLR to target desktop again.
Run dnx . web to run on WebListener.
The first line of your output should say "Loaded Module: dnx.core45.dll" instead of "Loaded Module: dnx.net45.dll"
The HelloWeb app should work the same as when running on the full desktop .NET Framework but now as a fully self-contained app with true side-by-side versioning support.
NOTE: There are many APIs from the .NET Framework that are not yet available when running on Core CLR. This set should get smaller and smaller as time goes on.
NOTE: There is no Core CLR currently available on OSX/Linux. There is only a single platform (mono45) and a single architecture (x86).
那么到這里為止,我們的程序還沒有跑起來,我們首先要安裝Mono,版本要求3.4.1 或更高,apt-get安裝的版本是3.12.1(參考http://www.mono-project.com/docs/getting-started/install/linux/#debian-ubuntu-and-derivatives)
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list sudo apt-get update
#我的環境是Ubuntu12.04 所以要執行
echo "deb http://download.mono-project.com/repo/debian wheezy-libtiff-compat main" | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list
apt-get install mono-complete
安裝完成之后,進入~/Home/samples/Console 執行命令
dnx . run
輸出 Hello World ,至此運行成功
進入~/Home/samples/HelloMvc,嘗試運行Web項目
dnx . kestrel
提示沒有安裝完整的packages
dun restore
還原所有的nuget包,再次執行 dnx . kestrel,仍然報錯,提交了一個issue:https://github.com/aspnet/Home/issues/420
后來確認應該采用dev分支的代碼。不過編譯后,仍然報錯,是mono本身的一個issue引起的,mono社區已經修復了。
暫時先測試到這里,等待coreCLR穩定再跟進
看樣子現在開始MVC6在linux運行還為時過早,等穩定了的吧。Mono已經開發了很多年了,跑NET應用已經比較穩定了。而我測試的ASPNET5、DNVM仍然是一個非常新的東西。所以此處運行示例有問題,並不代表,Mono本身有什么問題.