别人开源是种奉献精神, 微软突然把 .Net Framework 也开源, 人们看到的却是一种野心, 现在 .NET Core 开源还真正跨平台了, 真是雄心壮志啊.
本人 Java 多年, 但不可否认 C# 是一个很好的语言, 我认为单纯从语言上来说要优于 Java 语言, 所以 .NET Core 搭上 C# 力量更是不能小觑, 不得不摸索一下.
之前虽有浅尝 .NET Core 的 Hello World, 以及最简单的 ASP.NET 应用, 但未有成文, 在此再作演练一番以备忘.
安装 .NET Core
本人所用平台为 Mac, 所以当打开 .NET Core 的首页面 https://www.microsoft.com/net/core 时直接激活了 Mac 标签, 在这个页面我们知道 .NET Core 支持 Windows, Linux, Mac; 还有现成的 Docker 镜像拉下即用. .NET Core 当前版本是 1.0, 可以照着官方向导来做, 不过我倾向于全控制台来完成. 首先你的 Mac 得先有个 brew
, 现在用 port
管理应用比较少了吧.
1 2 |
$ brew update $ brew cask install dotnet |
说白了, 安装 .NET Core 只需要用 brew cask install dotnet
, 第一条命令没事都应该运行一下的, 发现有可升级的就运行一下 brew upgrade
.
brew cask install dotnet
会检测是否有 openssl
, 没有的话自动安装, 接着帮你下载 dotnet 安装包装上:
➜ brew cask install dotnet
==> Caveats
The latest version of OpenSSL is required to use .NET Core.
It was already installed, but you may need to link it:
ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
Zsh users may need to symlink the dotnet binary:
ln -s /usr/local/share/dotnet/dotnet /usr/local/bin
==> Satisfying dependencies
==> Installing Formula dependencies from Homebrew
openssl ... done
complete
==> Downloading https://download.microsoft.com/download/0/A/3/0A372822-205D-4A86-BFA7-084D2CBE9EDF/dotnet-dev-osx-x64.1.0.0-preview2-003121.
Already downloaded: /Users/Yanbin/Library/Caches/Homebrew/Cask/dotnet--1.0.0-preview2-003121.pkg
==> Verifying checksum for Cask dotnet
==> Running installer for dotnet; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
==> installer: Package name is Microsoft .NET Core 1.0.0 - SDK Preview 2 (x64)
==> installer: Upgrading at base path /
==> installer: The upgrade was successful.
? dotnet was successfully installed!
为什么是 brew cask install dotnet
而不是 brew install dotnet
呢? 没问题, 可以试一下
➜ brew install dotnet
Error: No available formula with the name "dotnet"
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
This formula was found in a tap:
Caskroom/cask/dotnet
To install it, run:
brew install Caskroom/cask/dotnet
brew install Caskroom/cask/dotnet
和 brew cask install dotnet
是一样的. 这里没有遵照官方文档去手工下载那个安装文件 dotnet-dev-osx-x64.1.0.0-preview2-003121.pkg 来执行, 我们做的更自动化.
装好了 openssl
和 .NET Core
之后按照提示运行下
1 2 3 4 5 |
$ ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/ $ ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/ #Zsh users may need to symlink the dotnet binary, 能执行 dotnet 命令就不用了 $ ln -s /usr/local/share/dotnet/dotnet /usr/local/bin |
如果不执行上面头两条命令, 我们在后面执行 dotne
命令时就会看到类似于这样的错误
Unhandled Exception: System.TypeInitializationException: The type initializer for 'Crypto' threw an exception. ---> System.TypeInitializationException: The type initializer for 'CryptoInitializer' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'System.Security.Cryptography.Native': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)
at Interop.CryptoInitializer.EnsureOpenSslInitialized()
at Interop.CryptoInitializer..cctor()
...................................................
现在就可以执行 dotnet
命令了, 它是你与 .NET Core 交互的门户, 也是一个构建工具.
创建 Hello World 项目
安装过程到此结束, 马上过渡到一个最简单的 Hello World 控制台程序的练习了
1 2 3 |
mkdir hwapp cd hwapp dotnet new |
第一次执行 dotnet new
命令时间长一点点, 可能一分钟, 以后就快了. 执行完了提示 Created new C# project in /Users/Yanbin/Desktop/hwapp
, 并且创建了两个文件
- Program.cs 这是一个标准的 Hello World 程序
123456789namespace HelloWorld{class Hello {static void Main(string[] args){System.Console.WriteLine("Hello World!");}}} - project.json 这是一个项目配置文件, 内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ "version": "1.0.0-*", "buildOptions": { "debugType": "portable", "emitEntryPoint": true }, "dependencies": {}, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.0" } }, "imports": "dnxcore50" } } } |
运行 Hello World 程序
1 2 |
dotnet restore dotnet run |
➜ hwapp$ dotnet restore
log : Restoring packages for /Users/Yanbin/Desktop/hwapp/project.json...
log : Writing lock file to disk. Path: /Users/Yanbin/Desktop/hwapp/project.lock.json
log : /Users/Yanbin/Desktop/hwapp/project.json
log : Restore completed in 1273ms.
hwapp$ dotnet run
Project hwapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling hwapp for .NETCoreApp,Version=v1.0
Compilation succeeded.
0 Warning(s)
0 Error(s)
Time elapsed 00:00:05.7311046
Hello World!
dotnet restore
会生成一个 project.lock.json
文件, 它是 dotnet run
必须的. 它像是 Visual Studio 中的 Solution 文件.
实质运行的就是那个 bin/Debug/netcoreapp1.0/hwapp.dll
文件, 比如我们可以执行
➜ hwapp$ dotnet bin/Debug/netcoreapp1.0/hwapp.dll
Hello World!
dotnet run
之后生成的文件如下
➜ hwapp$ tree
.
├── Program.cs
├── bin
│ └── Debug
│ └── netcoreapp1.0
│ ├── hwapp.deps.json
│ ├── hwapp.dll
│ ├── hwapp.pdb
│ ├── hwapp.runtimeconfig.dev.json
│ └── hwapp.runtimeconfig.json
├── obj
│ └── Debug
│ └── netcoreapp1.0
│ ├── dotnet-compile-csc.rsp
│ ├── dotnet-compile.assemblyinfo.cs
│ └── dotnet-compile.rsp
├── project.json
└── project.lock.json
dotnet 其他命令
dotnet
的其他几个命令如下, 用 dotnet --help
查看
new Initialize a basic .NET project
restore Restore dependencies specified in the .NET project
build Builds a .NET project
publish Publishes a .NET project for deployment (including the runtime)
run Compiles and immediately executes a .NET project
test Runs unit tests using the test runner specified in the project
pack Creates a NuGet package
现在对 .NET Core 有了一个基本的感观认识了, 接下来看看如何用 ASP.NET Core, 用 .NET Core 做网站了.
本文链接 https://yanbin.blog/net-core-console-hello-world/, 来自 隔叶黄莺 Yanbin Blog
[版权声明] 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。