Suche…
Parameter
Parameter | Einzelheiten |
---|---|
IDictionary<string,object> environment | Dies ist die einzige Sammlung, in der OWIN während eines Anrufs Informationen übermittelt. Alle Schlüssel finden Sie unter https://docs.asp.net/de/latest/fundamentals/owin.html#owin-keys |
Bemerkungen
Der AppFunc
Typ ist nur ein Alias für den Typ Func<IDictionary<string, object>, Task>
, um Methodensignaturen zu verkürzen, ähnlich wie in C ++ typedef
.
Geben Sie den Anforderungspfad und die für die Verarbeitung benötigte Zeit aus
//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
Lizenziert unter CC BY-SA 3.0
Nicht angeschlossen an Stack Overflow