-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathRestierBatchChangeSetRequestItem.cs
More file actions
163 lines (147 loc) · 6.12 KB
/
RestierBatchChangeSetRequestItem.cs
File metadata and controls
163 lines (147 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.OData.Batch;
using Microsoft.Restier.Core;
using Microsoft.Restier.Core.Submit;
namespace Microsoft.Restier.AspNet.Batch
{
/// <summary>
/// Represents an API <see cref="ChangeSet"/> request.
/// </summary>
public class RestierBatchChangeSetRequestItem : ChangeSetRequestItem
{
/// <summary>
/// An Api.
/// </summary>
private readonly ApiBase api;
/// <summary>
/// Initializes a new instance of the <see cref="RestierBatchChangeSetRequestItem" /> class.
/// </summary>
/// <param name="api">An Api.</param>
/// <param name="requests">The request messages.</param>
public RestierBatchChangeSetRequestItem(ApiBase api, IEnumerable<HttpRequestMessage> requests)
: base(requests)
{
Ensure.NotNull(api, nameof(api));
this.api = api;
}
/// <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, nameof(invoker));
var changeSetProperty = new RestierChangeSetProperty(this)
{
ChangeSet = new ChangeSet()
};
SetChangeSetProperty(changeSetProperty);
var contentIdToLocationMapping = new ConcurrentDictionary<string, string>();
var responseTasks = new List<Task<Task<HttpResponseMessage>>>();
foreach (var request in Requests)
{
// Since exceptions may occurs 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.
var tcs = new TaskCompletionSource<HttpResponseMessage>();
var task = SendMessageAsync(invoker, request, cancellationToken, contentIdToLocationMapping)
.ContinueWith(t =>
{
if (t.Exception is not null)
{
var taskEx = (t.Exception.InnerExceptions is not null &&
t.Exception.InnerExceptions.Count == 1)
? t.Exception.InnerExceptions.First()
: t.Exception;
changeSetProperty.Exceptions.Add(taskEx);
changeSetProperty.OnChangeSetCompleted();
tcs.SetException(taskEx.Demystify());
}
else
{
tcs.SetResult(t.Result);
}
return tcs.Task;
},
cancellationToken,
TaskContinuationOptions.None,
TaskScheduler.Current);
responseTasks.Add(task);
}
// the responseTasks will be complete after:
// - the ChangeSet is submitted
// - the responses are created and
// - the controller actions have returned
foreach (var responseTask in responseTasks)
{
await responseTask.ConfigureAwait(false);
}
var responses = new List<HttpResponseMessage>();
try
{
foreach (var responseTask in responseTasks)
{
var 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);
}
/// <summary>
/// Asynchronously submits a <see cref="ChangeSet"/>.
/// </summary>
/// <param name="changeSet">The change set to submit.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
#pragma warning disable CA1822 // Do not declare static members on generic types
internal async Task SubmitChangeSet(ChangeSet changeSet)
#pragma warning restore CA1822 // Do not declare static members on generic types
{
var submitResults = await api.SubmitAsync(changeSet).ConfigureAwait(false);
}
private static void DisposeResponses(IEnumerable<HttpResponseMessage> responses)
{
foreach (var response in responses)
{
if (response is not null)
{
response.Dispose();
}
}
}
private void SetChangeSetProperty(RestierChangeSetProperty changeSetProperty)
{
foreach (var request in Requests)
{
request.SetChangeSet(changeSetProperty);
}
}
}
}