2

The error is occuring in the getStores() function:

An unhandled exception occurred while processing the request. ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'

  public class StoreController : Controller

{
    // dependency-injected
    private readonly MySqlConnection _connection;

    public StoreController(MySqlConnection mySqlConnection)
    {
        _connection = mySqlConnection;
    }
 // GET
    public IActionResult Index()
    {
        return Content("home");
    }

    [HttpPost]
    [Route("/stores")]
    public async Task getStores()
    {
        // open connection
        await _connection.OpenAsync();
        // execute query and get stores
        using var command = new MySqlCommand("SELECT * FROM Stores", _connection);
        using var reader = await command.ExecuteReaderAsync();
        while (await reader.ReadAsync())
        {
            var value = reader.GetValue(0);
            Console.WriteLine(value);
        }
    }
}
18
  • 3
    What is the lifetime strategy you are using for injecting the connection? Commented May 7, 2020 at 11:55
  • Checkout this link and see if it helps. Post says the connection might have been closed due to inactivity or other reason. Commented May 7, 2020 at 11:56
  • Can you please add MySqlConnection code as well. Commented May 7, 2020 at 12:20
  • @Crowcoder Transient Commented May 7, 2020 at 12:48
  • Specifically what line of code blows up? Commented May 7, 2020 at 12:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.