There is already an open DataReader associated with this Connection which must be closed first
最近在写WebApi接口,遇到一些问题,总结一下。这些问题未必跟WebApi有关系,比较琐碎。
总结列表:
1. There is already an open DataReader associated with this Connection which must be closed first
2. npm install 失败
3. WebApi HTTP Put 405 Method not allowed
问题现象:一个接口被多次调用,可以认为是多线程调用接口,接口内部使用了C#的Async Await,代码形如:
return new GetXResponse
{
A= await A.GetAAsync(xx),
B= await B.GetBAsync(xx),
C= await C.GetCAsync(xx),
D= await D.GetDAsync(xx),
E= await E.GetEAsync(xx),
F= await F.GetFAsync(xx),
G= await G.GetGAsync(xx)
};
犯了一个错误,A、B、C、D等等都会连接数据库,连接数据库的代码,使用using不当
using (Connection)
{
return (await conn.QueryAsync<T>(sql, parms)).ToList();
}
Connection是在构造函数完成的初始化 new MySqlConnection,多线程调用时,using未及时回收数据库连接,修改如下之后,该异常可修复。
using (var conn = new MySqlConnection(ConnString))
{
return (await conn.QueryAsync<T>(sql, parms)).ToList();
}