ASP.NET Core 2 學習筆記(三)中間件

以前ASP.NET中使用的HTTP Modules及HTTP Handlers,在ASP.NET Core中已不復存在,取而代之的是Middleware。Middleware除了簡化了HTTP Modules/Handlers的使用方式,還帶入了Pipeline的概念。
本篇將介紹ASP.NET Core的Middleware概念及用法。git

Middleware 概念

ASP.NET Core在Middleware的官方說明中,使用了Pipeline這個名詞,意指Middleware像水管同樣能夠串聯在一塊兒,全部的Request及Response都會層層通過這些水管。
用圖例能夠很容易理解,以下圖:
github

 

App.Use

Middleware的註冊方式是在Startup.csConfigureIApplicationBuilder使用Use方法註冊。
大部分擴展的Middleware也都是以Use開頭的方法註冊,例如:瀏覽器

  • UseMvc():MVC的Middleware
  • UseRewriter():URL rewriting的Middleware

一個簡單的Middleware 範例。以下:bash

Startup.csapp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace MyWebsite
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("First Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("First Middleware out. \r\n");
            });

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Second Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("Second Middleware out. \r\n");
            });

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Third Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("Third Middleware out. \r\n");
            });

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World! \r\n");
            });
        }
    }
}

用瀏覽器打開網站任意連結,輸出結果: async

First Middleware in. 
Second Middleware in. 
Third Middleware in. 
Hello World! 
Third Middleware out. 
Second Middleware out. 
First Middleware out. 

在Pipeline的概念中,註冊順序是很重要的事情。請求通過的順序必定是先進後出。網站

Request 流程以下圖: ui

 

 Middleware 也能夠做爲攔截使用,以下:this

 Startup.csspa

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace MyWebsite
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("First Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("First Middleware out. \r\n");
            });

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Second Middleware in. \r\n");

                // 水管阻塞,封包不日後送 var condition = false; if (condition) { await next.Invoke(); }
                await context.Response.WriteAsync("Second Middleware out. \r\n");
            });

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Third Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("Third Middleware out. \r\n");
            });

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World! \r\n");
            });
        }
    }
}

輸出結果:

First Middleware in. 
Second Middleware in. 
Second Middleware out. 
First Middleware out.

在Second Middleware 中,由於沒有達成條件,因此封包也就不在日後面的水管傳送。流程如圖:

App.Run

Run是Middleware的最後一個行爲,以上面圖例來講,就是最末端的Action。
它不像Use能串聯其餘Middleware,但Run仍是能完整的使用Request及Response。

App.Map

Map是能用來處理一些簡單路由的Middleware,可依照不一樣的URL指向不一樣的Run及註冊不一樣的Use
新增一個路由以下:

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace MyWebsite
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("First Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("First Middleware out. \r\n");
            });

            // app.Use(async (context, next) =>
            // {
            //     await context.Response.WriteAsync("Second Middleware in. \r\n");

            //     // 水管阻塞,封包不日後送
            //     var condition = false;
            //     if (condition)
            //     {
            //         await next.Invoke();
            //     }
            //     await context.Response.WriteAsync("Second Middleware out. \r\n");
            // });

            app.Map("/second", mapApp => { mapApp.Use(async (context, next) => { await context.Response.WriteAsync("Second Middleware in. \r\n"); await next.Invoke(); await context.Response.WriteAsync("Second Middleware out. \r\n"); }); mapApp.Run(async context => { await context.Response.WriteAsync("Second. \r\n"); }); });


            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Third Middleware in. \r\n");
                await next.Invoke();
                await context.Response.WriteAsync("Third Middleware out. \r\n");
            });

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World! \r\n");
            });
        }
    }
}

開啓網站任意連結,會顯示:

First Middleware in. 
Third Middleware in. 
Hello World! 
Third Middleware out. 
First Middleware out. 

開啓網站http://localhost:5000/second,則會顯示:

First Middleware in. 
Second Middleware in. 
Second. 
Second Middleware out. 
First Middleware out. 

 

建立Middleware 類

若是Middleware所有都寫在Startup.cs,代碼將很難維護,因此應該把自定義的Middleware邏輯獨立出來。
創建Middleware類不須要額外繼承其它類或接口,通常的類便可,例子以下:

FirstMiddleware.cs

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace MyWebsite
{
    public class FirstMiddleware
    {
        private readonly RequestDelegate _next;

        public FirstMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            await context.Response.WriteAsync($"{nameof(FirstMiddleware)} in. \r\n");

            await _next(context);

            await context.Response.WriteAsync($"{nameof(FirstMiddleware)} out. \r\n");
        }
    }
}

全局註冊

Startup.Configure註冊Middleware就能夠套用到全部的Request。以下:

Startup.cs

// ...
public class Startup
{
    // ...
    public void Configure(IApplicationBuilder app)
    {
        app.UseMiddleware<FirstMiddleware>();
        // ...
    }
}

局部註冊

Middleware 也能夠只套用在特定的Controller 或Action。註冊方式以下:

Controllers\HomeController.cs

// ..
[MiddlewareFilter(typeof(FirstMiddleware))]
public class HomeController : Controller
{
    // ...

    [MiddlewareFilter(typeof(SecondMiddleware))]
    public IActionResult Index()
    {
        // ...
    }
}

Extensions

大部分擴展的Middleware都會用一個靜態方法包裝,如:UseMvc()UseRewriter()等。
自定義的Middleware固然也能夠透過靜態方法包,範例以下:

Extensions\CustomMiddlewareExtensions.cs

using Microsoft.AspNetCore.Builder;

namespace MyWebsite
{
    public static class CustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseFirstMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<FirstMiddleware>();
        }
    }
}

註冊Extension Middleware 的方式以下:

Startup.cs

// ...
public class Startup
{
    // ...
    public void Configure(IApplicationBuilder app)
    {
        app.UseFirstMiddleware();
        // ...
    }
}

參考

ASP.NET Core Middleware Fundamentals 
Creating Custom Middleware In ASP.Net Core

 

老司機發車啦:https://github.com/SnailDev/SnailDev.NETCore2Learning

相關文章
相關標籤/搜索