欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間示例詳解

 更新時(shí)間:2019年06月23日 11:34:17   作者:Rohmeng  
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

ASP.NET Core通過(guò)RequestDelegate這個(gè)委托類(lèi)型來(lái)定義中間件

public delegate Task RequestDelegate(HttpContext context);

可將一個(gè)單獨(dú)的請(qǐng)求委托并行指定為匿名方法(稱(chēng)為并行中間件),或在類(lèi)中對(duì)其進(jìn)行定義??赏ㄟ^(guò)Use,或在Middleware類(lèi)中配置要傳遞給委托執(zhí)行的方法(參數(shù)類(lèi)型HttpContext,返回值類(lèi)型Task)。

public static IApplicationBuilder Use(this IApplicationBuilder app, Func<HttpContext, Func<Task>, Task> middleware);

public static IApplicationBuilder UseMiddleware<TMiddleware>(this IApplicationBuilder app, params object[] args);

通過(guò)定義一個(gè)中間件類(lèi) 來(lái)計(jì)算http請(qǐng)求的時(shí)間,例:

public class ResponseTimeMiddleware
{
  // Name of the Response Header, Custom Headers starts with "X-" 
  private const string RESPONSE_HEADER_RESPONSE_TIME = "X-Response-Time-ms";
  // Handle to the next Middleware in the pipeline 
  private readonly RequestDelegate _next;
  public ResponseTimeMiddleware(RequestDelegate next)
  {
    _next = next;
  }
  public Task InvokeAsync(HttpContext context)
  {
    // Start the Timer using Stopwatch 
    var watch = new Stopwatch();
    watch.Start();
    context.Response.OnStarting(() => {
      // Stop the timer information and calculate the time  
      watch.Stop();
      var responseTimeForCompleteRequest = watch.ElapsedMilliseconds;
      // Add the Response time information in the Response headers.  
      context.Response.Headers[RESPONSE_HEADER_RESPONSE_TIME] = responseTimeForCompleteRequest.ToString();
      return Task.CompletedTask;
    });
    // Call the next delegate/middleware in the pipeline  
    return this._next(context);
  }
}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論