internal static T AddAsync <T>(ServiceContext context, T entity) where T : IEntity { //Initializing the Dataservice object with ServiceContext DataService.DataService service = new DataService.DataService(context); bool isAdded = false; IdsException exp = null; T actual = (T)Activator.CreateInstance(entity.GetType()); // Used to signal the waiting test thread that a async operation have completed. ManualResetEvent manualEvent = new ManualResetEvent(false); // Async callback events are anonomous and are in the same scope as the test code, // and therefore have access to the manualEvent variable. service.OnAddAsyncCompleted += (sender, e) => { isAdded = true; manualEvent.Set(); if (e.Error != null) { exp = e.Error; } if (exp == null) { if (e.Entity != null) { actual = (T)e.Entity; } } }; // Call the service method service.AddAsync(entity); manualEvent.WaitOne(30000, false); Thread.Sleep(10000); if (exp != null) { throw exp; } // Check if we completed the async call, or fail the test if we timed out. if (!isAdded) { Assert.Fail("Adding Entity Failed"); } // Set the event to non-signaled before making next async call. manualEvent.Reset(); return(actual); }