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表達式內使用。代表在哪裏設置等待行動
現在我應該在哪裏使用異步關鍵字來解決此錯誤。
我想最好是在委託人 – 2017-02-27 17:11:31