ASP.NET Core 的 Factory 中介軟體啟用

IMiddlewareFactory/IMiddleware中介軟體啟用的擴充點。

UseMiddleware 擴充方法會檢查中介軟體的已註冊類型是否實作 IMiddleware。 如果是,系統會使用容器中已註冊的 IMiddlewareFactory 執行個體來解析 IMiddleware 實作,而不是使用以慣例為基礎的中介軟體啟用邏輯。 中介軟體會註冊為應用程式服務容器中的範圍服務或暫時性服務

優點:

  • 依據用戶端要求啟用 (插入範圍服務)
  • 強型別的中介軟體

IMiddleware 是依據用戶端要求 (連線) 來啟用,因此範圍服務可以插入中介軟體的建構函式中。

查看或下載範例程式碼 (如何下載)

IMiddleware

IMiddleware 可定義應用程式要求管線的中介軟體。 InvokeAsync(HttpContext, RequestDelegate) 方法會處理要求,並傳回 Task 以代表中介軟體的執行。

依據慣例啟用的中介軟體:

public class ConventionalMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext context, AppDbContext db)
    {
        var keyValue = context.Request.Query["key"];

        if (!string.IsNullOrWhiteSpace(keyValue))
        {
            db.Add(new Request()
                {
                    DT = DateTime.UtcNow, 
                    MiddlewareActivation = "ConventionalMiddleware", 
                    Value = keyValue
                });

            await db.SaveChangesAsync();
        }

        await _next(context);
    }
}

MiddlewareFactory 啟用的中介軟體:

public class FactoryActivatedMiddleware : IMiddleware
{
    private readonly AppDbContext _db;

    public FactoryActivatedMiddleware(AppDbContext db)
    {
        _db = db;
    }

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        var keyValue = context.Request.Query["key"];

        if (!string.IsNullOrWhiteSpace(keyValue))
        {
            _db.Add(new Request()
                {
                    DT = DateTime.UtcNow, 
                    MiddlewareActivation = "FactoryActivatedMiddleware", 
                    Value = keyValue
                });

            await _db.SaveChangesAsync();
        }

        await next(context);
    }
}

您可為中介軟體建立延伸模組:

public static class MiddlewareExtensions
{
    public static IApplicationBuilder UseConventionalMiddleware(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<ConventionalMiddleware>();
    }

    public static IApplicationBuilder UseFactoryActivatedMiddleware(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<FactoryActivatedMiddleware>();
    }
}

您無法使用 UseMiddleware 將物件傳遞給由 Factory 啟用的中介軟體:

public static IApplicationBuilder UseFactoryActivatedMiddleware(
    this IApplicationBuilder builder, bool option)
{
    // Passing 'option' as an argument throws a NotSupportedException at runtime.
    return builder.UseMiddleware<FactoryActivatedMiddleware>(option);
}

由 Factory 所啟用中介軟體會新增至 Startup.ConfigureServices 中的內建容器:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(options =>
        options.UseInMemoryDatabase("InMemoryDb"));

    services.AddTransient<FactoryActivatedMiddleware>();

    services.AddRazorPages();
}

這兩個中介軟體都會在要求處理管線的 Startup.Configure 中註冊:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseConventionalMiddleware();
    app.UseFactoryActivatedMiddleware();

    app.UseStaticFiles();
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
    });
}

IMiddlewareFactory

IMiddlewareFactory 提供建立中介軟體的方法。 中介軟體 Factory 實作會在容器中註冊為範圍服務。

預設的 IMiddlewareFactory 實作是 MiddlewareFactory,其位於 Microsoft.AspNetCore.Http 套件中。

IMiddlewareFactory/IMiddleware中介軟體啟用的擴充點。

UseMiddleware 擴充方法會檢查中介軟體的已註冊類型是否實作 IMiddleware。 如果是,系統會使用容器中已註冊的 IMiddlewareFactory 執行個體來解析 IMiddleware 實作,而不是使用以慣例為基礎的中介軟體啟用邏輯。 中介軟體會註冊為應用程式服務容器中的範圍服務或暫時性服務

優點:

  • 依據用戶端要求啟用 (插入範圍服務)
  • 強型別的中介軟體

IMiddleware 是依據用戶端要求 (連線) 來啟用,因此範圍服務可以插入中介軟體的建構函式中。

查看或下載範例程式碼 (如何下載)

IMiddleware

IMiddleware 可定義應用程式要求管線的中介軟體。 InvokeAsync(HttpContext, RequestDelegate) 方法會處理要求,並傳回 Task 以代表中介軟體的執行。

依據慣例啟用的中介軟體:

public class ConventionalMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task InvokeAsync(HttpContext context, AppDbContext db)
    {
        var keyValue = context.Request.Query["key"];

        if (!string.IsNullOrWhiteSpace(keyValue))
        {
            db.Add(new Request()
                {
                    DT = DateTime.UtcNow, 
                    MiddlewareActivation = "ConventionalMiddleware", 
                    Value = keyValue
                });

            await db.SaveChangesAsync();
        }

        await _next(context);
    }
}

MiddlewareFactory 啟用的中介軟體:

public class FactoryActivatedMiddleware : IMiddleware
{
    private readonly AppDbContext _db;

    public FactoryActivatedMiddleware(AppDbContext db)
    {
        _db = db;
    }

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        var keyValue = context.Request.Query["key"];

        if (!string.IsNullOrWhiteSpace(keyValue))
        {
            _db.Add(new Request()
                {
                    DT = DateTime.UtcNow, 
                    MiddlewareActivation = "FactoryActivatedMiddleware", 
                    Value = keyValue
                });

            await _db.SaveChangesAsync();
        }

        await next(context);
    }
}

您可為中介軟體建立延伸模組:

public static class MiddlewareExtensions
{
    public static IApplicationBuilder UseConventionalMiddleware(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<ConventionalMiddleware>();
    }

    public static IApplicationBuilder UseFactoryActivatedMiddleware(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<FactoryActivatedMiddleware>();
    }
}

您無法使用 UseMiddleware 將物件傳遞給由 Factory 啟用的中介軟體:

public static IApplicationBuilder UseFactoryActivatedMiddleware(
    this IApplicationBuilder builder, bool option)
{
    // Passing 'option' as an argument throws a NotSupportedException at runtime.
    return builder.UseMiddleware<FactoryActivatedMiddleware>(option);
}

由 Factory 所啟用中介軟體會新增至 Startup.ConfigureServices 中的內建容器:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(options =>
        options.UseInMemoryDatabase("InMemoryDb"));

    services.AddTransient<FactoryActivatedMiddleware>();

    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

這兩個中介軟體都會在要求處理管線的 Startup.Configure 中註冊:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseConventionalMiddleware();
    app.UseFactoryActivatedMiddleware();

    app.UseStaticFiles();
    app.UseMvc();
}

IMiddlewareFactory

IMiddlewareFactory 提供建立中介軟體的方法。 中介軟體 Factory 實作會在容器中註冊為範圍服務。

預設的 IMiddlewareFactory 實作是 MiddlewareFactory,其位於 Microsoft.AspNetCore.Http 套件中。

其他資源