async-await

How do I handle async operations in Startup.Configure?

这一生的挚爱 提交于 2019-12-20 10:15:22
问题 In my ASP.NET 5 app, I want to load some data from Azure into a cache inside my Startup.Configure method. The Azure SDK exposes async methods exclusively. Typically, calling an async method is done via await inside an async method, like this: public async Task Configure(IApplicationBuilder app, IMemoryCache cache) { Data dataToCache = await DataSource.LoadDataAsync(); cache.Set("somekey", dataToCache); // remainder of Configure method omitted for clarity } However, ASP.NET 5 requires that the

Async library best practice: ConfigureAwait(false) vs. setting the synchronization context

不羁岁月 提交于 2019-12-20 09:56:22
问题 It's well-known that in a general-purpose library, ConfigureAwait(false) should be used on every await call to avoid continuing on the current SynchronizationContext. As an alternative to peppering the entire codebase with ConfigureAwait(false) , one could simply set the SynchronizationContext to null once, at the public surface method, and restore it before returning to the user. In other words: public async Task SomeSurfaceMethod() { var callerSyncCtx = SynchronizationContext.Current;

Calling async method to load data in constructor of viewmodel has a warning

落爺英雄遲暮 提交于 2019-12-20 09:56:13
问题 My view contains a ListView which display some data from internet, I create an async method to load data and call the method in my viewmodel's constructor. It has an warning prompt me now use await keyword. Any other solution to load data asynchronously in the constructor? 回答1: There are a couple of patterns which can be applied, all mentioned in the post by Stephan Cleary. However, let me propose something a bit different: Since you are in a WPF application, i would use the FrameworkElement

await keyword blocks main thread

两盒软妹~` 提交于 2019-12-20 09:17:24
问题 So I have the following code private async void button1_Click(object sender, EventArgs e) { await DoSomethingAsync(); MessageBox.Show("Test"); } private async Task DoSomethingAsync() { for (int i = 0; i < 1000000000; i++) { int a = 5; }; // simulate job MessageBox.Show("DoSomethingAsync is done"); await DoSomething2Async(); } private async Task DoSomething2Async() { for (int i = 0; i < 1000000000; i++) { int a = 5; } // simulate job MessageBox.Show("DoSomething2Async is done"); } Until both

Would a Task<T>.Convert<TResult> extension method be useful or does it have hidden dangers?

柔情痞子 提交于 2019-12-20 09:10:37
问题 I'm writing client libraries for Google Cloud APIs which have a fairly common pattern for async helper overloads: Do some brief synchronous work to set up a request Make an asynchronous request Transform the result in a simple way Currently we're using async methods for that, but: Transforming the result of await ends up being annoying in terms of precedence - we end up needing (await foo.Bar().ConfigureAwait(false)).TransformToBaz() and the brackets are annoying. Using two statements

Is it possible to call an awaitable method in a non async method? [duplicate]

社会主义新天地 提交于 2019-12-20 08:49:16
问题 This question already has answers here : How to call asynchronous method from synchronous method in C#? (14 answers) Closed 2 years ago . In a windows 8 application in C#/XAML, I sometimes want to call an awaitable method from a non asynchronous method. Actually is it correct to replace this : public async Task<string> MyCallingMethod() { string result = await myMethodAsync(); return result; } by this : public string MyCallingMethod() { Task.Run(async () => { string result = await

A code example illustrating the difference between the paradigms of async/await and Reactive (Rx) extension?

痞子三分冷 提交于 2019-12-20 08:39:02
问题 Both the System.Reactive extension for .NET and new C# 5.0 (.NET 4.5) async/await pursue (or based on) future and promises constructs paradigm (approach). Can you give the (*) simplest C# code example illustrating the difference between them? (*) Is it possible without I/O, internet or database connections? Update: Well, let me reformulate if this question seemed to be answered before. Why would one add and start using Reactive (Rx) extensions for .NET while using native .NET Iobservable

Node.js vs Async/await in .net

无人久伴 提交于 2019-12-20 08:25:49
问题 Can someone explain/ redirect me, what is the difference between Node.js's async model(non blocking thread) vs any other language for example c#'s asynchronous way of handling the I/O. This looks to me that both are same model. Kindly suggest. 回答1: Both models are very similar. There are two primary differences, one of which is going away soon (for some definition of "soon"). One difference is that Node.js is asynchronously single-threaded, while ASP.NET is asynchronously multi-threaded. This

Wrapping synchronous code into asynchronous call

只愿长相守 提交于 2019-12-20 07:59:16
问题 I have a method in ASP.NET application, that consumes quite a lot of time to complete. A call to this method might occur up to 3 times during one user request, depending on the cache state and parameters that user provides. Each call takes about 1-2 seconds to complete. The method itself is synchronous call to the service and there is no possibility to override the implementation. So the synchronous call to the service looks something like the following: public OutputModel Calculate

Error with await operator

时光毁灭记忆、已成空白 提交于 2019-12-20 07:56:58
问题 There is problem with my code. How can I solve this problem? This problem in await operator. public MyModel() { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync("https://api.vkontakte.ru/method/video.get?uid=219171498&access_token=d61b93dfded2a37dfcfa63779efdb149653292636cac442e53dae9ba6a049a75637143e318cc79e826149"); string googleSearchText = await response.Content.ReadAsStringAsync(); JObject googleSearch = JObject.Parse(googleSearchText); IList