use*_*767 5 c# asynchronous async-await asp.net-core-2.0
我有一个 .NET CORE 2 后端。在我的一个控制器端点中,我正在创建要通过电子邮件发送的邀请。这似乎是端点上的一个巨大瓶颈,考虑之后,我真的不需要等待这些邀请。如果电子邮件未能发送出去,我无论如何也无能为力。
如果我不这样做await sendFn(),它本质上会是一种即发即忘的方法吗?我正在阅读另一个 stackoverflow 线程,我必须这样做sendFn().ContinueWith(t => throw(t))才能捕获异常,因为它将在另一个线程中。
我在代码库周围有类似的邮件功能。他们每个人做的事情都略有不同,但是有没有我可以做的服务来包装这些东西,让它们着火而忘记?我认为有些地方我不能使用await(如果有效的话),但有些东西会改变数据库上下文,所以如果我不等待它们,我可能会遇到某些东西正在访问相同的数据库上下文的情况。
[HttpPost]
public async Task<IActionResult> CreateEvent([FromBody] Event val)
{
_ctx.Event.Add(val);
await _ctx.SaveChangesAsync();
await SendInvitations(val); // fn in question
return Ok();
}
public async Task SendInvitation(Event event)
{
forEach (var person in event.people)
{
await _ctx.Invitation.Add(person); // This shouldn't happen while another iteration or some other async code elsewhere is using the db ctx.
_ctx.SaveChangesAsync();
await _mailService.SendMail(person.email,"you have been invited"); // don't really need to await this.
}
}
Run Code Online (Sandbox Code Playgroud)
我正在将有关事件的数据发布到我的服务器。创建事件并将其保存到数据库后,我会为每个人创建邀请。这些邀请也是数据库项目。然后我发了一封电子邮件。我最担心的是,如果我放弃等待,那么当我创建邀请时,它可能会与其他地方或下一次迭代的 db 上下文发生冲突。
为了让您的代码编译和运行,我必须进行以下更改:
public async Task<IActionResult> CreateEvent(Event val)
{
_ctx.Event.Add(val);
await _ctx.SaveChangesAsync();
await SendInvitation(val);
return Ok();
}
public async Task SendInvitation(Event @event)
{
foreach (var person in @event.people)
{
await _ctx.Invitation.Add(person);
await _ctx.SaveChangesAsync();
await _mailService.SendMail(person.email, "you have been invited");
}
}
Run Code Online (Sandbox Code Playgroud)
我还必须编写这个线束代码:
public OK Ok() => new OK();
public class Event
{
public List<Person> people = new List<Person>();
}
public class Person
{
public string email;
}
public interface IActionResult { }
public class OK : IActionResult { }
public class Invitation
{
public Task Add(Person person) => Task.Run(() => { });
}
public static class _ctx
{
public static List<Event> Event = new List<Event>();
public static Invitation Invitation = new Invitation();
public static Task SaveChangesAsync() { return Task.Run(() => { }); }
}
public static class _mailService
{
public static Task SendMail(string email, string message) { return Task.Run(() => { }); }
}
Run Code Online (Sandbox Code Playgroud)
然后我更新SendInvitation如下:
public async Task SendInvitation(Event @event)
{
Thread.Sleep(2000);
foreach (var person in @event.people)
{
await _ctx.Invitation.Add(person);
await _ctx.SaveChangesAsync();
await _mailService.SendMail(person.email, "you have been invited");
}
Console.WriteLine("Done `SendInvitation`.");
}
Run Code Online (Sandbox Code Playgroud)
现在,我可以像这样运行它:
var e = new Event();
e.people.Add(new Person() { email = "foo@bar.com" });
CreateEvent(e).ContinueWith(t => Console.WriteLine("Done `CreateEvent`."));
Console.WriteLine("Done `Main`.");
Run Code Online (Sandbox Code Playgroud)
输出:
完成`主要`。
然后 2 秒后:
完成“发送邀请”。 完成`CreateEvent`。
如果我简单地更改CreateEvent为:
public async Task<IActionResult> CreateEvent(Event val)
{
_ctx.Event.Add(val);
await _ctx.SaveChangesAsync();
Task.Run(() => SendInvitation(val));
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
然后我得到这个输出:
完成`主要`。 完成`CreateEvent`。
然后 2 秒后:
完成“发送邀请”。
这似乎就是你想要的。
| 归档时间: |
|
| 查看次数: |
4414 次 |
| 最近记录: |