.NET跨平台:在Mac上跟着错误信息一步一步手写ASP.NET 5程序

今天坐高铁时尝试了一种学习ASP.NET 5的笨方法,从空文件夹开始,根据运行dnx . kestrel命令的错误信息,一步一步写代码,直至将一个最简单的ASP.NET程序运行起来。

尝试的具体步骤如下。

新建一个空文件夹HelloCnblogs:

mkdir HelloCnblogs && cd $_

在这个空HelloCnblogs文件夹中运行 dnx . kestrel 命令(基于CoreCLR的dnx),运行结果是如下的出错信息:

System.InvalidOperationException: Unable to resolve project 'HelloCnblogs' from /Git/HelloCnblogs
at Microsoft.Framework.Runtime.ApplicationHostContext..ctor
...

添加一个空project.json文件(命令为touch project.json),运行dnx . kestrel命令,错误信息如下:

Error: Microsoft.Framework.Runtime.FileFormatException: 
The JSON file can't be deserialized to a JSON object.
at Microsoft.Framework.Runtime.Project.GetProjectFromStream(
Stream stream, String projectName, String projectPath, ICollection`1 diagnostics)

在project.json文件中添加 {} ,运行dnx . kestrel命令,错误信息如下:

System.InvalidOperationException: Unable to load application or execute command 'kestrel'.
at Microsoft.Framework.ApplicationHost.Program.ThrowEntryPointNotfoundException(
DefaultHost host, String applicationName, Exception innerException)

在project.json中添加kestrel command:

"commands": {
"kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:8080"
}

运行dnx . kestrel,出错信息如下:

System.InvalidOperationException: Unable to load application or execute command 'Microsoft.AspNet.Hosting'.
Available commands: kestrel.
at Microsoft.Framework.ApplicationHost.Program.ThrowEntryPointNotfoundException(
DefaultHost host, String applicationName, Exception innerException)

在project.json中添加对Kestrel的引用

"dependencies": {
"Kestrel": "1.0.0-*"
}

运行dnu restore之后(基于mono的dnu),再运行dnx . kestrel,出错信息变为:

System.InvalidOperationException:
Failed to resolve the following dependencies for target framework 'DNXCore,Version=v5.0':
Kestrel 1.0.0-beta6-11871

在project.json中添加frameworks:

"frameworks": {
"dnxcore50": { }
}

运行dnu restore && dnx . kestrel,出现错误:

System.InvalidOperationException: A type named 'StartupProduction' or 'Startup' could not be found in assembly 'HelloCnblogs'.
at Microsoft.AspNet.Hosting.Startup.StartupLoader.FindStartupType(String startupAssemblyName, IList`1 diagnosticMessages)

创建Startup.cs文件,并添加一个Startup类:

namespace HelloCnblogs
{
public class Startup
{
}
}

继续dnx . kestrel,出现错误:

System.InvalidOperationException:
A method named 'ConfigureProduction' or 'Configure' in the type 'HelloCnblogs.Startup' could not be found.
at Microsoft.AspNet.Hosting.Startup.StartupLoader.FindMethod(
Type startupType, String methodName, String environmentName, Type returnType, Boolean required)

给Startup类添加Configure方法:

public class Startup
{
public void Configure(IApplicationBuilder app)
{
}
}

继续dnx . kestrel,出现错误:

error CS0246: The type or namespace name 'IApplicationBuilder' could not be found (are you missing a using directive or an assembly reference?)

在Startup.cs中添加命名空间:

using Microsoft.AspNet.Builder;

继续dnx . kestrel,这次成功运行!

Started

这里用浏览器访问 http://localhost:8080/ ,能成功访问,但页面一片空白,因为我们在程序中没进行任何内容输出操作。

于是,在Startup.cs中添加输入内容的代码:

public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async context => await context.Response.WriteAsync("Hello, cnblogs!"));
}
}

继续用dnx . kestrel命令运行,出现错误:

error CS1061: 'HttpResponse' does not contain a definition for 'WriteAsync' and no extension method 'WriteAsync' accepting a first argument of type 'HttpResponse' could be found (are you missing a using directive or an assembly reference?)

在Startup.cs中添加命名空间:

using Microsoft.AspNet.Http;

再次运行,成功!

$ dnx . kestrel
Started

浏览器访问 http://localhost:8080/ ,得到正常的响应内容:

Hello, cnblogs!

通过这样的试错法,得到了运行一个最简单的ASP.NET 5程序的最小配置:

一个文件夹,2个文件(project.json与Startup.cs)。

project.json文件中的内容:

{
"commands": {
"kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:8080"
},
"dependencies": {
"Kestrel": "1.0.0-*"
},
"frameworks": {
"dnxcore50": { }
}
}

Startup.cs中的内容:

using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; namespace HelloCnblogs
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(async context => await context.Response.WriteAsync("Hello, cnblogs!"));
}
}
}

虽然是用最笨的方法写了一个最简单的程序,但是这么动手操作一次,感觉就是不一样。

上一篇:【设计模式系列】之OO面向对象设计七大原则


下一篇:Nhibernate