Skip to content

Commit 853fc0a

Browse files
authored
New overloads for WriteStringAsync and WriteBytesAsync method which takes CancellationToken (#1111)
* New overloads for WriteStringAsync and WriteBytesAsync method which takes CancellationToken * write -> writes * Updated release notes
1 parent 14cc888 commit 853fc0a

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

release_notes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<!-- Please add your release notes in the following format:
33
- My change description (#PR/#issue)
44
-->
5-
- Adding support for opting-in to get empty entries in function trigger payload (#1091)
5+
- Adding support for opting-in to get empty entries in function trigger payload (#1091)
6+
- Exposed new overloads for `WriteStringAsync` and `WriteBytesAsync` methods (#1111)

src/DotNetWorker.Core/Http/HttpResponseDataExtensions.cs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Text;
77
using System.Threading;
88
using System.Threading.Tasks;
9-
using Azure.Core;
109
using Azure.Core.Serialization;
1110
using Microsoft.Extensions.Options;
1211
using Microsoft.Extensions.DependencyInjection;
@@ -43,13 +42,26 @@ public static void WriteString(this HttpResponseData response, string value, Enc
4342
}
4443

4544
/// <summary>
46-
/// Writes the provided string to the response body using the specified encoding.
45+
/// Asynchronously writes the provided string to the response body using the specified encoding.
4746
/// </summary>
4847
/// <param name="response">The response to write the string to.</param>
4948
/// <param name="value">The string content to write to the request body.</param>
5049
/// <param name="encoding">The encoding to use when writing the string. Defaults to UTF-8</param>
5150
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
5251
public static Task WriteStringAsync(this HttpResponseData response, string value, Encoding? encoding = null)
52+
{
53+
return WriteStringAsync(response, value, default, encoding);
54+
}
55+
56+
/// <summary>
57+
/// Asynchronously writes the provided string to the response body using the specified encoding, and monitors cancellation requests.
58+
/// </summary>
59+
/// <param name="response">The response to write the string to.</param>
60+
/// <param name="value">The string content to write to the request body.</param>
61+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
62+
/// <param name="encoding">The encoding to use when writing the string. Defaults to UTF-8</param>
63+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
64+
public static Task WriteStringAsync(this HttpResponseData response, string value, CancellationToken cancellationToken, Encoding? encoding = null)
5365
{
5466
if (response is null)
5567
{
@@ -64,11 +76,11 @@ public static Task WriteStringAsync(this HttpResponseData response, string value
6476
encoding ??= Encoding.UTF8;
6577

6678
byte[] bytes = encoding.GetBytes(value);
67-
return response.Body.WriteAsync(bytes, 0, bytes.Length);
79+
return response.Body.WriteAsync(bytes, 0, bytes.Length, cancellationToken);
6880
}
6981

7082
/// <summary>
71-
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
83+
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
7284
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200.
7385
/// </summary>
7486
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -82,7 +94,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
8294
}
8395

8496
/// <summary>
85-
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
97+
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
8698
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to the provided <paramref name="statusCode"/>.
8799
/// </summary>
88100
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -98,7 +110,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
98110
}
99111

100112
/// <summary>
101-
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
113+
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
102114
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200.
103115
/// </summary>
104116
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -120,7 +132,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
120132
}
121133

122134
/// <summary>
123-
/// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
135+
/// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker.
124136
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>.
125137
/// </summary>
126138
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -145,7 +157,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
145157

146158

147159
/// <summary>
148-
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
160+
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
149161
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200.
150162
/// </summary>
151163
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -160,7 +172,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
160172
}
161173

162174
/// <summary>
163-
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
175+
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
164176
/// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to the provided <paramref name="statusCode"/>.
165177
/// </summary>
166178
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -177,7 +189,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
177189
}
178190

179191
/// <summary>
180-
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
192+
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
181193
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200.
182194
/// </summary>
183195
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -195,7 +207,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in
195207
}
196208

197209
/// <summary>
198-
/// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
210+
/// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>.
199211
/// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>.
200212
/// </summary>
201213
/// <typeparam name="T">The type of object to write.</typeparam>
@@ -250,12 +262,24 @@ public static void WriteBytes(this HttpResponseData response, byte[] value)
250262
}
251263

252264
/// <summary>
253-
/// Writes the provided bytes to the response body.
265+
/// Asynchronously writes the provided bytes to the response body.
254266
/// </summary>
255267
/// <param name="response">The response to write the string to.</param>
256268
/// <param name="value">The byte content to write to the request body.</param>
257269
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
258270
public static Task WriteBytesAsync(this HttpResponseData response, byte[] value)
271+
{
272+
return WriteBytesAsync(response, value, default);
273+
}
274+
275+
/// <summary>
276+
/// Asynchronously writes the provided bytes to the response body, and monitors cancellation requests.
277+
/// </summary>
278+
/// <param name="response">The response to write the string to.</param>
279+
/// <param name="value">The byte content to write to the request body.</param>
280+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
281+
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
282+
public static Task WriteBytesAsync(this HttpResponseData response, byte[] value, CancellationToken cancellationToken)
259283
{
260284
if (response is null)
261285
{
@@ -267,7 +291,7 @@ public static Task WriteBytesAsync(this HttpResponseData response, byte[] value)
267291
throw new ArgumentNullException(nameof(value));
268292
}
269293

270-
return response.Body.WriteAsync(value, 0, value.Length);
294+
return response.Body.WriteAsync(value, 0, value.Length, cancellationToken);
271295
}
272296

273297
private static ObjectSerializer GetObjectSerializer(HttpResponseData response)

0 commit comments

Comments
 (0)