我试图查询表中的行数,然后将其返回到我的Controller Action。我使用Dapper作为我的Micro-ORM,并且似乎在尝试实现这一目标时遇到了一些问题。
public Task<int> GetPostCount()
{
using (var connection = new SqlConnection(configuration.GetConnectionString("DefaultConnection")))
{
return connection.QuerySingleAsync<int>(SqlQueries.Post.SelectPostCount);
}
}
[HttpGet("Posts/{page:int?}")]
public async Task<IActionResult> Posts(int page)
{
IEnumerable<PostHighlightViewModel> posts = await this.postService.GetAllPosts(postsPerPage, page < 1 ? 1 : page);
if (posts.Count() == 0)
{
// BOOM goes the dynamite - right here.
int numberOfPosts = await this.postService.GetPostCount();
int pageToFetch = numberOfPosts / postsPerPage;
posts = await this.postService.GetAllPosts(postsPerPage, pageToFetch);
}
return View("Index", posts);
}
我执行的Sql是
SELECT COUNT(*) FROM POSTS
当我执行查询时,我收到以下异常
申请已开始。按Ctrl + C关闭。 失败:Microsoft.AspNetCore.Server.Kestrel,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60 [13] 连接ID&#34; 0HKSQRDBQRA9E&#34;:应用程序抛出了未处理的异常。 System.InvalidOperationException:无效的操作。连接已关闭。 在System.Data.SqlClient.SqlCommand。&lt;&gt; c.b__110_0(任务
1 result) at System.Threading.Tasks.ContinuationResultTaskFromResultTask
2.InnerInvoke() 在System.Threading.Tasks.Task.Execute() ---从抛出异常的先前位置开始的堆栈跟踪结束--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在Dapper.SqlMapper.d__241.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() 在BlogWeb.Controllers.PostController.d__4.MoveNext()在C:\ Users \ johnathonsullinger \ Source \ VSTS \ Blog.Core \ Source \ BlogWeb \ Controllers \ PostController.cs:第32行 ---从抛出异常的先前位置开始的堆栈跟踪结束--- 在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在Microsoft.AspNetCore.Mvc.Internal.ObjectMethodExecutor.d__241.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionAsync>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeActionFilterAsync>d__41.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeAsync>d__32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.<InvokeActionAsync>d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Server.Kestrel.Http.Frame
1.d__2.MoveNext()
为了试图找出发生的事情,我离开这里并不多。我可以毫无问题地进行其他查询,但尝试查询记录计数似乎是一个问题。
我已尝试使用QueryFirstOrDefaultAsync
和QuerySingleAsync
。我应该不使用QueryAsync
系列方法吗?
答案 0 :(得分:4)
当执行从using
块返回时,将处理连接,因此在async
操作可以结束之前关闭连接。
您可以考虑在ContinueWith
中关闭连接,以确保在async
操作完成后关闭。
或者,我认为您可以在异步调用中使用await
关键字。