Exemple #1
0
 private void PostTestWorkRespose(IAsyncResult asyncResult)
 {
     try
     {
         DataServiceContext  asynchConnector = asyncResult.AsyncState as DataServiceContext;
         DataServiceResponse response        = asynchConnector.EndSaveChanges(asyncResult);
         foreach (ChangeOperationResponse changset in response)
         {
             if (changset.Error != null)
             {
                 throw changset.Error;
             }
         }
         SignalUI("TestStatus", "Sent Response");
     }
     catch (Exception exception)
     {
         string wrongError = "The context is already tracking a different entity with the same resource Uri.";
         if (!exception.InnerException.Message.Contains(wrongError))
         {
             SignalUI("TestStatus", "Failed : " + exception.StackTrace.ToString());
             SignalUI("TestStatus", "Failed : " + exception.InnerException.StackTrace.ToString());
             DataServiceContext dsc      = GetServiceContext();
             Message            response = new Message();
             response.ReturnValue = exception.ToString();
             response.WhoSentMe   = "SL";
             dsc.AddObject("Messages", response);
             dsc.BeginSaveChanges(PostTestWorkRespose, dsc);
         }
     }
 }
        public override object GetResults(IAsyncResult async)
        {
            object[]           state   = (object[])async.AsyncState;
            SaveChangesOptions option  = (SaveChangesOptions)state[0];
            DataServiceContext context = (DataServiceContext)state[1];

            return(context.EndSaveChanges(async));
        }
Exemple #3
0
        /// <summary>
        /// Asychronously saves changes in the data service by using the provided <see cref="T:Windows.UI.Core.CoreDispatcher"/>.
        /// </summary>
        /// <param name="context">The <see cref="T:System.Data.Services.Client.DataServiceContext"/> instance on which this extension method is enabled. </param>
        /// <param name="options">The save changes options emuneration, which supports flags.</param>
        /// <returns>A <see cref="T:System.Threading.Tasks.Task`1"/> that, when completed, returns the result as a <see cref="T:System.Data.Services.Client.DataServiceResponse"/>.</returns>
        public static async Task <DataServiceResponse> SaveChangesAsync(this DataServiceContext context, SaveChangesOptions options)
        {
            var queryTask = Task.Factory.FromAsync <DataServiceResponse>(
                context.BeginSaveChanges(options, null, null),
                (queryAsyncResult) =>
            {
                var results = context.EndSaveChanges(queryAsyncResult);
                return(results);
            });

            return(await queryTask);
        }
Exemple #4
0
        /// <summary>
        /// Asychronously saves changes in the data service by using the provided <see cref="T:Windows.UI.Core.CoreDispatcher"/>.
        /// </summary>
        /// <param name="context">The <see cref="T:System.Data.Services.Client.DataServiceContext"/> instance on which this extension method is enabled. </param>
        /// <param name="options">The save changes options emuneration, which supports flags.</param>
        /// <param name="dispatcher">The <see cref="T:Windows.UI.Core.CoreDispatcher"/> used to marshal the response back to the UI thread.</param>
        /// <returns>A <see cref="T:System.Threading.Tasks.Task`1"/> that, when completed, returns the result as a <see cref="T:System.Data.Services.Client.DataServiceResponse"/>.</returns>
        public static async Task <DataServiceResponse> SaveChangesAsync(this DataServiceContext context, SaveChangesOptions options, Windows.UI.Core.CoreDispatcher dispatcher)
        {
            var tcs = new TaskCompletionSource <DataServiceResponse>();

            context.BeginSaveChanges(async iar =>
            {
                await dispatcher.RunAsync(
                    Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    try
                    {
                        tcs.SetResult(context.EndSaveChanges(iar));
                    }
                    catch (DataServiceRequestException ex)
                    {
                        throw ex;
                    }
                });
            }, null);
            return(await tcs.Task);
        }
        public static DataServiceResponse SaveChanges(DataServiceContext context, SaveChangesOptions options, SaveChangesMode saveMode)
        {
            IList <EntityDescriptor> entites         = (from e in context.Entities where e.State != EntityStates.Unchanged select e).ToArray();
            IList <LinkDescriptor>   links           = (from e in context.Links where e.State != EntityStates.Unchanged select e).ToArray();
            IList <StreamDescriptor> streams         = context.Entities.SelectMany(e => e.StreamDescriptors).Where(s => s.State != EntityStates.Unchanged).ToArray();
            IList <EntityDescriptor> deletedEntities = (from e in entites where e.State == EntityStates.Deleted select e).ToArray();
            IList <LinkDescriptor>   deletedLinks    = (from e in links where e.State == EntityStates.Deleted select e).ToArray();

            DataServiceResponse response = null;

            switch (saveMode)
            {
            case SaveChangesMode.Synchronous:
                response = context.SaveChanges(options);
                break;

            case SaveChangesMode.AsyncWaitOnAsyncWaitHandle:
            {
                IAsyncResult async = context.BeginSaveChanges(options, null, null);
                if (!async.CompletedSynchronously)
                {
                    Assert.IsTrue(async.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginSaveChanges {0} timeout", options);
                }

                Assert.IsTrue(async.IsCompleted);
                response = context.EndSaveChanges(async);
                break;
            }

            case SaveChangesMode.AsyncCallback:
            {
                SaveChangesCallback callback = new SaveChangesCallback();
                IAsyncResult        async    = context.BeginSaveChanges(options, callback.CallbackMethod, new object[] { options, context });

                Assert.IsTrue(callback.Finished.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginSaveChanges {0} Asyncallback timeout", options);
                Assert.IsTrue(async.IsCompleted);

                if (null != callback.CallbackFailure)
                {
                    Assert.IsNull(callback.CallbackResult, callback.CallbackFailure.ToString());
                    throw callback.CallbackFailure;
                }

                response = (DataServiceResponse)callback.CallbackResult;
                break;
            }

            default:
                Assert.Fail("shouldn't be here");
                break;
            }

            int entityIndex = 0;
            int linkIndex   = 0;
            int streamIndex = 0;

            if (options == SaveChangesOptions.BatchWithSingleChangeset)
            {
                Assert.AreEqual <int>(response.BatchStatusCode, (int)HttpStatusCode.Accepted, "Expecting 202 as the status code for batch requests");
                Assert.IsTrue(response.BatchHeaders["Content-Type"].StartsWith("multipart/mixed; boundary=batchresponse_"), "expecting content type to be multipart mixed with a boundary value");
                Assert.IsTrue(response.IsBatchResponse, "Expecting response to be batch response");
            }
            else
            {
                Assert.AreEqual <int>(response.BatchStatusCode, -1, "expecting status code to be zero");
                Assert.IsTrue(response.BatchHeaders.Count == 0, "expecting no header information");
                Assert.IsFalse(response.IsBatchResponse, "expecting this to be non batch response");
            }

            foreach (ChangeOperationResponse changeset in response)
            {
                EntityStates state;
                bool         wasDeletedState;

                if (changeset.Descriptor is EntityDescriptor)
                {
                    EntityDescriptor tor = (EntityDescriptor)changeset.Descriptor;
                    state           = tor.State;
                    wasDeletedState = deletedEntities.Contains(tor);

                    // for MLE, more than one request can be sent for the same entity descriptor
                    if (entites.Count > entityIndex && Object.ReferenceEquals(entites[entityIndex].Entity, tor.Entity))
                    {
                        entityIndex++;
                    }
                    else
                    {
                        Assert.IsTrue(Object.ReferenceEquals(tor.Entity, entites[entityIndex - 1].Entity), "For MLE, it must match with the previous request");
                    }

                    Assert.IsNull(changeset.Error);
                }
                else if (changeset.Descriptor is LinkDescriptor)
                {
                    LinkDescriptor tor = (LinkDescriptor)changeset.Descriptor;
                    state           = tor.State;
                    wasDeletedState = deletedLinks.Contains(tor);

                    Assert.AreSame(tor.Source, links[linkIndex].Source);
                    Assert.AreEqual(tor.SourceProperty, links[linkIndex].SourceProperty);
                    Assert.AreSame(tor.Target, links[linkIndex].Target);
                    Assert.IsNull(changeset.Error);
                    linkIndex++;
                }
                else
                {
                    Assert.IsTrue(changeset.Descriptor is StreamDescriptor, "Must be stream descriptor");
                    if (streams.Count > streamIndex && streams.Contains(changeset.Descriptor))
                    {
                        streamIndex++;
                    }

                    state           = changeset.Descriptor.State;
                    wasDeletedState = false;
                }

                if (changeset.Error != null)
                {
                    Assert.AreNotEqual(EntityStates.Unchanged, state);
                }
                else
                {
                    if (wasDeletedState)
                    {
                        Assert.AreEqual(EntityStates.Detached, state);
                    }
                    else
                    {
                        Assert.AreEqual(EntityStates.Unchanged, state);
                    }
                }
            }

            Assert.AreEqual(entites.Count, entityIndex, "entities SaveChangesOptions.{0}", options);
            Assert.AreEqual(links.Count, linkIndex, "links SaveChangesOptions.{0}", options);
            Assert.AreEqual(streams.Count, streamIndex, "streams SaveChangesOptions.{0}", options);

            entites = context.Entities;
            links   = context.Links;
            return(response);
        }
Exemple #6
0
        internal void SaveChanges(DataServiceContext ctx)
        {
            var ar = ctx.BeginSaveChanges(null, null).EnqueueWait(this);

            ctx.EndSaveChanges(ar);
        }
 /// <summary>
 /// Extension method to perform sync/async version of DataServiceContext.SaveChanges dynamically
 /// </summary>
 /// <param name="context">The context to save changes on</param>
 /// <param name="continuation">The asynchronous continuation</param>
 /// <param name="async">A value indicating whether or not to use async API</param>
 /// <param name="options">The save changes options to use or null to use the overload without options</param>
 /// <param name="onCompletion">A callback for when the call completes</param>
 public static void SaveChanges(this DataServiceContext context, IAsyncContinuation continuation, bool async, SaveChangesOptions?options, Action <DataServiceResponse> onCompletion)
 {
     ExceptionUtilities.CheckArgumentNotNull(context, "context");
     if (options == null)
     {
         AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceResponse>(continuation, async, () => context.SaveChanges(), c => context.BeginSaveChanges(c, null), r => context.EndSaveChanges(r), onCompletion);
     }
     else
     {
         AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceResponse>(continuation, async, () => context.SaveChanges(options.Value), c => context.BeginSaveChanges(options.Value, c, null), r => context.EndSaveChanges(r), onCompletion);
     }
 }
Exemple #8
0
 internal void SaveChanges(DataServiceContext ctx)
 {
          var ar = ctx.BeginSaveChanges(null, null).EnqueueWait(this);
         ctx.EndSaveChanges(ar);
 }