我有这个构造main(),它创造了
var tasks = new List<Task>();
var t = Task.Factory.StartNew(
async () =>
{
Foo.Fim();
await Foo.DoBar();
});
//DoBar not completed
t.Wait();
//Foo.Fim() done, Foo.DoBar should be but isn't
Run Code Online (Sandbox Code Playgroud)
但是,当我.Wait为t时,它不会等待呼叫DoBar()完成.我怎么让它实际等待?
由于我已将我的WCF方法转换为Async,因此我的单元测试失败了,我无法找出正确的语法来使它们工作.
Cllient代理类
public interface IClientProxy
{
Task DoSomething(CredentialDataList credentialData, string store);
}
Run Code Online (Sandbox Code Playgroud)
服务类
public class CredentialSync : ICredentialSync
{
private ICredentialRepository _repository;
private IClientProxy _client;
public CredentialSync()
{
this._repository = new CredentialRepository();
this._client = new ClientProxy();
}
public CredentialSync(ICredentialRepository repository, IClientProxy client)
{
this._repository = repository;
this._client = client;
}
public async Task Synchronise(string payrollNumber)
{
try
{
if (string.IsNullOrEmpty(payrollNumber))
{
.... some code
}
else
{
CredentialDataList credentialData = new CredentialDataList();
List<CredentialData> credentialList = new List<CredentialData>();
// fetch the …Run Code Online (Sandbox Code Playgroud) 以下代码将允许我更新FirstName ="john"和LastName ="Doe"的电子邮件.如何在不使用Save()方法的情况下更新电子邮件和电话?
MongoDB.Driver.MongoServer _server = MongoDB.Driver.MongoServer.Create("mongodb://localhost");
MongoDB.Driver.MongoDatabase _dataBase = _server.GetDatabase("test");
MongoDB.Driver.MongoCollection<Person> _person = _dataBase.GetCollection<Person>("person");
//Creat new person and insert it into collection
ObjectId newId = ObjectId.GenerateNewId();
Person newPerson = new Person();
newPerson.Id = newId.ToString();
newPerson.FirstName = "John";
newPerson.LastName = "Doe";
newPerson.Email = "john.doe@gmail.com";
newPerson.Phone = "8005551222";
_person.Insert(newPerson);
//Update phone and email for all record with firstname john and lastname doe
MongoDB.Driver.Builders.QueryComplete myQuery = MongoDB.Driver.Builders.Query.And(MongoDB.Driver.Builders.Query.EQ("FirstName", "John"), MongoDB.Driver.Builders.Query.EQ("LastName", "Doe"));
MongoDB.Driver.Builders.UpdateBuilder update = MongoDB.Driver.Builders.Update.Set("Email", "jdoe@gmail.com");
_person.Update(myQuery, update);
Run Code Online (Sandbox Code Playgroud) 异步控制器有不同的示例.其中一些在方法定义中使用CancellationToken:
public async Task<ActionResult> ShowItem(int id, CancellationToken cancellationToken)
{
await Database.GetItem(id, cancellationToken);
...
Run Code Online (Sandbox Code Playgroud)
但是其他示例甚至是VS2013的默认ASP.NET项目根本不使用CancellationToken而没有使用它:
public async Task<ActionResult> ShowItem(int id)
{
await Database.GetItem(id);
...
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚,我们是否应该在控制器中使用CancellationToken(以及为什么).
c# asp.net-mvc async-await asp.net-web-api cancellation-token
我使用MongoDB C#驱动程序以编程方式创建了一个新的文档集合.
此时我想以编程方式创建和构建索引.我怎样才能做到这一点?
我们使用IEnumerables从数据库中返回大量数据集:
public IEnumerable<Data> Read(...)
{
using(var connection = new SqlConnection(...))
{
// ...
while(reader.Read())
{
// ...
yield return item;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们想使用异步方法来做同样的事情.但是,async没有IEnumerables,因此我们必须将数据收集到列表中,直到加载整个数据集:
public async Task<List<Data>> ReadAsync(...)
{
var result = new List<Data>();
using(var connection = new SqlConnection(...))
{
// ...
while(await reader.ReadAsync().ConfigureAwait(false))
{
// ...
result.Add(item);
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
这将消耗服务器上的大量资源,因为所有数据必须在返回之前在列表中.IEnumerables处理大数据流的最佳且易于使用的异步替代方法是什么?我想避免在处理时将所有数据存储在内存中.
更新这个问题的目的是得到一个简单的答案Task.Run()和死锁.我非常理解不混合异步和同步的理论推理,我将它们铭记于心.我不是在向别人学习新事物; 我尽力做到这一点.有时候所有人都需要技术答案......
我有一个Dispose()需要调用异步方法的方法.由于95%的代码都是异步的,因此重构不是最佳选择.拥有IAsyncDisposable框架支持的(以及其他功能)将是理想的,但我们还没有.所以在同一时间,我需要找到一种可靠的方法从同步方法调用异步方法而不会发生死锁.
我宁愿不使用,ConfigureAwait(false)因为这使得责任分散在我的整个代码中,以便被调用者以某种方式行事,以防调用者是同步的.我宁愿在同步方法中做一些事情,因为它是一个不正常的bugger.
在阅读了Stephen Cleary关于另一个Task.Run()总是在线程池中调度甚至异步方法的问题的评论后,它让我思考.
在ASP.NET中的.NET 4.5或任何其他同步上下文中,将任务调度到当前线程/同一线程,如果我有一个异步方法:
private async Task MyAsyncMethod()
{
...
}
Run Code Online (Sandbox Code Playgroud)
我想从同步方法中调用它,我可以使用Task.Run()它Wait()来避免死锁,因为它将异步方法排队到线程池吗?
private void MySynchronousMethodLikeDisposeForExample()
{
// MyAsyncMethod will get queued to the thread pool
// so it shouldn't deadlock with the Wait() ??
Task.Run((Func<Task>)MyAsyncMethod).Wait();
}
Run Code Online (Sandbox Code Playgroud) 在mongodb的官方文档中,他们提到了upserts,所以编写一个upsert命令而不是:
if (_campaignRepo.Exists(camp))
{
_campaignRepo.DeleteByIdAndSystemId(camp);
}
_campaignRepo.Save(camp);
Run Code Online (Sandbox Code Playgroud)
如果可能的话,可以在db级别实现该逻辑的东西.那么如果有一个upsert的方法是什么?
我最近发现MongoDB中的所有查询都区分大小写.如何进行不区分大小写的搜索?
我找到了一种方法:
Query.Matches(
"FirstName",
BsonRegularExpression.Create(new Regex(searchKey,RegexOptions.IgnoreCase)));
Run Code Online (Sandbox Code Playgroud) 我使用控制台应用程序作为概念证明和获得异步返回值的新需求.
我发现我需要Task.WaitAll()在我的main方法中使用以避免需要异步"main()"方法,这是非法的.
我现在卡住试图弄清楚允许我使用泛型的重载,或者只返回我可以投射的对象,但是在Main()中.
.net c# console-application task-parallel-library async-await
c# ×10
.net ×8
async-await ×6
mongodb ×4
asynchronous ×2
asp.net-mvc ×1
indexing ×1
moq ×1
task ×1