In my previous blog posts, I have wrote about how to consume an Open Data Protocol (OData) feed. In those posts, I used the NerdDinner OData end point. Right now, maybe a few of you are wondering, how can I make that OData service in .NET? Well, it’s actually not as hard as you thought it would be and I’m going to explain it how. Oh I almost forgot, in this example, I will use the Northwind sampe database which can be downloaded in here.
1. Create a new WCF Service application
If you look at the solution explorer, you will see that we have IService1.cs and Service1.svc. Just delete these two files. We don’t need them anyway.
2. Define the data model
To define the data model, I’m going to use Entity Framework. Maybe you are going to ask me, why am I using the Entity Framework? Well, it’s simply because Entity Framework make this task a whole lot easier so we can save time. To create an Entity Framework, you need to add a new ADO.NET Entity Data Model. I will name it Northwind.edmx because I’m using the Northwind database.
In the Entity Data Model Wizard, select Generate from Database.
Then choose your connection and create the connection string.
Pick the tables that you want to publish and click Finish.
3. Create the data service
OK, we already have the data model so it’s time to create the data service. We need to add a new WCF Data Service to our project.
After the WCF Data Service have been created, you need to change the /* TODO: put your data source class name here */ with the name of the entity container of the data model, which in my case is NorthwindEntities.
4. Enabling access to data service resources
Put the code below to the InitializeService function in Northwind.svc.cs file.
And that’s it. You can now run the application. But before you do, maybe you need to turn off the feed reading view in Internet Explorer which is located in Internet Options->Content and choose Settings in the Feed and Web Slices then uncheck the Turn on feed reading view. After you run the application, it will appear something like this:
Just test that out with the OData query in here. Enjoy…