1

I am trying to implement asynchronous web api call from asp.net

My code is similar like this

var response = httpClient.PostAsJsonAsync("api/apicontroller/execute/", executeModel).Result;

This is working in synchronous mode.

I do not want to wait for my webapi call to complete. How I can implement this using .net framework 4?

2 Answers 2

2

Change the line to:

var response = await httpClient.PostAsJsonAsync("api/apicontroller/execute/", executeModel);

Checking the Result property makes it synchronous. Also make sure your method signature contains the async keyword.

edit: If you don't want to wait for the result right away, you can postpone the await like this (or never perform it):

var task = httpClient.PostAsJsonAsync("api/apicontroller/execute/", executeModel);
/* Do other stuff in parallell here */
var result = await task;
Sign up to request clarification or add additional context in comments.

Comments

1

How I can implement this using .net framework 4?

You can't use async, await, or HttpClient on ASP.NET 4. They all require ASP.NET 4.5. Your options are to upgrade to ASP.NET 4.5 and use the natural await syntax or to stay on ASP.NET 4 and use something like WebClient.

2 Comments

There's also the Microsoft.Bcl.Async package on NuGet, if you're using .NET 4.0 in VS2012.
@DanielMann: No. Microsoft.Bcl.Async is not supported on ASP.NET. It's only for desktop/mobile/store projects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.