无法从标记为async </t>的方法返回IObservable <t>

时间:2013-12-03 20:59:43

标签: c# async-await system.reactive

这个人为的例子大致是我的代码的结构:

public abstract class SuperHeroBase
{
    protected SuperHeroBase() { }

    public async Task<CrimeFightingResult> FightCrimeAsync()
    {
        var result = new CrimeFightingResult();

        result.State = CrimeFightingStates.Fighting;

        try
        {
            await FightCrimeOverride(results);
        } 
        catch
        {
            SetError(results);
        }

        if (result.State == CrimeFightingStates.Fighting)
            result.State = CrimeFightingStates.GoodGuyWon;

        return result;
    }

    protected SetError(CrimeFightingResult results)
    {
        result.State = CrimeFightingStates.BadGuyWon;
    }

    protected abstract Task FightCrimeOverride(CrimeFightingResult results);
}

public enum CrimeFightingStates
{
    NotStarted,
    Fighting,
    GoodGuyWon, // success state
    BadGuyWon // error state
}

public class CrimeFightingResult
{
    internal class CrimeFightingResult() { }

    public CrimeFightingStates State { get; internal set; }
}

现在我正在尝试构建一个可以容纳多个SuperHero对象并提供AllHerosFightCrime方法的集合。英雄不应该一次全部战斗(下一个在第一个完成时开始)。

public class SuperHeroCollection : ObservableCollection<SuperHeroBase>
{
    public SuperHeroCollection() { }

    // I mark the method async...
    public async IObservable<CrimeFightingResult> AllHerosFightCrime()
    {
        var heros = new List<SuperHeroBase>(this);

        var results = new ReplaySubject<CrimeFightingResult>();

        foreach (var hero in heros)
        {
            // ... so I can await on FightCrimeAsync and push 
            // the result to the subject when done
            var result = await hero.FightCrimeAsync();
            results.OnNext(result);
        }

        results.OnCompleted();

        // I can't return the IObservable here because the method is marked Async.
        // It expects a return type of CrimeFightingResult
        return results;
    }
}

如何退回IObservable<CrimeFightingResults>并仍然等待FightCrimeAsync的电话?

1 个答案:

答案 0 :(得分:4)

您可以将任务转换为可观察对象,并使用Merge

进行组合
public IObservable<CrimeFightingResult> AllHerosFightCrime()
{
    var heros = new List<SuperHeroBase>(this);
    return heros.Select(h => h.FightCrimeAsync().ToObservable())
        .Merge();
}

如果您想维持收到活动的顺序,可以使用Concat代替Merge