NET Core中怎麼使用HttpContext.Current

來源:互聯網
上載者:User

標籤:conf   single   環境   blank   services   factory   blog   strong   return   

NET Core中怎麼使用HttpContext.Current

閱讀目錄

  • 一、前言
  • 二、IHttpContextAccessor
  • 三、HttpContextAccessor
回到目錄一、前言

  我們都知道,ASP.NET Core作為最新的架構,在MVC5和ASP.NET WebForm的基礎上做了大量的重構。如果我們想使用以前版本中的HttpContext.Current的話,目前是停用,因為ASP.NET Core中是並沒有這個API的。

  當然我們也可以通過在Controller中訪問HttpContext,但是某些情況下,這樣使用起來還是不如HttpContext.Current方便。

回到目錄二、IHttpContextAccessor

  利用ASP.NET Core的依賴注入容器系統,通過請求擷取IHttpContextAccessor介面,我們擁有類比使用HttpContext.Current這樣API的可能性。但是因為IHttpContextAccessor介面預設不是由依賴注入進行執行個體管理的。我們先要將它註冊到ServiceCollection中:

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  4. // Other code...
  5. }

  來類比一個HttpContext.Current吧:

  1. public static class HttpContext
  2. {
  3. public static IServiceProvider ServiceProvider;
  4. public static Microsoft.AspNetCore.Http.HttpContext Current
  5. {
  6. get
  7. {
  8. object factory = ServiceProvider.GetService(typeof(Microsoft.AspNetCore.Http.IHttpContextAccessor));
  9. Microsoft.AspNetCore.Http.HttpContext context = ((Microsoft.AspNetCore.Http.HttpContextAccessor)factory).HttpContext;
  10. return context;
  11. }
  12. }
  13. }

  其實說到HttpContext.Current就不得不提到多線程問題,在以前的ASP.NET版本中,如果遇到多線程環境很有可能HttpContext.Current為空白的情況。說到這個問題以前就是有解決方案的,那就是CallContext;

  CallContext 是類似於方法調用的執行緒區域儲存區的專用集合對象,並提供對每個邏輯執行線程都唯一的資料槽。資料槽不在其他邏輯線程上的調用上下文之間共用。當 CallContext 沿執行代碼路徑往返傳播並且由該路徑中的各個對象檢查時,可將對象添加到其中。

  當使用ASP.NET的時候,雖然線城池裡的線程是複用的,但是CallContext並不在一個線程的多次使用中共用。因為CallContext是針對邏輯線程的TLS,線程池中被複用的線程是作業系統中的核心對象而不是託管對象。就像資料庫連接池中儲存的是非託管資源而不是託管資源。因此,先後執行的兩個託管線程可能在底層複用了一個物理線程(核心對象),但並不能共用同一組CallContext資料槽。就像先後new的兩個SqlConnection對象可能在底層使用了同一個物理串連,但是託管對象的屬性已經被重設。

  與此對照的是ThreadStaticAttribute,標記上這個特性的靜態欄位是往物理線程的TLS中儲存資料(根據MSDN的描述猜的。具體沒試過),因此如果兩個託管線程對象內部使用的是同一個物理線程,則這個欄位會複用(在兩個線程通過這一欄位訪問同一個資料槽)。

  在.NET Core中,也有新的API選擇,AsyncLocal<T>。

回到目錄三、HttpContextAccessor

  我們來看看ASP.NET Core中的IHttpContextAccessor介面實現吧:

 

  1. public class HttpContextAccessor : IHttpContextAccessor
  2. {
  3. #if NET451
  4. private static readonly string LogicalDataKey = "__HttpContext_Current__" + AppDomain.CurrentDomain.Id;
  5. public HttpContext HttpContext
  6. {
  7. get
  8. {
  9. var handle = CallContext.LogicalGetData(LogicalDataKey) as ObjectHandle;
  10. return handle?.Unwrap() as HttpContext;
  11. }
  12. set
  13. {
  14. CallContext.LogicalSetData(LogicalDataKey, new ObjectHandle(value));
  15. }
  16. }
  17. #elif NETSTANDARD1_3
  18. private AsyncLocal<HttpContext> _httpContextCurrent = new AsyncLocal<HttpContext>();
  19. public HttpContext HttpContext
  20. {
  21. get
  22. {
  23. return _httpContextCurrent.Value;
  24. }
  25. set
  26. {
  27. _httpContextCurrent.Value = value;
  28. }
  29. }
  30. #endif
  31. }

 

  最後我只能說在ASP.NET Core中是萬物皆DI啊,其實Core中的實現早就為我們想好了這些功能,只是改變了使用方式。

 

GitHub:https://github.com/maxzhang1985/YOYOFx  如果覺還可以請Star下, 歡迎一起交流。

 

.NET Core 開源學習群: 214741894  

NET Core中怎麼使用HttpContext.Current

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.