private void SetChangeSetProperty(RestierChangeSetProperty changeSetProperty)
 {
     foreach (HttpRequestMessage request in this.Requests)
     {
         request.SetChangeSet(changeSetProperty);
     }
 }
예제 #2
0
        /// <summary>
        /// Asynchronously sends the request.
        /// </summary>
        /// <param name="invoker">The invoker.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The task object that contains the batch response.</returns>
        public override async Task <ODataBatchResponseItem> SendRequestAsync(
            HttpMessageInvoker invoker,
            CancellationToken cancellationToken)
        {
            Ensure.NotNull(invoker, "invoker");

            RestierChangeSetProperty changeSetProperty = new RestierChangeSetProperty(this);

            changeSetProperty.ChangeSet = new ChangeSet();
            this.SetChangeSetProperty(changeSetProperty);

            Dictionary <string, string>        contentIdToLocationMapping = new Dictionary <string, string>();
            List <Task <HttpResponseMessage> > responseTasks = new List <Task <HttpResponseMessage> >();

            foreach (HttpRequestMessage request in Requests)
            {
                responseTasks.Add(SendMessageAsync(invoker, request, cancellationToken, contentIdToLocationMapping));
            }

            // the responseTasks will be complete after:
            // - the ChangeSet is submitted
            // - the responses are created and
            // - the controller actions have returned
            await Task.WhenAll(responseTasks);

            List <HttpResponseMessage> responses = new List <HttpResponseMessage>();

            try
            {
                foreach (Task <HttpResponseMessage> responseTask in responseTasks)
                {
                    HttpResponseMessage response = responseTask.Result;
                    if (response.IsSuccessStatusCode)
                    {
                        responses.Add(response);
                    }
                    else
                    {
                        DisposeResponses(responses);
                        responses.Clear();
                        responses.Add(response);
                        return(new ChangeSetResponseItem(responses));
                    }
                }
            }
            catch
            {
                DisposeResponses(responses);
                throw;
            }

            return(new ChangeSetResponseItem(responses));
        }
        /// <summary>
        /// Asynchronously sends the request.
        /// </summary>
        /// <param name="invoker">The invoker.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The task object that contains the batch response.</returns>
        public override async Task<ODataBatchResponseItem> SendRequestAsync(
            HttpMessageInvoker invoker,
            CancellationToken cancellationToken)
        {
            Ensure.NotNull(invoker, "invoker");

            RestierChangeSetProperty changeSetProperty = new RestierChangeSetProperty(this);
            changeSetProperty.ChangeSet = new ChangeSet();
            this.SetChangeSetProperty(changeSetProperty);

            Dictionary<string, string> contentIdToLocationMapping = new Dictionary<string, string>();
            List<Task<HttpResponseMessage>> responseTasks = new List<Task<HttpResponseMessage>>();
            foreach (HttpRequestMessage request in Requests)
            {
                responseTasks.Add(SendMessageAsync(invoker, request, cancellationToken, contentIdToLocationMapping));
            }

            // the responseTasks will be complete after:
            // - the ChangeSet is submitted
            // - the responses are created and
            // - the controller actions have returned
            await Task.WhenAll(responseTasks);

            List<HttpResponseMessage> responses = new List<HttpResponseMessage>();
            try
            {
                foreach (Task<HttpResponseMessage> responseTask in responseTasks)
                {
                    HttpResponseMessage response = responseTask.Result;
                    if (response.IsSuccessStatusCode)
                    {
                        responses.Add(response);
                    }
                    else
                    {
                        DisposeResponses(responses);
                        responses.Clear();
                        responses.Add(response);
                        return new ChangeSetResponseItem(responses);
                    }
                }
            }
            catch
            {
                DisposeResponses(responses);
                throw;
            }

            return new ChangeSetResponseItem(responses);
        }
 /// <summary>
 /// Sets the <see cref="RestierChangeSetProperty"/> to the <see cref="HttpRequestMessage"/>.
 /// </summary>
 /// <param name="request">The HTTP request.</param>
 /// <param name="changeSetProperty">The change set to be set.</param>
 public static void SetChangeSet(this HttpRequestMessage request, RestierChangeSetProperty changeSetProperty)
 {
     Ensure.NotNull(request, "request");
     request.Properties.Add(ChangeSetKey, changeSetProperty);
 }
        /// <summary>
        /// Asynchronously sends the request.
        /// </summary>
        /// <param name="invoker">The invoker.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The task object that contains the batch response.</returns>
        public override async Task <ODataBatchResponseItem> SendRequestAsync(
            HttpMessageInvoker invoker,
            CancellationToken cancellationToken)
        {
            Ensure.NotNull(invoker, "invoker");

            RestierChangeSetProperty changeSetProperty = new RestierChangeSetProperty(this);

            changeSetProperty.ChangeSet = new ChangeSet();
            this.SetChangeSetProperty(changeSetProperty);

            Dictionary <string, string> contentIdToLocationMapping = new Dictionary <string, string>();
            var responseTasks = new List <Task <Task <HttpResponseMessage> > >();

            foreach (HttpRequestMessage request in Requests)
            {
                // Since exceptions may occure before the request is sent to RestierController,
                // we must catch the exceptions here and call OnChangeSetCompleted,
                // so as to avoid deadlock mentioned in Github Issue #82.
                TaskCompletionSource <HttpResponseMessage> tcs = new TaskCompletionSource <HttpResponseMessage>();
                var task =
                    SendMessageAsync(invoker, request, cancellationToken, contentIdToLocationMapping)
                    .ContinueWith(
                        t =>
                {
                    if (t.Exception != null)
                    {
                        var taskEx = (t.Exception.InnerExceptions != null &&
                                      t.Exception.InnerExceptions.Count == 1)
                                        ? t.Exception.InnerExceptions.First()
                                        : t.Exception;
                        changeSetProperty.Exceptions.Add(taskEx);
                        changeSetProperty.OnChangeSetCompleted(request);
                        tcs.SetException(taskEx);
                    }
                    else
                    {
                        tcs.SetResult(t.Result);
                    }

                    return(tcs.Task);
                },
                        cancellationToken);

                responseTasks.Add(task);
            }

            // the responseTasks will be complete after:
            // - the ChangeSet is submitted
            // - the responses are created and
            // - the controller actions have returned
            await Task.WhenAll(responseTasks);

            List <HttpResponseMessage> responses = new List <HttpResponseMessage>();

            try
            {
                foreach (var responseTask in responseTasks)
                {
                    HttpResponseMessage response = responseTask.Result.Result;
                    if (response.IsSuccessStatusCode)
                    {
                        responses.Add(response);
                    }
                    else
                    {
                        DisposeResponses(responses);
                        responses.Clear();
                        responses.Add(response);
                        return(new ChangeSetResponseItem(responses));
                    }
                }
            }
            catch
            {
                DisposeResponses(responses);
                throw;
            }

            return(new ChangeSetResponseItem(responses));
        }
 private void SetChangeSetProperty(RestierChangeSetProperty changeSetProperty)
 {
     foreach (HttpRequestMessage request in this.Requests)
     {
         request.SetChangeSet(changeSetProperty);
     }
 }