HttpContext.Items を使用して、パイプライン内で任意の値を渡すことができます:
context.Items["some"] = "value";
より良い解決策は、範囲指定されたサービスを使用することです。これを見てください:リクエストごとのミドルウェアの依存関係
コードは次のようになります:
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext, IImpersonatorRepo imperRepo)
{
imperRepo.MyProperty = 1000;
await _next(httpContext);
}
}
次に、ImpersonatorRepo を次のように登録します。
services.AddScoped<IImpersonatorRepo, ImpersonatorRepo>()