错误发生在 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);
}
}
}