I’m in the process of developing a web app using an ASP.NET Core Web API (in a Docker container) and a separate Blazor web app. I’ve added individual user account functionality for authorization/authentication using Jwt Bearer tokens. Using the API’s auth endpoints in Swagger works as expected. However, when trying to call the API with POST
or PUT
requests in the Blazor web app, an error is thrown by both the API and the app. GET
requests are working correctly and return data.
The Login POST
response is generating an OK response from the API with the Json data correctly serialized. The exception triggers when running each middleware after the response.
A significant amount of code used for the auth controller was taken from the Visual Studio templates, so I would expect it to work as-is.
Here’s a snippet of code from the auth controller (truncated for brevity):
[HttpPost]
public async Task<Results<Ok<LoginResponseDTO>, ProblemHttpResult>>
Login([FromBody] LoginRequestDTO loginRequestDto)
{
...
var loginResult = await signInManager.PasswordSignInAsync(...);
...
var token = tokenRepository.GetAccessTokenResponse(user, principal, [.. roles]);
return TypedResults.Ok(token); // ERROR IS THROWN HERE, DURING MIDDLEWARE EXECUTION
}
And the web app’s API call:
public async Task<LoginResponseDTO> Login(LoginRequestDTO loginRequestDto)
{
var path = $"{apiOptions.Value.Version}{apiOptions.Value.LoginPath}";
return await HttpPost<LoginRequestDTO, LoginResponseDTO>(loginRequestDto, path);
}
private async Task<TResponse> HttpPost<TRequest, TResponse>(TRequest request, string path)
{
var httpClient = apiHttpClient.CreateClient(ApiOptions.ApiTitle);
var requestMessage = new HttpRequestMessage()
{
Method = HttpMethod.Post,
RequestUri = new Uri(httpClient.BaseAddress, path),
Content = JsonContent.Create(request)
};
var response = await httpClient.SendAsync(requestMessage); // ERROR IS THROWN HERE
return await response.Content.ReadFromJsonAsync<TResponse>();
}
The exception thrown on the API side:
The response has already started, the error page middleware will not be executed.
Connection id “0HN4M86HTJ4O4”, Request id “0HN4M86HTJ4O4:00000002”: An unhandled exception was thrown System.InvalidOperationException: StatusCode cannot be set because the response has already started.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ThrowResponseAlreadyStartedException(String value)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.set_StatusCode(Int32 value)
at Microsoft.AspNetCore.Http.HttpResults.Ok1.ExecuteAsync(HttpContext httpContext) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|7_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication
1 application)
And finally, the exception thrown by the Blazor web app:
System.Net.Http.HttpRequestException: Error while copying content to a stream.
System.Net.Http.HttpIOException: The response ended prematurely. (ResponseEnded)
at System.Net.Http.HttpConnection.FillAsync(Boolean async)
at System.Net.Http.HttpConnection.ChunkedEncodingReadStream.CopyToAsyncCore(Stream destination, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionResponseContent.g__Impl|6_0(Stream stream, CancellationToken cancellationToken)
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
— End of inner exception stack trace —
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at SocialMediaApp.Shared.Services.HttpApiService.HttpPost[TRequest,TResponse](TRequest request, String path) in H:VS ProjectsSocialMediaWebAppSocialMediaApp.WebServicesHttpApiService.cs:line 39
at SocialMediaApp.Shared.Services.HttpApiService.Login(LoginRequestDTO loginRequestDto) in H:VS ProjectsSocialMediaWebAppSocialMediaApp.WebServicesHttpApiService.cs:line 87
at SocialMediaApp.Web.Components.Account.Pages.Login.LoginUser() in H:VS ProjectsSocialMediaWebAppSocialMediaApp.WebComponentsAccountPagesLogin.razor:line 93
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.g__Execute|38_0()
at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<b__10_0>d.MoveNext()
— End of stack trace from previous location —
at Microsoft.AspNetCore.Antiforgery.Internal.AntiforgeryMiddleware.InvokeAwaited(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
I’ve tried disabling unnecessary middleware, using the ControllerBase
OK response instead of TypedResults
, and just about every other potential solution for this exception I can find. Since Swagger is able to read the response correctly, it seems the error might be caused by the web app somewhere, but I’m not sure where.
Methunas is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.