Cannot write to the response body, the response has completed


I have this weird error coming from ServiceStack.ServiceStackHost and not sure how to solve it.

When I run my .net 6 console api app and it displays the api surface in any web browser (tested it on edge and chrome) then all displays well. ( https://localhost/metadata) When I hit refresh in the browser (with very fast iteration of refresh button) then I eventually hit the following error.

ServiceStack.ServiceStackHost: Error: Cannot write to the response body, the response has completed.
Object name: ‘HttpResponseStream’.
System.ObjectDisposedException: Cannot write to the response body, the response has completed.
Object name: ‘HttpResponseStream’.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponsePipeWriter.g__ThrowObjectDisposedException|15_0()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(ReadOnlyMemory1 source, CancellationToken cancellationToken) at Microsoft.AspNetCore.Watch.BrowserRefresh.WebSocketScriptInjection.TryInjectLiveReloadScriptAsync(Stream baseStream, ReadOnlyMemory1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Watch.BrowserRefresh.ResponseStreamWrapper.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)

I tried to replicate this in a smaller repo but not having any success yet.

Any pointers on how to solve this would be much appreciated.

Are you running this during development? Because this looks like it’s caused by the dotnet watch process that’s inserted into the dotnet process to refresh the browser.

Hi @mythz , yes just running this in Debug mode. Not using dotnet watch only VS2022 (latest preview). Unfortunately can’t check it in VS2019 as I’m using .net 6.

Was converting my app to .NET 6 and the first issue was the “CSS hot reload” issue in VS2022. That is now sorted , but on this one still puzzling me.

It’s the dotnet file watch browser process that’s used to enable its hot reload support, it has issues:

Thanks @mythz, much appreciated. You are saving my sanity !! Will keep an eye on this as I don’t see this behavior in a minimalistic api console app in VS2022. I just wish MS can stabilize the toolchain and IDE because currently almost everything is linked to VS2022 Preview and it’s extremely tedious to pinpoint an issue.