Creating a WCF REST Service with Entity Framework

1 Comment

In this post, we will go through how to use WCF to create a REST service, also using the Entity Framework to connect to a database.

We have a SQL database that holds customer information. We would like to expose this as through a REST service. In the database, we have a table called Customers that we will build our service for:

Open Visual Studio and create a new WCF Service Library project:

You will see:

If we ran this by pressing F5, it would launch the WCF Test Client:

Add a reference to ServiceModel.Web:

Let’s add an ADO  Entity Data Model:

Select EF Designer from Database:

Connect to the database:

Click Next:

Select EF 6.x:

Select the Customers table:

You will see:

Build the solution.

Open the IService1.cs file. Let’s rename this to ICustomerService:

Now let’s create some code. In this demo, we will get a list of customers in JSON, and also select a single customer through the /customer/{Id} URL.

The IService1.cs code should look like below. We are using [WebGet] and defining UriTemplates /customers and /customer/{Id}:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace Carl.WCFREST
{
    [ServiceContract]
    public interface ICustomerService
    {
        [OperationContract]
        [WebGet(UriTemplate = "/customer/{Id}", ResponseFormat=WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        Customer GetCustomer(string Id);

        [OperationContract]
        [WebGet(UriTemplate = "/customers", ResponseFormat=WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        IQueryable<Customer> GetCustomers();

    }
}

Now let’s write the Service1.cs code to get the data. We are utilizing EF here, so with very little code we can get all customers or lookup one customer:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Carl.WCFREST
{
    public class Service1 : ICustomerService
    {
        private BasicCRMEntities db = new BasicCRMEntities();

        public IQueryable<Customer> GetCustomers()
        {
            return db.Customers;
        }

        public Customer GetCustomer(string Id)
        {
            int CustomerId = Convert.ToInt32(Id);
            Customer c = db.Customers.Find(CustomerId);
            return c;
        }
    }
}

Finally, the App.config code looks like below:

Now let’s run the code. Click F5. We have this configured to run at: http://localhost:7777/CustomerService – http://localhost:7777/CustomerService/customers:

And for a single customer – http://localhost:7777/CustomerService/customer/1:

 

 

 

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, BI, Web, Cloud and Data Science.

carldesouza.comLinkedIn Twitter | YouTube

 

One Response to Creating a WCF REST Service with Entity Framework

Leave a Reply

Your email address will not be published. Required fields are marked *