我的.NET核心代码出了什么问题,在进行数据库请求的时候出现了错误

问题描述 投票:2回答:1

错误发生在 getStores() 函数时发生了一个未处理的异常。

处理请求时发生了一个未处理的异常。 ObjectDisposedException。无法访问一个已处理的对象。 对象名称:'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);
        }
    }
}
c# asp.net .net asp.net-core mysql-connector
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.