dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.27k stars 5.9k forks source link

Migrating from 2.2 to 5.0 IFeatureCollection has been disposed. #22901

Open vikassharma5981 opened 3 years ago

vikassharma5981 commented 3 years ago

We recently migrated from 2.2 to 5.0, everything works as expected but lately, we have started to face a problem with the APIs. We get a 200 OK with no response body. Referred the documents here for getting a resolution https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-5.0&tabs=visual-studio#break

StartUp.cs

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers().AddNewtonsoftJson(); ;
            services.AddCors();

            services.Configure<ApiBehaviorOptions>(a =>
            {
                a.InvalidModelStateResponseFactory = context =>
                {
                    var problemdetails = new CustomModelStateErrors().BindErrorResponse(context);
                    return new BadRequestObjectResult(problemdetails);
                };
            });
}

Configure method

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

            }
            app.UseSwagger();
            app.UseSwaggerUI(x =>
            {
                x.SwaggerEndpoint("/swagger/v2/swagger.json", "Sales Order Api");
            });
            app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
            app.UseAuthenticationMiddleware();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

Points to note, we are not using

  1. we are not using HttpContextAccessor
  2. We are using NewtonSoft to serialize the response

This is the controller:

[Route("items")]
        [CustomAuthorizeFilter()]
        [CustomExceptionFilter]
        [HttpPost]
        public IActionResult CreatePartDetailsForSaleOrder([FromBody] AddOrderLineItemRequestModel addOrderLine)
        {

                return CreateWebResponse(response);
            }

        }

This is the trace that we have: _System.ObjectDisposedException: Cannot access a disposed object. Object name: 'FileBufferingReadStream'. at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ThrowIfDisposed() at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.Read(Span1 buffer) at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at Microsoft.AspNetCore.WebUtilities.HttpRequestStreamReader.ReadIntoBuffer() at Microsoft.AspNetCore.WebUtilities.HttpRequestStreamReader.Read(Span1 buffer) at Microsoft.AspNetCore.WebUtilities.HttpRequestStreamReader.Read(Char[] buffer, Int32 index, Int32 count) at Newtonsoft.Json.JsonTextReader.ReadData(Boolean append, Int32 charsRequired) at Newtonsoft.Json.JsonTextReader.ReadChars(Int32 relativePosition, Boolean append) at Newtonsoft.Json.JsonTextReader.MatchValue(String value) at Newtonsoft.Json.JsonTextReader.MatchValueWithTrailingSeparator(String value) at Newtonsoft.Json.JsonTextReader.ParseFalse() at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonReader.Skip() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.HandleError(JsonReader reader, Boolean readPastError, Int32 initialDepth) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonInputFormatter.ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) at Microsoft.AspNetCore.Mvc.ModelBinding.Binders.BodyModelBinder.BindModelAsync(ModelBindingContext bindingContext) at Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinder.BindModelAsync(ActionContext actionContext, IModelBinder modelBinder, IValueProvider valueProvider, ParameterDescriptor parameter, ModelMetadata metadata, Object value, Object container) at Microsoft.AspNetCore.Mvc.Controllers.ControllerBinderDelegateProvider.<>cDisplayClass0_0.<gBind|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.gAwaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|250(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

Any assistance would be highly appreciated.

vikassharma5981 commented 3 years ago

Can I have some help here, I am struggling to fix this issue?

tdykstra commented 3 years ago

@Rick-Anderson Who could help with this / should we transfer it to the ASP.NET Core repo?

Rick-Anderson commented 3 years ago

Create the simplest possible project on a public GitHub repository that reproduces the issue. After that we can transfer the issue to https://github.com/dotnet/aspnetcore/issues