2021年11月17日 星期三

使用 await 等待非同步函數問題

MVC
====
若controller 有全域 DbContext 物件,執行 await 後會造成 物件自動dispose
解決方式為不使用全域DbContext 物件,需要時再透過 using 建立


webforms
====
webmethod 執行 await 後就無法再取得 System.Web.HttpContext.Current,必須先用變數記錄起來,await 之後再填回
var httpcontext = HttpContext.Current;
await xxxAsync().ConfigureAwait(false);
HttpContext.Current = httpcontext;

沒有留言:

自訂權限驗證機制

// 使用 filter [Route("api/[controller]")] [ApiController] [Authorize] [TypeFilter(typeof(CustomAsyncAuthorizationFilter))] public c...