示例#1
0
        internal static T FindByIdAsync <T>(ServiceContext context, T entity) where T : IEntity
        {
            //Initializing the Dataservice object with ServiceContext
            DataService.DataService service = new DataService.DataService(context);

            bool isFindById = false;

            IdsException exp = null;

            // Used to signal the waiting test thread that a async operation have completed.
            ManualResetEvent manualEvent = new ManualResetEvent(false);

            T returnedEntity = default(T);

            // Async callback events are anonomous and are in the same scope as the test code,
            // and therefore have access to the manualEvent variable.
            service.OnFindByIdAsyncCompleted += (sender, e) =>
            {
                Assert.IsNotNull(e);
                Assert.IsNotNull(e.Entity);
                manualEvent.Set();
                isFindById     = true;
                returnedEntity = (T)e.Entity;
            };

            // Call the service method
            service.FindByIdAsync <T>(entity);
            manualEvent.WaitOne(60000, false); Thread.Sleep(10000);

            // Check if we completed the async call, or fail the test if we timed out.
            if (!isFindById)
            {
                Assert.Fail("FindByID Failed");
            }

            if (exp != null)
            {
                throw exp;
            }

            // Set the event to non-signaled before making next async call.
            manualEvent.Reset();
            return(returnedEntity);
        }