System.ObjectDisposedException: 'Cannot access a disposed object. ObjectDisposed_ObjectName_Name', al momento de guardar mi lista de imagenes

Andrea Evelyn Mejia Rubio 0 Reputation points
2024-04-09T19:43:43.2266667+00:00

My View in vue.js sends me the image arrangement to my api, which I want to save in a folder asynchronously and when I do the second round in my foreach to the second image it gives me the error.

foreach (var imagen in imagenes)
{
    ImagenesProducto imagenesProducto = new ImagenesProducto();

 using (var stream = new FileStream(concFile, FileMode.Create))
 {
     await imagen.CopyToAsync(stream);
 }
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,275 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
298 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 48,576 Reputation points
    2024-04-10T16:42:11.8233333+00:00

    I think we're missing some information here to be helpful. It looks like you have a list of streams(?) in imagenes. You enumerate through and for each one you are creating an instance of ImagenesProducto but that doesn't seem to be used elsewhere so we'll ignore it. You then create a new file using a stream and copy the current item (stream?) into it. By itself this code is fine so I think something else is going on here.

    Look at the callstack and tell us what specifically is getting disposed too early. My gut instinct is that it is your imagen variable which means whatever is passing you that set of items might be bad.

    Step through your code and when you get to the CopyToAsync line look at both objects in the debugger and see which one is marked as disposed.


  2. Bruce (SqlWork.com) 56,686 Reputation points
    2024-04-21T15:24:05.29+00:00

    I assume imagenes is posted by the vue app. Your api code is probably returning a response to the vue.js code before the sample code completes. The api must await the copy before returning. If you don’t want wait for the file copy, the postback image streams must be copied to new buffers before calling the code.

    0 comments No comments