question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Will calling HttpResponse.WriteAsync() cause HttpResponse.HasStarted to be set to true

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I am observing the following behavior when running my webapp in using dotnet core 3.1

var b = response.HasStarted // b is false
response.WriteAsync("hello").Wait();
b = response.HasStarted // b is true

where response is of type Microsoft.AspNetCore.Http.HttpResponse

Is this the intended behavior? Is this how .Net Framework worked?

According to https://learn.microsoft.com/en-us/aspnet/core/fundamentals/use-http-context?view=aspnetcore-7.0 “A response is started by flushing the response body or calling HttpResponse.StartAsync”

Yet if I step through the code it looks like I am doing neither.

Expected Behavior

response.WriteAsync does not cause response.HasStarted to be true

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

dotnet core 3.1

Anything else?

No response

Issue Analytics

  • State:open
  • Created 9 months ago
  • Reactions:1
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Tratchercommented, Dec 22, 2022
1reaction
davidfowlcommented, Dec 22, 2022
  1. Would be better (since it would just work in both cases). There’s also a 3, which is register the HttpResponse.OnStarting event and write headers in there. I don’t know what your code looks like. Response buffering is tricky because of the potential performance problems.
Read more comments on GitHub >

github_iconTop Results From Across the Web

what's HttpContext.Response.HasStarted for?
Server.Response.HasStarted : Indicates if the response status, reason, and headers are prepared to send and can no longer be modified. This is ...
Read more >
Use HttpContext in ASP.NET Core
StartAsync(CancellationToken). The HttpResponse. HasStarted property indicates whether the response has started.
Read more >
Content Injection with Response Rewriting in ASP.NET Core 3.x
In this post I discuss how to intercept Response output by using a customized stream, ... The reason for this suggestion is that...
Read more >
Handling errors in ASP.NET Core Web API - DevTrends
This post looks at the best ways to handle exceptions, validation and other invalid requests such as 404s in ASP.NET Core Web API...
Read more >
Middleware
If the response has not started ( HttpResponse.HasStarted ) it can be cleared ( HttpResponse.Clear() ) and an alternate response sent, or the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found