Sunday 7 January 2018

Consuming WCF Service In MVC Application Using Entity Framework

Here we post the how to consuming wcf service in mvc application using entity framework then follow the following steps.
1. Make sure that Entity framework is already installed with Visual Studio.otherwise,install it using NuGet Packages.
Create a table in your database with the name of "test" as the following.















Step 1. Create a blank solution.
File-New-Project

type the name of the solution "WCFMVCEF". Then select the template MVC then create the solution.



























Step 2: creating an WCF Application.
Right click on the WCFMVCEF solution go to Add->New Project then open "Add New Project" window select Visual C# then display the WCF Service Application.
Shown in below screen.















Select WCF Service Application and type the name as WCFServiceApp click on OK.

Step 3:
Creating a service for CRUD operation.
Right click on the WCFServiceApp project and select Add->New Item.
Choose Web option from left pane and select "WCF Service".
Type the name as "MyService.svc" and click on Add button.
















Step 4: Creating an Entity Framework Model.
Right click on the WCFServiceApp project and select Add-->New Item.





















Choose Data option from left pane and select "ADO.NET Entity data model".

























Type the name as "EntityModel.edmx" and click on Add button,same as in the following images.




















Step 5:
Write a service for CRUD operation.
  1. Open MyService.csv page from WCFService application and write the following code:
















  1. Now ,open IMyService and write the "ServiceContract" and "DataContract",as follows.

















Service has been completed .now build the service.
Run the service and copy the service URL,as shown in the following image(http://localhost:52911/MyService.svc), for creating the reference.

















Step 6 : Creating an MVC Application
Now right click on the "WCFMVCEF" solution in solution Explorer again.
Select a new project.
Select ASP .NET MVC4 Web Application.
Enter the name of the application as "MVCApplication".

Click on the OK.

Adding a project priority and setting the reference.
  1. Right click on the MVCApplication and click on add service refrence as shown in the below image.
  2. Paste the copied Service URL in the given address and press Go button.
  3. All the Services will display ,as in the following screen just give the namespace as "ServiceRefernce1" and click on OK button.























Since WCF service application and MVC application both are in the same solution ,we have to build the service first and then the MVC application in order to consume the service in MVC application .
Do the following steps for that.
  1. Right Click on the WCFMVCEF solution in solution Explorer and click on properties.
  2. Check the "Multiple Startup Project" and set the application priority for WCF and MVC application as shown in the following screen.
















Create a Model:
right click on model folder and click on class.write the class name as "User" and create the following properties.

















Create a controller:
Right click on the Controller folder and click on add controller. Give the name of controller as "HomeController" and write the following action for CRUD operation.





















Creating a View:
Creating a view is very simple just right click on All action of the controller and click on Add View .the following is the code for all views (Index).


@model IEnumerable<MVCApplication.Models.User>

@{
ViewBag.Title = "Index";
}


@using (Html.BeginForm())
{
<div>
<h2>User Details</h2>
@Html.ActionLink("Add User", "Add", "")
</div>
<div>
<table>
<tr style="background-color: #FFFACD; text-align:center">
<th style="text-align:left">
ID
</th>
<th style="text-align:left">
Name
</th>
<th style="text-align:left">
Author
</th>
</tr>

@{
foreach (var item in Model)
{
<tr style="background-color: #FFFFF0">
<td>
@item.id
</td>
<td>
@item.name
</td>

<td>
@Html.ActionLink("Edit", "Edit", new { id = @item.id }) /@Html.ActionLink("Delete", "Delete", new { id = @item.id})

</td>

</tr>
}
}

</table>

</div>


}  


Now press F5 to run the Application.

Hope your Application view will be like the following image.



























No comments:

Post a Comment