Skip to content

Add CancellationToken overloads for reading multipart form sections #41532

@martincostello

Description

@martincostello

Background and Motivation

While checking for other usages of ReadAsStringAsync() when working on #41531, I found one other occurrence here:

public Task<string> GetValueAsync()
{
return Section.ReadAsStringAsync();
}

As well as adding an overload for a CancellationToken parameter there, another would be needed here:

/// <summary>
/// Reads the body of the section as a string
/// </summary>
/// <param name="section">The section to read from</param>
/// <returns>The body steam as string</returns>
public static async Task<string> ReadAsStringAsync(this MultipartSection section)

The new overload could then be used here:

var value = await formDataSection.GetValueAsync();

Proposed API

namespace Microsoft.AspNetCore.WebUtilities;

public class FormMultipartSection
{
+   public Task<string> GetValueAsync(CancellationToken cancellationToken);
}

public static class MultipartSectionStreamExtensions
{
+   public static async Task<string> ReadAsStringAsync(this MultipartSection section, CancellationToken cancellationToken);
}

Usage Examples

var formDataSection = new FormMultipartSection(section, contentDisposition);
var key = formDataSection.Name;
var value = await formDataSection.GetValueAsync(cancellationToken);

Alternative Designs

Add an optional parameter instead as a binary-breaking change for .NET 7.

namespace Microsoft.AspNetCore.WebUtilities;

public class FormMultipartSection
{
-   public Task<string> GetValueAsync();
+   public Task<string> GetValueAsync(CancellationToken cancellationToken = default);
}

public static class MultipartSectionStreamExtensions
{
-   public static async Task<string> ReadAsStringAsync(this MultipartSection section);
+   public static async Task<string> ReadAsStringAsync(this MultipartSection section, CancellationToken cancellationToken = default);
}

Risks

None known.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions