-
Notifications
You must be signed in to change notification settings - Fork 201
New overloads for WriteStringAsync and WriteBytesAsync method which takes CancellationToken #1111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core; | ||
| using Azure.Core.Serialization; | ||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
@@ -43,13 +42,26 @@ public static void WriteString(this HttpResponseData response, string value, Enc | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the provided string to the response body using the specified encoding. | ||
| /// Asynchronously writes the provided string to the response body using the specified encoding. | ||
| /// </summary> | ||
| /// <param name="response">The response to write the string to.</param> | ||
| /// <param name="value">The string content to write to the request body.</param> | ||
| /// <param name="encoding">The encoding to use when writing the string. Defaults to UTF-8</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| public static Task WriteStringAsync(this HttpResponseData response, string value, Encoding? encoding = null) | ||
| { | ||
| return WriteStringAsync(response, value, default, encoding); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously writes the provided string to the response body using the specified encoding, and monitors cancellation requests. | ||
| /// </summary> | ||
| /// <param name="response">The response to write the string to.</param> | ||
| /// <param name="value">The string content to write to the request body.</param> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <param name="encoding">The encoding to use when writing the string. Defaults to UTF-8</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| public static Task WriteStringAsync(this HttpResponseData response, string value, CancellationToken cancellationToken, Encoding? encoding = null) | ||
|
||
| { | ||
| if (response is null) | ||
| { | ||
|
|
@@ -64,11 +76,11 @@ public static Task WriteStringAsync(this HttpResponseData response, string value | |
| encoding ??= Encoding.UTF8; | ||
|
|
||
| byte[] bytes = encoding.GetBytes(value); | ||
| return response.Body.WriteAsync(bytes, 0, bytes.Length); | ||
| return response.Body.WriteAsync(bytes, 0, bytes.Length, cancellationToken); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -82,7 +94,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// 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"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -98,7 +110,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -120,7 +132,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the default <see cref="ObjectSerializer"/> configured for this worker. | ||
| /// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -145,7 +157,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
|
|
||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// The response content-type will be set to <code>application/json; charset=utf-8</code> and the status code set to 200. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -160,7 +172,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// 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"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -177,7 +189,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to 200. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -195,7 +207,7 @@ public static ValueTask WriteAsJsonAsync<T>(this HttpResponseData response, T in | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Write the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// Asynchronously writes the specified value as JSON to the response body using the provided <see cref="ObjectSerializer"/>. | ||
| /// The response content-type will be set to the provided <paramref name="contentType"/> and the status code set to the provided <paramref name="statusCode"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to write.</typeparam> | ||
|
|
@@ -250,12 +262,24 @@ public static void WriteBytes(this HttpResponseData response, byte[] value) | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the provided bytes to the response body. | ||
| /// Asynchronously writes the provided bytes to the response body. | ||
| /// </summary> | ||
| /// <param name="response">The response to write the string to.</param> | ||
| /// <param name="value">The byte content to write to the request body.</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| public static Task WriteBytesAsync(this HttpResponseData response, byte[] value) | ||
| { | ||
| return WriteBytesAsync(response, value, default); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asynchronously writes the provided bytes to the response body, and monitors cancellation requests. | ||
| /// </summary> | ||
| /// <param name="response">The response to write the string to.</param> | ||
| /// <param name="value">The byte content to write to the request body.</param> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns> | ||
| public static Task WriteBytesAsync(this HttpResponseData response, byte[] value, CancellationToken cancellationToken) | ||
|
||
| { | ||
| if (response is null) | ||
| { | ||
|
|
@@ -267,7 +291,7 @@ public static Task WriteBytesAsync(this HttpResponseData response, byte[] value) | |
| throw new ArgumentNullException(nameof(value)); | ||
| } | ||
|
|
||
| return response.Body.WriteAsync(value, 0, value.Length); | ||
| return response.Body.WriteAsync(value, 0, value.Length, cancellationToken); | ||
| } | ||
|
|
||
| private static ObjectSerializer GetObjectSerializer(HttpResponseData response) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch!