async-await

Calling an async method using a Task.Run seems wrong?

别来无恙 提交于 2019-12-17 19:35:03
问题 I recently came across this code written by a contractor we had working for us. It's either devilishly clever or silly (I think the latter but I wanted a second opinion). I'm not massively up to speed on async await . Basically it worked like this: public bool Send(TemplatedMessageDto message) { return Task.Run(() => SendAsync(message)) .GetAwaiter() .GetResult(); } public async Task<bool> SendAsync(TemplatedMessageDto message) { //code doing stuff var results = await _externalresource

MVC4 .NET 4.5 async action method does not redirect

送分小仙女□ 提交于 2019-12-17 19:25:58
问题 I am trying to implement a task action method in my MVC 4 application. Everything works on the back in, but it is not redirecting. public class AccountController : AsyncController { [HttpPost] [AllowAnonymous] public async Task<ActionResult> Login(LoginModel model, string returnUrl) { var client = new ClientHelper("login"); account = await client.CallActionType<LoginModel, Account>(EnumHelpers.HttpType.Post, model); if (account != null) { validLogin = true; } return Redirect(returnUrl); //

Understanding async/await on NodeJS

∥☆過路亽.° 提交于 2019-12-17 19:22:59
问题 I think my understanding of it might be affected by my experience with .NET 's async/await , so I'd like some code example: I'm trying to make a express controller wait 5 seconds before returning the response: const getUsers = async (ms) => { var wait = ms => new Promise(resolve => setTimeout(resolve, ms)); await wait(ms); }; export const index = (req, res) => { async () => { await getUsers(5000); res.json([ { id: 1, name: 'John Doe', }, { id: 2, name: 'Jane Doe', }, ]); }; }; This code doesn

A websocket's ReceiveAsync method does not await the entire message

梦想与她 提交于 2019-12-17 19:16:10
问题 I am receiving JSON through a websocket. At least: I am partially. Using an online websocket service I receive the full JSON response (all the HTML markup is ignored). When I look at the JSON that I receive in my console I can see the HTML markup (viewing it with the HTML viewer during debugging removes the HTML) but it ends abruptly (incomplete data). My buffer has plenty of space and I am using async-await to (supposedly) wait for the entire response to come in before continuing. private

SmtpClient.SendMailAsync causes deadlock when throwing a specific exception

China☆狼群 提交于 2019-12-17 19:12:23
问题 I'm trying to setup email confirmation for an ASP.NET MVC5 website, based on the example AccountController from the VS2013 project template. I've implemented the IIdentityMessageService using SmtpClient , trying to keep it as simple as possible: public class EmailService : IIdentityMessageService { public async Task SendAsync(IdentityMessage message) { using(var client = new SmtpClient()) { var mailMessage = new MailMessage("some.guy@company.com", message.Destination, message.Subject, message

HttpClient Singleton Implementation in ASP.NET MVC

痞子三分冷 提交于 2019-12-17 19:02:11
问题 After reading this blog post and thisofficial note on www.asp.net: HttpClient is intended to be instantiated once and re-used throughout the life of an application. Especially in server applications, creating a new HttpClient instance for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. I discovered that our code was disposing the HttpClient on each call. I'm updating our code so that we reuse the HttClient, but I'm

Preserve HttpContext when going async with WebAPI (Medium Trust)

末鹿安然 提交于 2019-12-17 18:59:52
问题 I am building a set of ASP.Net hosted WebAPI services that must use an old library which depends heavily on HttpContext.Current. I am having trouble ensuring that context is preserved in all the methods that participate in an async call. I have tried several variations with await/Task.Wait and TaskScheduler.FromCurrentSynchronizationContext() on the below code. [HttpGet] public Task<IEnumerable<string>> ContinueWith() { Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR"); //or

Difference of using async / await vs promises?

泪湿孤枕 提交于 2019-12-17 18:56:51
问题 I am looking for a answer on what to use in my nodeJS app. I have code which handles my generic dB access to mssql. This code is written using an async functions and then I used a promise to call that function and all works fine. As my app is getting bigger and code larger I am planning to move some of the logic into functions and then call them. So my question is: is there a drawback to using a mix of async/await and promises or does it really not matter? Async / await makes it easier to

Timeout pattern on task-based asynchronous method in C#

▼魔方 西西 提交于 2019-12-17 18:36:15
问题 As far as I know, there're two possible patterns to implement a timeout to task-based asynchronous methods: Built-in timeout public Task DoStuffAsync(TimeSpan timeout) This approach is harder to implement because it's not easy to implement a global timeout for the entire call stack. For example, a Web API controller receives an HTTP request and it calls DoStuffAsync , and the caller wants a global timeout of 3 seconds. That is, each inner async method call will need to receive the subtract of

async/await native implementations

人盡茶涼 提交于 2019-12-17 18:35:54
问题 This proposal suggests that async functions can use generator functions under the hood, although I cannot find a confirmation of this in ES2017 spec. Moreover, when generator prototype becomes messed up in Chrome/Node.js, async functions don't seem to be affected, this suggests that GeneratorFunction isn't used by AsyncFunction , at least directly: Object.getPrototypeOf((function * () {}).prototype).next = null; (async () => { return await Promise.resolve(1); })() .then(console.log); How