0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NETのお勉強6_ミドルウェア使ってみる

Posted at

概要

AmazonLinux2023インスタンス上にコンテナで.NETを利用したサーバをたてる
そのお勉強メモ6

ミドルウェアを使ってみる

グローバルなミドルウェア

どのルートであっても必ず通るようになるミドルウェア
xx.xx.xx.xx/helloにアクセスして、コンテナログに「GlobalMiddleware start」と「GlobalMiddleware end」が出力されていればok

Program.cs
var app = builder.Build();
app.UseMiddleware<GlobalMiddleware>(); // これ追加
app.MapGet("/", () => "Hello, World!");
app.MapControllers();
app.Run();
src/Middleware/GlobalMiddleware.cs
public class GlobalMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task Invoke(HttpContext context)
    {
        Console.WriteLine("GlobalMiddleware start");

        await _next(context);

        Console.WriteLine("GlobalMiddleware end");
    }
}

特定ルートにだけ適用するミドルウェア

ついでに、Program.csにミドルウェアがまとまらないようにミドルウェア適用用のクラス作ってみる

Program.cs
var app = builder.Build();
app.RegisterMiddlewares(); // これ追加
app.MapGet("/", () => "Hello, World!");
app.MapControllers();
app.Run();
Middleware/MiddlewareFactory.cs
public static class MiddlewareFactory
{
    public static void RegisterMiddlewares(this IApplicationBuilder app)
    {
        MiddlewareFactory.RegisterGlobalMiddleware(app);
        MiddlewareFactory.RegisterHelloMiddleware(app);
    }

    private static void RegisterGlobalMiddleware(IApplicationBuilder app)
    {
        app.UseMiddleware<GlobalMiddleware>();
    }

    private static void RegisterHelloMiddleware(IApplicationBuilder app)
    {
        // /helloから始まるルート全部に適用
        app.UseWhen(
            context => context.Request.Path.StartsWithSegments("/hello", StringComparison.OrdinalIgnoreCase),
            appBuilder => {
                appBuilder.UseMiddleware<HelloMiddleware>();
            }
        );
    }
}
Middleware/HelloMiddleware.cs
public class HelloMiddleware
{
    private readonly RequestDelegate _next;

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

    public async Task Invoke(HttpContext context)
    {
        Console.WriteLine("HelloMiddleware start");

        await _next(context);

        Console.WriteLine("HelloMiddleware end");
    }
}

その他メモ

拡張メソッド

.NETではなくC#の言語機能で、既存のクラスに新しいメソッドを追加する機能だそう
Traitみたいなものか?
MiddlewareFactoryの関数をapp.RegisterMiddlewares()という呼び出し方ができているのはこれのおかげ
this修飾子をつけると、その後に設定したクラスに対して新しいメソッドを追加できる

ルート、アクション指定の処理指定方法

・useWhen以外にMapWhenもある
・Controller/Actionごとに設定するActionFilterというのもあるらしい

使い分けとか、動きの違いとか追って調査したい
ざっくりActionFilterの書き方こんな感じ

.cs
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("[controller]")]
public class HelloController : ControllerBase
{
    [HttpGet]
    [HelloMiddlewareFilter] // これ
    public string index() => "Hello index";
}

でも、ミドルウェアも完全パス指定できるらしい
どうしたもんだろう

.cs
app.UseWhen(
    context => context.Request.Path.StartsWithSegments("/hello"),
    appBuilder => {
        appBuilder.UseMiddleware<HelloMiddleware>();
    }
);

雑記

ここまで勉強してみて思ったけど、すごいlaravelに似てる
DIだし、まだやってないけどORMもあるみたいだし、ミドルウェアあるし
割と理解しやすいかも?
C#とPHPの言語の差はかなり感じるけど

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?