解决await卡死问题

程序中使用了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

参考:

http://www.mamicode.com/info-detail-1467856.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值