-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathRestierBatchChangeSetRequestItem.cs
More file actions
142 lines (127 loc) · 5.56 KB
/
RestierBatchChangeSetRequestItem.cs
File metadata and controls
142 lines (127 loc) · 5.56 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
// 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.Threading.Tasks;
using Microsoft.AspNet.OData.Batch;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.Restier.Core;
using Microsoft.Restier.Core.Submit;
namespace Microsoft.Restier.AspNetCore.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="contexts">The request messages.</param>
public RestierBatchChangeSetRequestItem(ApiBase api, IEnumerable<HttpContext> contexts)
: base(contexts)
{
Ensure.NotNull(api, nameof(api));
this.api = api;
}
/// <summary>
/// Asynchronously sends the request.
/// </summary>
/// <param name="handler">The handler for processing a message.</param>
/// <returns>The task object that contains the batch response.</returns>
public override async Task<ODataBatchResponseItem> SendRequestAsync(RequestDelegate handler)
{
Ensure.NotNull(handler, nameof(handler));
var changeSetProperty = new RestierChangeSetProperty(this)
{
ChangeSet = new ChangeSet(),
};
SetChangeSetProperty(changeSetProperty);
var contentIdToLocationMapping = new ConcurrentDictionary<string, string>();
var responseTasks = new List<Task<Task<HttpContext>>>();
foreach (var context in Contexts)
{
// Since exceptions may occur 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<HttpContext>();
var task =
ODataBatchRequestItem.SendRequestAsync(handler, context, 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(context);
}
return tcs.Task;
},
context.RequestAborted,
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 returnContexts = new List<HttpContext>();
foreach (var responseTask in responseTasks)
{
var returnContext = responseTask.Result.Result;
if (returnContext.Response.IsSuccessStatusCode())
{
returnContexts.Add(returnContext);
}
else
{
returnContexts.Clear();
returnContexts.Add(returnContext);
return new ChangeSetResponseItem(returnContexts);
}
}
return new ChangeSetResponseItem(returnContexts);
}
/// <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 void SetChangeSetProperty(RestierChangeSetProperty changeSetProperty)
{
foreach (var context in Contexts)
{
context.SetChangeSet(changeSetProperty);
}
}
}
}