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);
}
}
}
In this code all are correct but i need await _db.SaveChangesAsync()
instead of _db.SaveChangesAsync()
. If i put await operator in front of _db.SaveChangesAsync(), it shows me an error like await operator can only be used within an async lamda expression.
Now where should i use async keyword to resolved this error.