程序中使用了Dapper,在执行
return await this.Connection.QueryFirstOrDefaultAsync<T>(...);时出现卡死现象,连接mysql不会出现这个问题,连接Sqlserver数据库会出现,以为是Dapper中的SqlMapper中的问题,网上也有说是SqlMapper中方法QueryFirstOrDefaultAsync有问题,然后下载了Dapper的源码,发现其await的所有方法后都加上了.ConfigureAwait(false); 然后果断在调用的方法后加上.ConfigureAwait(false); 访问mysql和sqlserver均不再卡死,
ConfigureAwait
// 参数:
// continueOnCapturedContext:
// 尝试将延续任务封送回原始上下文,则为 true;否则为 false。
public async Task<ActionResult> Index()
{
bool HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // false
bool SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // false
System.Web.HttpContext.Current.Session["test"] = "Index";
string VALUE = await GETVALUE().ConfigureAwait(false);
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
NormalFun();
return View();
}
private bool NormalFun()
{
bool HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
bool SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
return true;
}
private async Task<string> GETVALUE()
{
var test = System.Web.HttpContext.Current.Session["test"]; // index
bool HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // false
bool SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // false
await Task.Run(async () =>
{
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
//Thread.Sleep(3000);
await GETVALUE2();
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
}).ConfigureAwait(false);
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
return "string";
}
private async Task<string> GETVALUE2()
{
//var test = System.Web.HttpContext.Current.Session["test"]; // 未将对象引用到实例
bool HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
bool SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
await Task.Run(() =>
{
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
Thread.Sleep(3000);
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
}).ConfigureAwait(false);
HttpContextIsNull = System.Web.HttpContext.Current == null ? true : false; // true
SynchronizationContextIsNull = SynchronizationContext.Current == null ? true : false; // true
return "string";
}
A有ConfigureAwait(false),则HttpContextIsNull,SynchronizationContextIsNull 为空,即如果要在await之后使用System.Web.HttpContext.Current或SynchronizationContext.Current ,则不应该配置ConfigureAwait(false),但GETVALUE()方法中可以加ConfigureAwait(false),因为在B之后没有使用System.Web.HttpContext.Current,如使用,则需要去掉或设为true
参考: