async-await

Does async/await have an extra cost associated when returning early from the method?

旧街凉风 提交于 2021-01-27 19:42:05
问题 Does async/await have an extra cost associated when returning early from the method compared to the synchronous method? Take these examples: public async Task<bool> TryDoSomethingAsync() { if ( Is99PercentOfTheCases() ) return false; // does this path... await DoSomethingAsync(); return true; } // versus public bool TryDoSomething() { if ( Is99PercentOfTheCases() ) return false; // ...have the same cost as this path? DoSomething(); return true; } I know async/await has an extra cost

combining results from several async/await calls

你。 提交于 2021-01-27 18:33:04
问题 I want to return the consolidated results from calling many async/await functions. Consider my (wrong) pseudocode below const getRemoteData = async function(uri) { // get result from remote server via XMLHttpRequest return result; } const finalResult {}; const result1 = getRemoteData('http://…'); result1.forEach(async record => { // each record has a key for another URI const result2 = await getRemoteData(record["uri"]); // result2 has a key for yet another URI const result3 = await

.Net Core Async critical section if working on same entity

你说的曾经没有我的故事 提交于 2021-01-27 17:51:29
问题 I need to be sure that a method accessed via a web API cannot be accessed by multiple call at the same time if it work on the same object with the same id I understand the use of SemaphoreSlim but a simple implemetation of that will lock the critical section for all. But I need that section locked only if it works on the same entity and not on 2 different This is my scenario, an user start to work, the entity is created and is ready to be modified, then one or more user can manipulate this

C# Entity Framework error on async methods

↘锁芯ラ 提交于 2021-01-27 14:35:11
问题 I have already seen this, but I am experiencing another problem. I have this service class for managing ASP.NET identity roles: public class RoleService : IRoleService { private readonly RoleManager<ApplicationRole> _roleManager; public RoleService(RoleManager<ApplicationRole> roleManager) { this._roleManager = roleManager; } public async Task<IdentityResult> CreateAsync(ApplicationRole role) { return await this._roleManager.CreateAsync(role); } } As suggested by this question, I use the

C# Entity Framework error on async methods

北慕城南 提交于 2021-01-27 13:17:02
问题 I have already seen this, but I am experiencing another problem. I have this service class for managing ASP.NET identity roles: public class RoleService : IRoleService { private readonly RoleManager<ApplicationRole> _roleManager; public RoleService(RoleManager<ApplicationRole> roleManager) { this._roleManager = roleManager; } public async Task<IdentityResult> CreateAsync(ApplicationRole role) { return await this._roleManager.CreateAsync(role); } } As suggested by this question, I use the

Error on Future generator closure: Captured variable cannot escape `FnMut` closure body

会有一股神秘感。 提交于 2021-01-27 12:03:28
问题 I want to create a simple websocket server. I want to process the incoming messages and send a response, but I get an error: error: captured variable cannot escape `FnMut` closure body --> src\main.rs:32:27 | 32 | incoming.for_each(|m| async { | _________________________-_^ | | | | | inferred to be a `FnMut` closure 33 | | match m { 34 | | // Error here... 35 | | Ok(message) => do_something(message, db, &mut outgoing).await, 36 | | Err(e) => panic!(e) 37 | | } 38 | | }).await; | |_____^

Choosing not to await async function in action in ASP.NET Core WebAPI controller

☆樱花仙子☆ 提交于 2021-01-27 08:01:54
问题 The scenario is the following: Backend: Asp.NET Core WebAPI 2.2 Frontend: iOS and Android which consumes the API I have a function allowing the user to send messages to other users. The sending of a message is done in an asynchronous action: public async Task<IActionResult> CreateMessage This action does the following in order: Validation Awaits persistance of the message to DB Awaits the notification of relevant clients via SignalR Doesn't await the sending of push notification via Azure

Choosing not to await async function in action in ASP.NET Core WebAPI controller

假装没事ソ 提交于 2021-01-27 08:01:44
问题 The scenario is the following: Backend: Asp.NET Core WebAPI 2.2 Frontend: iOS and Android which consumes the API I have a function allowing the user to send messages to other users. The sending of a message is done in an asynchronous action: public async Task<IActionResult> CreateMessage This action does the following in order: Validation Awaits persistance of the message to DB Awaits the notification of relevant clients via SignalR Doesn't await the sending of push notification via Azure

Choosing not to await async function in action in ASP.NET Core WebAPI controller

徘徊边缘 提交于 2021-01-27 08:01:03
问题 The scenario is the following: Backend: Asp.NET Core WebAPI 2.2 Frontend: iOS and Android which consumes the API I have a function allowing the user to send messages to other users. The sending of a message is done in an asynchronous action: public async Task<IActionResult> CreateMessage This action does the following in order: Validation Awaits persistance of the message to DB Awaits the notification of relevant clients via SignalR Doesn't await the sending of push notification via Azure

Add delay to parallel API call

↘锁芯ラ 提交于 2021-01-27 07:35:04
问题 I'm using Polly to make parallel API calls. The server however can't process more than 25 calls per second and so I'm wondering if there is a way to add a 1s delay after each batch of 25 calls? var policy = Policy .Handle<HttpRequestException>() .RetryAsync(3); foreach (var mediaItem in uploadedMedia) { var mediaRequest = new HttpRequestMessage { *** } async Task<string> func() { var response = await client.SendAsync(mediaRequest); return await response.Content.ReadAsStringAsync(); } tasks