This article includes a step by step tutorial to learn WCF using MVC in Visual Studio. We are also using the Entity Framework (.edmx model) for database operations. This scenario targets the user of Entity Framework Model's first approach that consumes WCF service which is consumed in MVC applications for CRUD operations.

Note :

  1. Make sure that Entity Framework is already installed with Visual Studio. Otherwise, install it using NuGet Packages.
  2. Create a table in your database with the name of "UserDetail", as the following.

    Id int
    Name Nvarchar(250)
    Email Nvarchar(250)

    table

Step 1. Crate a blank solution

Step 2. Creating an WCF Application

Step 3. Creating a service for CRUD operation.

Step 4. Creating an Entity Framework Model.

Step 5. Write a service for CRUD operation

  1. Open MyService.csv page from WcfService application and write the following code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Data;
    4. using System.Linq;
    5. using System.Runtime.Serialization;
    6. using System.ServiceModel;
    7. using System.Text;
    8. namespace WcfServiceApp
    9. {
    10. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MyService" in code, svc and config file together.
    11. // NOTE: In order to launch WCF Test Client for testing this service, please select MyService.svc or MyService.svc.cs at the Solution Explorer and start debugging.
    12. public class MyService : IMyService
    13. {
    14. public void DoWork()
    15. {
    16. }
    17. public List<UserDetail> GetAllUser()
    18. {
    19. List<UserDetail> userlst = new List<UserDetail>();
    20. TestDBEntities tstDb = new TestDBEntities();
    21. var lstUsr = from k in tstDb.UserDetails select k;
    22. foreach (var item in lstUsr)
    23. {
    24. UserDetail usr = new UserDetail();
    25. usr.Id = item.Id;
    26. usr.Name = item.Name;
    27. usr.Email = item.Email;
    28. userlst.Add(usr);
    29. }
    30. return userlst;
    31. }
    32. public UserDetail GetAllUserById(int id)
    33. {
    34. TestDBEntities tstDb = new TestDBEntities();
    35. var lstUsr = from k in tstDb.UserDetails where k.Id==id select k;
    36. UserDetail usr = new UserDetail();
    37. foreach (var item in lstUsr)
    38. {
    39. usr.Id = item.Id;
    40. usr.Name = item.Name;
    41. usr.Email = item.Email;
    42. }
    43. return usr;
    44. }
    45. public int DeleteUserById(int Id)
    46. {
    47. TestDBEntities tstDb = new TestDBEntities();
    48. UserDetail usrdtl = new UserDetail();
    49. usrdtl.Id = Id;
    50. tstDb.Entry(usrdtl).State = EntityState.Deleted;
    51. int Retval = tstDb.SaveChanges();
    52. return Retval;
    53. }
    54. public int AddUser(string Name, string Email)
    55. {
    56. TestDBEntities tstDb = new TestDBEntities();
    57. UserDetail usrdtl = new UserDetail();
    58. usrdtl.Name = Name;
    59. usrdtl.Email = Email;
    60. tstDb.UserDetails.Add(usrdtl);
    61. int Retval = tstDb.SaveChanges();
    62. return Retval;
    63. }
    64. public int UpdateUser(int Id,string Name, string Email)
    65. {
    66. TestDBEntities tstDb = new TestDBEntities();
    67. UserDetail usrdtl = new UserDetail();
    68. usrdtl.Id = Id;
    69. usrdtl.Name = Name;
    70. usrdtl.Email = Email;
    71. tstDb.Entry(usrdtl).State = EntityState.Modified;
    72. int Retval = tstDb.SaveChanges();
    73. return Retval;
    74. }
    75. }
    76. }
  2. Now, Open IMyService and write the "ServiceContract" and "DatatContract", as follows.
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Runtime.Serialization;
    5. using System.ServiceModel;
    6. using System.Text;
    7. namespace WcfServiceApp
    8. {
    9. // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMyService" in both code and config file together.
    10. [ServiceContract]
    11. public interface IMyService
    12. {
    13. [OperationContract]
    14. List<UserDetail> GetAllUser();
    15. [OperationContract]
    16. int AddUser(string Name, string Email);
    17. [OperationContract]
    18. UserDetail GetAllUserById(int id);
    19. [OperationContract]
    20. int UpdateUser(int Id, string Name, string Email);
    21. [OperationContract]
    22. int DeleteUserById(int Id);
    23. }
    24. [DataContract]
    25. public class UserDetails
    26. {
    27. [DataMember]
    28. public int Id { get; set; }
    29. [DataMember]
    30. public string Name { get; set; }
    31. [DataMember]
    32. public string Email { get; set; }
    33. }
    34. }

Service has been completed. Now, build the service.

  1. Press F5 to run the Service.
  2. Copy the Service URL, as shown in following image (localhost:1034/MyService.svc), for creating the reference.

    Service URL

Step 6. Creating an MVC Application

Adding a Project Priority and setting the reference

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 for that,

Create a Model

Right click on Model folder and click on Class. Write the class name as "User" and create the following properties.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace MvcApp.Models
  6. {
  7. public class User
  8. {
  9. public int Id { get; set; }
  10. public string Name { get; set; }
  11. public string Email { get; set; }
  12. }
  13. }
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.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MvcApp.Models;
  7. namespace MvcApp.Controllers
  8. {
  9. public class HomeController : Controller
  10. {
  11. //
  12. // GET: /Home/
  13. ServiceReference1.MyServiceClient ur = new ServiceReference1.MyServiceClient();
  14. public ActionResult Index()
  15. {
  16. List<User> lstRecord = new List<User>();
  17. var lst = ur.GetAllUser();
  18. foreach (var item in lst)
  19. {
  20. User usr = new User();
  21. usr.Id = item.Id;
  22. usr.Name = item.Name;
  23. usr.Email = item.Email;
  24. lstRecord.Add(usr);
  25. }
  26. return View(lstRecord);
  27. }
  28. public ActionResult Add()
  29. {
  30. return View();
  31. }
  32. [HttpPost]
  33. public ActionResult Add(User mdl)
  34. {
  35. User usr= new User();
  36. usr.Name=mdl.Name;
  37. usr.Email=mdl.Email;
  38. ur.AddUser(usr.Name,usr.Email);
  39. return RedirectToAction("Index", "Home");
  40. }
  41. public ActionResult Delete(int id)
  42. {
  43. int retval = ur.DeleteUserById(id);
  44. if (retval > 0)
  45. {
  46. return RedirectToAction("Index", "Home");
  47. }
  48. return View();
  49. }
  50. public ActionResult Edit(int id)
  51. {
  52. var lst = ur.GetAllUserById(id);
  53. User usr = new User();
  54. usr.Id = lst.Id;
  55. usr.Name = lst.Name;
  56. usr.Email = lst.Email;
  57. return View(usr);
  58. }
  59. [HttpPost]
  60. public ActionResult Edit(User mdl)
  61. {
  62. User usr = new User();
  63. usr.Id = mdl.Id;
  64. usr.Name = mdl.Name;
  65. usr.Email = mdl.Email;
  66. int Retval = ur.UpdateUser(usr.Id, usr.Name, usr.Email);
  67. if (Retval > 0)
  68. {
  69. return RedirectToAction("Index", "Home");
  70. }
  71. return View();
  72. }
  73. }
  74. }
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, Add, Edit).

Index.cshtml
  1. @model IEnumerable<MvcApp.Models.User>
  2. @{
  3. ViewBag.Title = "Index";
  4. }
  5. @using (Html.BeginForm()){
  6. <div>
  7. <h2>User Details</h2>
  8. @Html.ActionLink("Add User", "Add", "")
  9. </div>
  10. <div>
  11. <table >
  12. <tr style="background-color: #FFFACD; text-align:center">
  13. <th style="text-align:left">
  14. Name
  15. </th>
  16. <th style="text-align:left">
  17. Email
  18. </th>
  19. <th style="text-align:left">
  20. Manage
  21. </th>
  22. </tr>
  23. @{
  24. foreach (var item in Model)
  25. {
  26. <tr style="background-color: #FFFFF0">
  27. <td>
  28. @item.Name
  29. </td>
  30. <td>
  31. @item.Email
  32. </td>
  33. <td>
  34. @Html.ActionLink("Edit", "Edit", new { id = @item.Id }) /@Html.ActionLink("Delete", "Delete", new {id[email protected] })
  35. </td>
  36. </tr>
  37. }
  38. }
  39. </table>
  40. </div>
  41. }
Add.cshtml
  1. @model MvcApp.Models.User
  2. @{
  3. ViewBag.Title = "Add";
  4. }
  5. <h2>Add New User</h2>
  6. @using (Html.BeginForm()) {
  7. <div style="text-align:center">
  8. <table>
  9. <tr>
  10. <td>
  11. Name :
  12. </td>
  13. <td>
  14. @Html.TextBoxFor(m=>m.Name)
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>
  19. Email :
  20. </td>
  21. <td>
  22. @Html.TextBoxFor(m=>m.Email)
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>
  27. Email :
  28. </td>
  29. <td>
  30. <input type="submit" value="Submit" />
  31. </td>
  32. </tr>
  33. </table>
  34. </div>
  35. }
Edit.cshtml
  1. @model MvcApp.Models.User
  2. @{
  3. ViewBag.Title = "Edit";
  4. }
  5. <h2>Edit User</h2>
  6. @using (Html.BeginForm()) {
  7. <div style="text-align:center">
  8. <table>
  9. <tr>
  10. <td>
  11. Name :
  12. </td>
  13. <td>
  14. @Html.TextBoxFor(m=>m.Name)
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>
  19. Email :
  20. </td>
  21. <td>
  22. @Html.TextBoxFor(m=>m.Email)
  23. </td>
  24. </tr>
  25. <tr>
  26. <td>
  27. </td>
  28. <td>
  29. <input type="submit" value="Update" />
  30. </td>
  31. </tr>
  32. </table>
  33. </div>
  34. }
Now, press F5 to run the Application

Hope your Application View will be like the following image.

Apllcation

And , Like following.

Apllcation
Apllcation