I was confronted with unexpected and strange behavior when working with async/await methods. Execution is breaking after calling await. There is no exceptions or async continue, just break and nothing. Here is code:
public async Task<IEnumerable<MailChimpUser>> GetAllMembersAsync(string listName) { try { //Breaks here var list = await GetListByName(listName); //Nevertheless, if extract method code //var lists = await _apiManager.Lists.GetAllAsync().ConfigureAwait(false); //list = lists.FirstOrDefault(l => l.Name == name); //the execution will continueя var members = await _apiManager.Members.GetAllAsync(list.Id).ConfigureAwait(false); var result = members.Select(m => new MailChimpUser() { ... } ); //the same behavior here, after the return it does not return to the method that awaiting it. return result; } catch (Exception ex) { var logger = LogManager.GetCurrentClassLogger(); logger.Error(ex, ex.Message, new object[] { }); throw ex; } } private async Task<MailChimp.Net.Models.List> GetListByName(string name) { try { var lists = await _apiManager.Lists.GetAllAsync().ConfigureAwait(false); var list = lists.FirstOrDefault(l => l.Name == name); if (list == null) throw new Exception(String.Format("MailChimp: List \"{0}\" not found", name)); return list; } catch (Exception ex) { var logger = LogManager.GetCurrentClassLogger(); logger.Error(ex, ex.Message, new object[] { }); throw ex; } } Can you tell me what I'm doing wrong and how can I fix it?
throw ex;should bethrow;.GetAllMembersAsync, onlyGetAllAsync.Resultof the returnedTasks, you may miss an exceptions thrown byGetListByName.