c# - Mongodb C# Driver 2.0 同步计数器

标签 c# asynchronous mongodb-csharp-2.0

我需要使用尚未切换到异步的旧 Controller 来计算与过滤器匹配的项目。我们有一个 async answer关于如何做到这一点:

long result = await collection.CountAsync(Builders<Hamster>.Filter.Eq(_ => _.Name, "bar"));

我还找到了一篇文章Introducing the 2.0 .NET Driver在 MongodDb 网站上有一条评论似乎证实了这是不可能的:

  1. It's async only: That's true. There has been a trend for new APIs to be async only (e.g. Microsoft's HttpClient). In general async programming is easy and results in higher server throughput without requiring a large number of threads. We are considering whether we should also support a sync API, and while we have gotten some requests for it (yours included) in general users seem eager to use async programming.

尽管如此,我想问一下是否有办法做到这一点/确认如果不使 Controller 方法异步(及其所有助手)是不可能的。

最佳答案

从驱动程序的 v2.2 开始,所有异步方法都有同步重载,因此您应该使用它们而不是阻塞在异步 API 上。这样做会降低性能并可能导致死锁:

long countOfItemsMatchingFilter = yourCollectionName.Count(yourFilterName);

关于c# - Mongodb C# Driver 2.0 同步计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35211595/

相关文章:

c# - Visual Studio - 编辑继续后出现错误 "Metadata file ' XYZ' 无法找到“

google-app-engine - 如果应用程序在 Google App Engine 上调用 10 个以上的异步 URL Fetch 会怎样?

sql-server-2008 - SQL Server 在线程中触发并运行 CLR 方法

c# - 将 [BsonExtraElements] 展平为键值对 json 输出

c# - BSON 文档到 FilterDefinition<Bson> MongoDb c# 驱动程序

c# - JSON 阅读器期待一个值但发现 'function'

c# - 创建 Controller 时为"Exception has been thrown by target of invocation"(带有MySql的ASP.NET MVC 5)

c# - 如何在 C# datagridview 中处理自定义类型的编辑?

c# - 如何停止传播异步流 (IAsyncEnumerable)

javascript - 等待函数结束(在 async.series 中)无法按预期工作