//// GET api/authors //public IEnumerable<Author> Get() //{ // var repo = new AuthorRepository(); // return repo.GetAuthors(); //} // GET api/authors public Task <FullResponse> Get() { var authorRepo = new AuthorRepository(); var titleRepo = new TitleRepository(); var tcs = new TaskCompletionSource <FullResponse>(); var response = new FullResponse(); int outstandingOperations = 2; Task.Factory.FromAsync(authorRepo.BeginGetAuthors(null, null), iar => { response.Authors = authorRepo.EndGetAuthors(iar); int currentCount = Interlocked.Decrement(ref outstandingOperations); if (currentCount == 0) { tcs.SetResult(response); } }); Task.Factory.FromAsync(titleRepo.BeginGetTitles(null, null), iar => { response.Titles = titleRepo.EndGetTitles(iar); int currentCount = Interlocked.Decrement(ref outstandingOperations); if (currentCount == 0) { tcs.SetResult(response); } }); return(tcs.Task); }
private IAsyncResult StartGetData(object sender, EventArgs e, AsyncCallback cb, object extradata) { asyncResult = new CountingAsyncResult(cb, extradata, 2); SynchronizationContext ctx = SynchronizationContext.Current; authorIar = authorRepo.BeginGetAuthors(iar => { try { // OperationThatCouldThrowException(); } catch (Exception x) { ctx.Send(_ => { throw new WrapperException("An error occurred during processing", x); }, null); } finally { asyncResult.OperationComplete(); } //ctx.Post(_ => // { // try // { // OperationThatCouldThrowException(); // } // finally // { // asyncResult.OperationComplete(); // } // }, null); }, null); titleIar = titleRepo.BeginGetTitles(iar => { asyncResult.OperationComplete(); }, null); return(asyncResult); }
public IAsyncResult BeginGetAuthorsAndTitles(AsyncCallback callback, object state) { var tcs = new TaskCompletionSource <FullDetails>(state); int outstandingOperations = 2; authorRepo.BeginGetAuthors(iar => { try { response.Authors = authorRepo.EndGetAuthors(iar) .Select(a => new AuthorDTO { FirstName = a.FirstName, LastName = a.LastName }) .ToList(); } catch (Exception x) { faultMessage = "Error retrieving authors"; } finally { int currentOutstanding = Interlocked.Decrement(ref outstandingOperations); if (currentOutstanding == 0) { tcs.SetResult(response); callback(tcs.Task); } } }, null); titleRepo.BeginGetTitles(iar => { try { response.Titles = titleRepo.EndGetTitles(iar) .Select(a => new TitleDTO() { Name = a.Name, Price = a.Price == 0.0m ? (decimal?)null : a.Price }) .ToList(); } catch (Exception x) { faultMessage = "Error retrieving titles"; } finally { int currentOutstanding = Interlocked.Decrement(ref outstandingOperations); if (currentOutstanding == 0) { tcs.SetResult(response); callback(tcs.Task); } } }, null); return(tcs.Task); }