Consuming WCF methods using async and await in MVC 4 application

In this article we are going to focus on how to consume WCF methods using async and await in MVC 4 application. This a simple demo of how to use async/await concept in mvc 4 application while consuming WCF service.

In this article we are going to focus on how to consume WCF methods using async and await in MVC 4 application.

Step 1: Launch Visual Studio and create a WCF Service application as shown below.
wcf service
Open the Service1.svc.cs file and modify the GetData method with the following code to make it simple for this demo.

public string GetData(int value)
return "hello";

Step 2: Now build the WCF Service and Press Ctrl +F5 to open it in browser window. Now copy the service url from the browser.

Step 3: Create MVC 4 internet application in Visual Studio.
Step 4: Right-Click References and select Add Service Reference -> Now paste the url we have copied in Step 2. Click on Go. This will discover the WCF Service.
add reference
Step 5: In the Add Service Reference window -> click on Advanced and then in the Service Reference Settings window, make sure "Generate task based operations" is checked. Then select "Reuse types in specified referenced assemblies and then select "Newtonsoft.Json" and click on ok. This will add wcf service reference in mvc project.
advanced setting async await wcf
Step 6: Open HomeController.cs file. add the reference to below namespace at the top of the file.
using System.Threading.Tasks;
Step 7: Now use the async/await pattern to call the GetData method of the WCF service.
public async Task CallMyWCFService()
ServiceReference1.Service1Client c = new ServiceReference1.Service1Client();
var taskAsync = await c.GetDataAsync(5);
ViewBag.Result = taskAsync.ToString();
return View();


Step 8: Now right click the above action method and select "Add View". Then click on OK. This adds the view named "CallMyWCFService" to the MVC project. Add the following code in your view.

ViewBag.Title = "Test WCF Service";

<h2>Test WCF Service async await pattern</h2>

The response from the service is : @ViewBag.Result

Step 9: Open the browser and access this view as shown below. Here the port number may vary based on the port on which your mvc application is running.


Please note that if you are unable to add reference to wcf service and getting error as unable to find service in the solution then run svcutil.exe utility from the visualstudio command prompt.
svcutil.exe http://localhost:24743/Service1.svc?wsdl

wcf service


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: