Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实现同步方法的异步版本:如何返回任务<;int>;哪个是常数1?_C#_.net_Async Await - Fatal编程技术网

C# 实现同步方法的异步版本:如何返回任务<;int>;哪个是常数1?

C# 实现同步方法的异步版本:如何返回任务<;int>;哪个是常数1?,c#,.net,async-await,C#,.net,Async Await,我有一个sync方法,现在我想实现它的异步版本。幸运的是,底层调用已经有一个异步版本(dbSet.savechangesync()),但是在我的算法中有一个if分支返回常量literal 1 我不知道如何在异步版本中实现这一部分 同步版本: public virtual int Add(T entity) { SetLogContext(entity, _logctx); dbSet.Add(entity); if (isAuto

我有一个sync方法,现在我想实现它的异步版本。幸运的是,底层调用已经有一个异步版本(
dbSet.savechangesync()
),但是在我的算法中有一个if分支返回常量literal 1

我不知道如何在异步版本中实现这一部分

同步版本:

    public virtual int Add(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return ctx.SaveChanges();
        }
        return 1;
    }
    public virtual Task<int> AddAsync(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return ctx.SaveChangesAsync();
        }
        return ??? // What to write here?
    }
异步版本:

    public virtual int Add(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return ctx.SaveChanges();
        }
        return 1;
    }
    public virtual Task<int> AddAsync(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return ctx.SaveChangesAsync();
        }
        return ??? // What to write here?
    }
公共虚拟任务AddAsync(T实体) { SetLogContext(实体,_logctx); 添加(实体); 如果(是自治的) { 返回ctx.SaveChangesAsync(); } return???//在这里写什么? } 使用


您有两种可能实现您的需求:一种是使用
async
关键字:

public virtual async Task<int> AddAsync<T>(T entity)
{
    SetLogContext(entity, _logctx);
    dbSet.Add(entity);

    if (isAutonomous)
    {
        return await ctx.SaveChangesAsync();
    }
    return 1;
}
公共虚拟异步任务AddAsync(T实体)
{
SetLogContext(实体,_logctx);
添加(实体);
如果(是自治的)
{
return wait ctx.SaveChangesAsync();
}
返回1;
}

另一种方法是在您想要返回数字的位置使用
Task.FromResult(1)

我在这里看到的问题是,您创建了一个“Async”方法,它实际上不是
Async
。这可能会误导下一个使用它的人

正如@xxMUROxx已经提到的,一个“Async”方法的前缀应该是
Async
关键字

    public virtual async Task<int> AddAsync<T>(T entity)
    {
        SetLogContext(entity, _logctx);
        dbSet.Add(entity);

        if (isAutonomous)
        {
            return await ctx.SaveChangesAsync();
        }
        return 1;
    }
公共虚拟异步任务AddAsync(T实体)
{
SetLogContext(实体,_logctx);
添加(实体);
如果(是自治的)
{
return wait ctx.SaveChangesAsync();
}
返回1;
}
使用
async
await
模式的全部要点是:。当点击
wait
关键字时,该方法返回给调用者,并且通常返回到原始调用者。通常,这将是一个
异步void
事件处理程序,就像单击按钮一样

发生这种情况时,它允许调用线程在等待长时间运行的操作完成时继续执行。在长时间运行的操作完成后,代码将创建一个延续,并根据返回的结果继续执行任何其他代码


通过从方法中省略
async
关键字,可以有效地使调用再次同步。首先破坏了调用
savechangessync
的目的。

返回任务。FromResult(1)
Doomer:这似乎是一个答案,为什么不把它作为一个答案来写呢?当我阅读第一篇文章(async/await)时,可以找到文档,这是一种反模式的情况。。。它不需要再创建一个额外的状态机。当然,调用代码可以
await
方法返回的任务,而不管该方法是否使用async/await本身?这在文档中都有详细说明。>“wait关键字只能在标记为
async
的方法中使用”,但这与答案中的主要声明完全不同,即返回任务但不使用async/wait的方法不能异步调用。True,您可以异步运行从方法返回的任务,而不考虑
async
关键字,因为它委托给另一个
async
方法。我想我只是被方法命名的语义迷住了。我很抱歉。@craftworkgames我知道这很旧,但是命名约定建议开发人员将
Async
附加到任何返回
任务的方法,而不是暗示该方法实际上执行一些内部
Async
任务。我个人觉得命名惯例很愚蠢。它有匈牙利符号的味道,我们选择在内部省略它。我迫不及待地想让微软放弃它。