WCF Tutorial
www.Learn2Expert.net A new ASP.Net MVC 4, SSIS, Interview Q/A tutorial - Visit - www.Learn2Expert.net
Skip Navigation LinksHomeCreating WCF Data Service 4.5 No of Views: 28529
WCF tutorial | Creating WCF Data Service

Creating WCF Data Service

Download Source:

WCF_DataService.zip

In this article let's understand WCF Data Service and OData by creating the WCF Data Service and expose the data using OData protocol. It is explained by exposing the "Person" details from "AdventureWorksDatabase" as data service using OData protocol.

Later we can see how to read the "Person" details from web browser in different format and using different filter condition.

Later .Net client application is created to consume the data service and query the "Person" information using query.

Step 1:

Create an empty web application project by selecting "ASP.NET Empty Web Application" template.

Step 2:

Now we need to create a Data model which needs to be exposed as service. So let's try to add new data model to map the "Person" table from "AdventureWorksDatabase" as entity. This entity collection will later be exposed as service for querying.

Right click the project and select "Add"->"New Item" and select "ADO.Net Entity Data Model"

Step 3:

Entity Data Model Wizard will appear select "Generate from Database" and click "Next"

Step 4:

In the Database connection click the "New Connection" button and specify the server name and select "AdventureWorks" database and click "OK"

Step 5:

Now the connection string will be displayed as show below and click "Next"

Step 6:

This section is used to select the table, views or store produce for which entity need to be defined. Select "Person" table from "Person" schema and click "Finish". This will create the "Person" entity which is mapped to the database will be created.

Step 7:

Now Entity Data Model is created for the "Person" table and it is exposed as property with name "People".

File: "AdventureWorksModel.Context.cs"

    public partial class AdventureWorks2012Entities : DbContext
    {
        public AdventureWorks2012Entities()
            : base("name=AdventureWorks2012Entities")
        {

        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
    
        public DbSet<Person> People { get; set; }
    }

           

Step 8:

Let's expose this data as resource using Data service. Right click the project select "Add"->"New Item" and select "WCF Data Service 5.6"

Step 9:

In Entity Data Model, data context object is the main type to map the all the table, views, SP from database to Entity collection using property. Data Service class can be defined based on the EDM data context as shown below.

You can also set the access rights like (Read, Write, Read/Write, delete etc) to the property of the EDM in data service. These operations are done using the HTTP verbs like GET, POST, PUT etc.

Below example, I have set the "Read" access to the "People" property of the data context. So collection of "Person" can be read by any one by applying query expression.

Step 10:

Run the service and you will see the output as shown below. This shows that services expose the collection of "People" entity.

Step 11:

Enter the URL as mention below to return all the "People" resources in form of AtomPub

URL : "http://localhost:24641/MyODataService.svc/People"

Step 12:

You can get the data in JSON format by mentioning the format in the URL

URL: "http://localhost:24641/MyODataService.svc/People()?$format=verbosejson"

Step 13:

Now we are done with data service creation, let's start creating the client application to consume this OData service.

Create a new .Net console application and add service reference as shown below with OData service url

Step 14:

Enter the source code as shown below and run the application

Step 15:

While debugging the application, you can see that client application send the request to OData service through URL with all filter condition to read the resource information.

Step 16:

Output will all the resource information received using url are shown below

Tips!

  • Always create the service with Interface->Implementation format, mention the contract in Interface.
  • Define the service in Class library and refer the class library in Host project. Don’t use service class in host project.
  • Change the instance mode to per call as default.
  • Always catch exception using try/catch block and throw exception using FaultException < T >.
  • Logging and Include exception should be enable while compiling the project in debug mode. While in production deployment disable the logging and Include exception details.