第一次接觸Core 6.0
1.採用minimal API構件ASP.NET CORE程式
RequestDelegate handler = context => context.Response.WriteAsync("Hello,World!"); //中介軟體委託 WebApplicationBuilder builder = WebApplication.CreateBuilder(args);//通過靜態工廠方法,建立應用程式構造類例項 WebApplication app = builder.Build();//構建應用程式類 app.Run(handler);//把handle中介軟體插入到應用程式管道內。 app.Run();//啟動應用程式
// public delegate Task RequestDelegate(HttpContext context); RequestDeleate就是一個委託。
2.一步建立WebApplication
WebApplication app = WebApplication.Create(args);//直接通過應用程式類的靜態工廠方法,建立對應的應用程式例項。 app.Run(HandleAsync); app.Run(); //這個的本質就是RequestDelegate委託 static Task HandleAsync(HttpContext httpContext) => httpContext.Response.WriteAsync("Hello,World!1");
3.構造應用程式之前就新增中介軟體
WebApplication app = WebApplication.Create(args); IApplicationBuilder appBuilder= app; appBuilder.Use(HelloMiddleware).Use(WorldMiddleware);//Use方法把中介軟體新增到應用程式管道內(按先後順序插入)。 app.Run(); static RequestDelegate HelloMiddleware(RequestDelegate next) => async httpContext => { await httpContext.Response.WriteAsync("Hello, "); await next(httpContext); };static RequestDelegate WorldMiddleware(RequestDelegate next) => async httpContext => await httpContext.Response.WriteAsync("World!");
4.使用中介軟體變體委託
WebApplication app = WebApplication.Create(args); IApplicationBuilder appBuilder = app; appBuilder.Use(HelloMiddleware).Use(WorldMiddleware); app.Run(); static async Task HelloMiddleware(HttpContext httpContext, RequestDelegate next) //Q.這裡的Next委託是怎麼確定的? { await httpContext.Response.WriteAsync("Hello, "); await next(httpContext); }; static async Task WorldMiddleware(HttpContext httpContext, RequestDelegate next) => await httpContext.Response.WriteAsync("World!"); //Q.這裡的Next委託是怎麼確定的?
WebApplication app = WebApplication.Create(args); IApplicationBuilder appBuilder = app; appBuilder.Use(HelloMiddleware).Use(WorldMiddleware); app.Run(); static async Task HelloMiddleware(HttpContext httpContext, Func<Task> next) //Q.這裡的Next委託是怎麼確定的? { await httpContext.Response.WriteAsync("Hello, "); await next(); }; static async Task WorldMiddleware(HttpContext httpContext, Func<Task> next) => await httpContext.Response.WriteAsync("World!"); //Q.這裡的Next委託是怎麼確定的?
5.使用強型別中介軟體
using APP; using Microsoft.Extensions.Options; var builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton<IGreeter, Greeter>().AddSingleton<GreetingMiddleware>(); //.Configure<GreetingOptions>(builder.Configuration.GetSection("greeting")); //依賴注入單例物件。 var app = builder.Build(); app.UseMiddleware<GreetingMiddleware>(); app.Run(); namespace APP { public interface IGreeter { string Greet(DateTimeOffset time); } public class Greeter : IGreeter { private readonly IConfiguration _configuration; public Greeter(IConfiguration configuration) => _configuration = configuration.GetSection("greeting"); public string Greet(DateTimeOffset time) => time.Hour switch { var h when h >= 5 && h < 12 => _configuration["morning"], var h when h >= 12 && h < 17 => _configuration["afternoon"], _ => _configuration["evening"] }; }public class GreetingMiddleware : IMiddleware { private readonly IGreeter _greeter; public GreetingMiddleware(IGreeter greeter) => _greeter = greeter; public Task InvokeAsync(HttpContext context, RequestDelegate next) => context.Response.WriteAsync(_greeter.Greet(DateTimeOffset.UtcNow)); }
public class GreetingOptions
{
public string Morning { get; set; }
public string Afternoon { get; set; }
public string Evening { get; set; }
}
}
也可以改成下面的這種形式
/// <summary> /// 也可以改寫成下面這樣,建構函式注入 /// </summary> public class GreetingMiddleware //這裡特別注意,我並沒有實現IMiddleware,也是可以直接當成中介軟體來使用的。 { private readonly IGreeter _greeter; public GreetingMiddleware(IGreeter greeter, RequestDelegate next) => _greeter = greeter; public Task InvokeAsync(HttpContext context) => context.Response.WriteAsync(_greeter.Greet(DateTimeOffset.UtcNow)); } /// <summary> /// 也可以改成下面這樣,方法注入 /// </summary> public class GreetingMiddleware { public GreetingMiddleware(RequestDelegate next) { } public Task InvokeAsync(HttpContext context, IGreeter greeter) => context.Response.WriteAsync(greeter.Greet(DateTimeOffset.UtcNow)); }
知識點:
RequestDelegate --中介軟體委託
IMiddleware --中介軟體介面
WebApplication --應用程式類,用於配置HTTP管道和Web應用程式。
WebApplicationBuilder --應用程式類的建造者。
HttpContext--當前上下文類(主要包含HttpRequest和HttpResponse屬性)
Microsoft.NET.Sdk --控制檯應用程式
Microsoft.NET.Sdk.Web --Web應用程式(掛載在控制檯上)
ImplicitUsings屬性與C#10提供的一個叫做"全域性名稱空間"有關
Nullable屬性與一個名為"空值(null)驗證"的特性有關。
.NET6專案控制檯,可以直接沒有引用、名稱空間和類的原因是與C#10提供的一個被"頂級語句(Top-level Statements)"的特性有關。
中介軟體的定義要求
中介軟體型別需要定義一個公共例項型別(靜態型別無效),其建構函式可以注入任意的依賴服務看,單必須包含一個RequestDelegate型別的引數該引數表示由後續中介軟體構成的管道,當前中介軟體利用它將請求分發給後續管道作進一步處理。
針對請求的處理實現在一個命名為InvokeAsync或者Invoke方法中,該方法返回型別為Task,第一個引數並繫結為當前的HttpContext上下文。
參考連結
https://www.cnblogs.com/artech/p/inside-asp-net-core-6-1.html#s101