System.ObjectDisposedException: Cannot access a disposed object. Object name: 'MemoryPool'. Asp.Net Core

Mani 1 Reputation point
2023-01-06T05:31:39.727+00:00
app.UseMiddleware(typeof(ErrorHandlingMiddleware));  
  
app.UseMiddleware<TokenHandler>();  

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'MemoryPool'. at System.Buffers.MemoryPoolThrowHelper.ThrowObjectDisposedException(ExceptionArgument argument) at System.Buffers.SlabMemoryPool.Lease() at System.IO.Pipelines.Pipe.AllocateSegment(Int32 sizeHint) at System.IO.Pipelines.Pipe.AllocateWriteHeadSynchronized(Int32 sizeHint) at Microsoft.AspNetCore.Server.IIS.Core.OutputProducer.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.<>c__DisplayClass335_0.<g__WriteFirstAsync|0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|29_0TFilter,TFilterAsync at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNextTFilter,TFilterAsync at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at DesktopEngine.Helper.TokenHandler.Invoke(HttpContext context) in C:\DesktopEngine\Helper\TokenHandler.cs:line 62 at DesktopEngine.MiddleWare.ErrorHandlingMiddleware.Invoke(HttpContext context) in C:\DesktopEngine\MiddleWare\ErrorHandlingMiddleware.cs:line 26|

How to fix this?

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

1 answer

Sort by: Most helpful
  1. Mani 1 Reputation point
    2023-01-08T12:42:09.293+00:00

    Kindly find the middleware code

    https://codeshare.io/pqRpyX.

    Please share your recommendation.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.