Skip to content

When ApiVersionOptions.ReportApiVersions is true and Response.Body stream in used in a controller an exception is thrown #273

Closed
@gpcastro

Description

@gpcastro

In a ASP.NET Core web API project, an exception is thrown when ReportApiVersions is true as the versioning framework tries to add a header to an HTTP response after the response started being streamed to the client.

Simple repro:

  1. In an ASP.NET core web API, have this in the Startup:
services.AddApiVersioning(o =>
{
    o.ReportApiVersions = true;
    o.AssumeDefaultVersionWhenUnspecified = true; // only needed to facilitate the test, not needed for repro if the api version is supplied.
});
  1. In a controller class, add a version, e.g.
[ApiVersion("1.0")]
[Route("api/[controller]")]
public class ValuesController : Controller
  1. Then in the method, write something to Reponse.Body, for example:
[HttpGet]
public async Task Get()
{
    Response.StatusCode = 200;
    using (var writer = new StreamWriter(Response.Body))
    {
        await writer.WriteAsync("Hello");
    }
}

When the controller method is fired, an exception is thrown as the versioning framework tries to add a header to the response after the HTTP streaming has started, and thus the header had already been sent.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions