2017-02-27 26 views
0
public enum SyncListsEnum 
{ 
    Country = 1, 
    Language = 2 
} 

public class SyncListsService 
{ 
    private readonly WeCandidateContext _db; 
    private readonly IRestClientFactory _factory; 
    private readonly IConfigurationService _configService; 

    private readonly Dictionary<SyncListsEnum, Action<IList<ExpandoObject>>> _syncLists; 

    public SyncListsService(WeCandidateContext db, IRestClientFactory factory, IConfigurationService configService) 
    { 
     _db = db; 
     _factory = factory; 
     _configService = configService; 

     _syncLists = new Dictionary<SyncListsEnum, Action<IList<ExpandoObject>>>() 
     { 
      {SyncListsEnum.Country, items =>{ new CountrySync(_db).SyncItems(items); _db.SaveChangesAsync();}}, 
      {SyncListsEnum.Language, items =>{ new LanguageSync(_db).SyncItems(items); _db.SaveChangesAsync();}} 
     }; 
    } 

    public async Task SyncList(string listName) 
    { 
     SyncListsEnum list; 
     if (!Enum.TryParse(listName, true, out list)) return; 


     // get list content from Recruiter 
     var items = await GetListFromRecruiter(); 
     // call a specific routine for each list to handle insert/update/delete 
     if (items != null) 
     { 
      // Synchronizes the local table with items retrieced from Recruiter. 
      this._syncLists[list](items); 
     } 
    } 
} 

在此代碼中所有都是正確的,但我需要await _db.SaveChangesAsync()而不是_db.SaveChangesAsync()。如果我把等待運營商在_db.SaveChangesAsync()前,它顯示我像等錯誤等待操作員只能在異步lamda表達式內使用。代表在哪裏設置等待行動

現在我應該在哪裏使用異步關鍵字來解決此錯誤。

+0

我想最好是在委託人 – 2017-02-27 17:11:31

回答

1

async放在你的lambda表達式前面,然後你可以使用await。請參閱「can not await async lambda

+3

或沒有添加'await'的情況下返回'Task'或者這個:http://stackoverflow.com/questions/14015319/where-do-i-mark -a-λ-表達-異步 – adam0101