欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

ASP.NET

當(dāng)前位置:主頁(yè) > 網(wǎng)絡(luò)編程 > ASP.NET >

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

來源:本站原創(chuàng)|時(shí)間:2020-01-11|欄目:ASP.NET|點(diǎn)擊: 次

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

public delegate Task RequestDelegate(HttpContext context);

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

通過定義一個(gè)中間件類 來計(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ì)我們的支持。

上一篇:asp.net core集成JWT的步驟記錄

欄    目:ASP.NET

下一篇:WCF如何綁定netTcpBinding寄宿到控制臺(tái)應(yīng)用程序詳解

本文標(biāo)題:ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間示例詳解

本文地址:http://mengdiqiu.com.cn/a1/ASP_NET/10913.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有