수색…
매개 변수
매개 변수 | 세부 |
---|---|
IDictionary<string,object> environment | 이것은 OWIN이 통화 중에 정보를 교환하는 유일한 컬렉션입니다. 모든 키는 https://docs.asp.net/en/latest/fundamentals/owin.html#owin-keys 에서 찾을 수 있습니다. |
비고
AppFunc
유형은 C ++의 typedef
와 매우 비슷하게 Func<IDictionary<string, object>, Task>
type의 별명이며 메소드 서명을 단축합니다.
요청 경로와 요청을 처리하는 데 걸린 시간 출력
//define a short alias to avoid chubby method signatures
using AppFunc = Func<IDictionary<string, object>, Task>;
class RequestTimeMiddleware
{
private AppFunc _next;
public RequestTimeMiddleware(AppFunc next)
{
_next = next;
}
public async Task Invoke(IDictionary<string, object> environment)
{
IOwinContext context = new OwinContext(environment);
var path = context.Request.Path;
var sw = Stopwatch.StartNew();
//Queue up the next middleware in the pipeline
await _next(environment);
//When the request comes back, log the elapsed time
Console.WriteLine($"Request for {path} processed in {sw.ElapsedMilliseconds}ms");
}
}
public static class RequestTimeMiddlewareExtensions
{
//Extension method as syntactic sugar, to get a meaningful way
//in adding the middleware to the pipeline
public static void UseRequestTimeMiddleware(this IAppBuilder app)
{
app.Use<RequestTimeMiddleware>();
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
//add the Middleware as early as possible
app.UseRequestTimeMiddleware();
//Queue up every other module
app.Use(async (environment, next) =>
{
await environment.Response.WriteAsync("Hello from the console world");
await next();
});
}
}
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow