async-await

HttpResponseMessage' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter'

你。 提交于 2020-12-27 07:14:01
问题 I have this xUnit method in C# which test a web api [Fact] public async Task GetWeatherForecast() { var apiClient = new HttpClient(); var apiResponse = await apiClient.GetAsync($"http://xxx/weatherforecast").Result; Assert.True(apiResponse.IsSuccessStatusCode); } But hit this error HttpResponseMessage' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' . If I removed async Task and await , it could run successfully. 回答1: Don't call Result , it's

HttpResponseMessage' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter'

主宰稳场 提交于 2020-12-27 07:14:00
问题 I have this xUnit method in C# which test a web api [Fact] public async Task GetWeatherForecast() { var apiClient = new HttpClient(); var apiResponse = await apiClient.GetAsync($"http://xxx/weatherforecast").Result; Assert.True(apiResponse.IsSuccessStatusCode); } But hit this error HttpResponseMessage' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' . If I removed async Task and await , it could run successfully. 回答1: Don't call Result , it's

Why does AsyncLocal<T> return different results when code is refactored slightly?

人盡茶涼 提交于 2020-12-25 14:05:10
问题 When I call WrapperAsync AsyncLocalContext.Value returns null. When I run the same code block outside the method, in the Main method, AsyncLocalContext.Value is not null (which is what I would expect). The functionality is exactly the same yet the results are different. Is this a bug with the Asynclocal class or is there another explanation? internal class Program { private static readonly AsyncLocal<string> AsyncLocalContext = new AsyncLocal<string>(); private static void Main() { const

Why does AsyncLocal<T> return different results when code is refactored slightly?

走远了吗. 提交于 2020-12-25 14:02:49
问题 When I call WrapperAsync AsyncLocalContext.Value returns null. When I run the same code block outside the method, in the Main method, AsyncLocalContext.Value is not null (which is what I would expect). The functionality is exactly the same yet the results are different. Is this a bug with the Asynclocal class or is there another explanation? internal class Program { private static readonly AsyncLocal<string> AsyncLocalContext = new AsyncLocal<string>(); private static void Main() { const

Async Await Running Synchronously

谁都会走 提交于 2020-12-21 03:57:36
问题 I'm trying to learn async/await and created the following test code, however it is running synchronously and I'm not sure why. class Program { static void Main() { TestAsync testAsync = new TestAsync(); testAsync.Run(); Console.Read(); } } public class TestAsync { public async void Run() { Task<int> resultTask = GetInt(); Console.WriteLine("2)"); int x = await resultTask; Console.WriteLine("4)"); } public async Task<int> GetInt() { Task<int> GetIntAfterLongWaitTask = GetIntAfterLongWait();

Async Await Running Synchronously

℡╲_俬逩灬. 提交于 2020-12-21 03:57:34
问题 I'm trying to learn async/await and created the following test code, however it is running synchronously and I'm not sure why. class Program { static void Main() { TestAsync testAsync = new TestAsync(); testAsync.Run(); Console.Read(); } } public class TestAsync { public async void Run() { Task<int> resultTask = GetInt(); Console.WriteLine("2)"); int x = await resultTask; Console.WriteLine("4)"); } public async Task<int> GetInt() { Task<int> GetIntAfterLongWaitTask = GetIntAfterLongWait();

Async Await Running Synchronously

半腔热情 提交于 2020-12-21 03:55:32
问题 I'm trying to learn async/await and created the following test code, however it is running synchronously and I'm not sure why. class Program { static void Main() { TestAsync testAsync = new TestAsync(); testAsync.Run(); Console.Read(); } } public class TestAsync { public async void Run() { Task<int> resultTask = GetInt(); Console.WriteLine("2)"); int x = await resultTask; Console.WriteLine("4)"); } public async Task<int> GetInt() { Task<int> GetIntAfterLongWaitTask = GetIntAfterLongWait();

Async Await Running Synchronously

天大地大妈咪最大 提交于 2020-12-21 03:54:38
问题 I'm trying to learn async/await and created the following test code, however it is running synchronously and I'm not sure why. class Program { static void Main() { TestAsync testAsync = new TestAsync(); testAsync.Run(); Console.Read(); } } public class TestAsync { public async void Run() { Task<int> resultTask = GetInt(); Console.WriteLine("2)"); int x = await resultTask; Console.WriteLine("4)"); } public async Task<int> GetInt() { Task<int> GetIntAfterLongWaitTask = GetIntAfterLongWait();

How to execute async functions in VSCode debugger? [duplicate]

夙愿已清 提交于 2020-12-15 06:50:16
问题 This question already has answers here : How to debug async/await in visual studio code? (5 answers) Closed 19 days ago . If I drop into the VSCode debugger in some javascript code and call an asynchronous function with await it just returns a promise. How can I resolve the promise within the debugger so I can see what the result is? For example, if I define a function like so: const doAsyncThing = async () => { return new Promise((resolve) => { setTimeout(() => { resolve(5) }, 1000) }) }

“async Task” vs “return Task.Run”

元气小坏坏 提交于 2020-12-15 06:03:27
问题 I'm new to async/await c# model and trying to understand if these two options are essentially the same thing: public Task LongRunningMethod() { return Task.Run(async () => { await DoStuff(); }); } //then call it await LongRunningMethod(); and this public async Task LongRunningMethod() { await DoStuff(); } //then call it await LongRunningMethod(); I'm thinking the 1st way will use up an extra thread from the pool... And it also wraps a Task into an extra Task. Or am I wrong? 回答1: Task.Run will