728x90
Custom Middleware에서 Scoped lifetime의 서비스를 주입받는 경우에는
Constructor가 아니라 InvokeAsync 메서드에서 주입받아야 한다.
public class MyCustomMiddleware
{
private readonly RequestDelegate _next;
public MyCustomMiddleware(RequestDelegate next)
{
_next = next;
}
// IMessageWriter is injected into InvokeAsync
public async Task InvokeAsync(HttpContext httpContext, IMessageWriter svc)
{
svc.Write(DateTime.Now.Ticks.ToString());
await _next(httpContext);
}
}
constructor에 파라미터를 추가했을 때 에러가 발생해서 찾아보는데 아래 문장 때문에 조금 헷갈렸다.
Additional parameters for the constructor and Invoke/InvokeAsync are populated by dependency injection (DI).
조금만 더 읽어볼걸...
돌고 돌아서 결국 MS documentation으로 돌아와서 알게 되었다.
Write custom ASP.NET Core middleware
Learn how to write custom ASP.NET Core middleware.
learn.microsoft.com
728x90
'Programming > .NET' 카테고리의 다른 글
[.NET] ML.NET 고양이 vs 개 분류(Image Classification) (0) | 2023.02.05 |
---|---|
[.NET] BenchmarkDotNet로 .NET 6 vs .NET 4.7.2 성능 비교 (0) | 2023.01.15 |
[.NET] Google Gmail SMTP 설정 방법 & 전송 기능 (0) | 2022.11.07 |
[.NET Core] Razor 파일 코드 변동사항 바로 적용되도록 설정하기 (0) | 2022.10.26 |
[.NET Core] wwwroot 폴더 밖에 있는 file download (0) | 2022.10.19 |
댓글