0

I've just successfully retargeted my Web API app from .NET Framework 4.7.1 to .NET Core 2.1. The application runs perfectly on my local machine.

After deploying it to an Azure app service the app starts up and looks healthy. However I log the following error whenever I make any API call (including to receive static files).

Microsoft.AspNetCore.Server.Kestrel: Connection id "0HLIFNSQMBR9Q", Request id "0HLIFNSQMBR9Q:0000000D": An unhandled exception was thrown by the application. System.ObjectDisposedException: Cannot access a disposed object. Object name: 'FrameResponseStream'. at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.FrameResponseStream.ValidateState(CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.FrameResponseStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.FrameResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder) at System.IO.StreamWriter.Write(String value) at Microsoft.AspNetCore.Hosting.Views.ErrorPage.ExecuteAsync() at Microsoft.Extensions.RazorViews.BaseView.ExecuteAsync(HttpContext context) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Frame`1.ProcessRequestsAsync()

Unfortunately the above stack trace does not mention any of our code, and so I'm not entirely sure where this exception is coming from. This error did not occur when .NET Framework was targeted, so it must be related to the retargeting to .NET Core 2.1.

I have checked this StackOverflow question which also describes the exception after migrating to .NET Core 2.1, but I've tried looking for and fixing the situations described in the answers without success.

What could be causing this error and how can I fix it?

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
  • 1
    Did you follow this? https://learn.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-2.1 – Marie Nov 21 '18 at 18:09
  • Exception kinda gives you a hint which class is causing the issue: `: Cannot access a disposed object. Object name: 'FrameResponseStream'`. Quick Google search for `FrameResponseStream` ends up in https://github.com/aspnet/KestrelHttpServer/issues/2299 and a [referenced issue](https://github.com/aspnet/Docs/issues/5466) which is about improving https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-2.1 docs page. Does that give you enough direction? – Tseng Nov 21 '18 at 18:16
  • Thanks @Marie I'll check it out. The article talks about upgrading from .NET Core 2.0 to 2.1 - we are going from targeting .NET Framework. Perhaps there is done useful information there though! – 08Dc91wk Nov 21 '18 at 18:19
  • Hi @Tseng I have looked at all those links but I'm not sure that the suggested improvements to the docs give me any more direction - but I'll read it thoroughly and see if I can figure it out from there, thanks – 08Dc91wk Nov 21 '18 at 18:33
  • You are not using Websockets anywhere, directly or indirectly (SignalR) or putting anything form a request into the background thread / service and not awaiting the middleware till that one is finished? – Tseng Nov 21 '18 at 18:35
  • And in general it says, it happens, when you add something that requires a connection to a background service, then end the connection (this causes the services instantiated during the request to become disposed), without waiting for the background task to finish. The services tries to write a response, but it cant because the connection is already finished and disposed, hence the suggestion to change the error message to `System.ObjectDisposedException: Cannot write to the response because the response has ended. Object name: 'FrameResponseStream'.` – Tseng Nov 21 '18 at 18:43

0 Answers0