Accessing a WCF Service in an ASP.Net MVC Application
In this article we will see how to access a WCF service and use it in our ASP.Net MVC application.
In this article we will see how to access a WCF service and use it in our ASP.Net MVC application. In my previous articles we have seen the creation and consumtion of WCF services by a console client and a WPF client. In this article we will create the client as a MVC application.
For creating a WCF service you can read more over here. For this article we will create one simple WCF service which will return the dataset and in the MVC application we will simply access this WCF service and display the list of authors on the View. So let's start by creating the WCF service.
Creating WCF Author Service
Create a new WCF project in Visual Studio and name it AuthorService. When the service is created a default service will be created; delete it and add a new service to the project and name it ServiceAuthor which will create an IServiceAuthor Interface and a ServiceAuthor.cs class. Define one method in IServiceAuthor which returns a Dataset and in ServiceAuthor.cs implement the method which returns the dataset like below. Here I'm creating it on the fly but you can populate it from a database.
public interface IServiceAuthor
public class ServiceAuthor : IServiceAuthor
public void DoWork()
public System.Data.DataSet ReturnAuthor()
DataSet ds = new DataSet(); DataTable dt = new DataTable("Author");
DataColumn dc1 = new DataColumn("AuthorId", typeof(Int32));
DataColumn dc2 = new DataColumn("Name", typeof(string));
DataColumn dc3 = new DataColumn("Location", typeof(string));
DataRow dr1 = dt.NewRow();
dr1 = 10;
dr1 = "Krishna Garad";
dr1 = "India";
DataRow dr2 = dt.NewRow();
dr2 = 20;
dr2 = "Mahesh Chand";
dr2 = "USA";
Now run the service application and copy the address.
Creating MVC client Application
Start a new MVC web application; add the controller in the controller folder with the name Home. Now add the service reference by right-clicking on the solution and selecting Service Reference and paste the copied address in the address box in the opened dialog like below.
Now we also have a service reference, so create the proxy for our WCF service like below in the Home controller.
ClientAuthorReference.ServiceAuthorClient obj = new ClientAuthorReference.ServiceAuthorClient();
Still now we have a proxy for our WCF service; now we can call the methods of our WCF service. On start up only we will show the AuthorList return from WCF service so in the Index Action write the following code and add the view to display the List of Authors.
public ActionResult Index()
DataSet ds = obj.ReturnAuthor();
ViewBag.AuthorList = ds.Tables;
Add a view to display the list of Authors by looping over the DataTables which we supplied to ViewBag. Write the following code in your aspx page.
<%foreach (System.Data.DataRow dr in ViewBag.AuthorList.Rows)
<% } %>
In the above markup we are simply reading each row and displaying on the page. Now run the application and view the Author List returned from WCF service.
In this way we can use a WCF service as a Model for our MVC application.