Last reviewed: 11/04/2012
In tutorial 1 you created a simple golfer message board application. In tutorial 2 you updated the application to store message board data, which includes a name and a message, in a SQL Database.
This tutorial is based on these two previous tutorials. In this tutorial you extend the Web application to add a data service that implements the Open Data Protocol (OData). This OData service uses the SQL Database created in tutorial 2.
Note: The tutorial code has been tested on Windows Azure SDK (October 2012).
In this tutorial, you will learn how to:
Note the following requirements before you begin this lesson:
The data service runs in the Windows Azure Web role and uses Entity Framework to access data from the SQL Database service. For more general information on the Windows Azure and SQL Database architecture used in this application, see the section Understanding the Architecture in tutorial 2.
In tutorial 2, the application accessed the GolferMessageBoardDB database by using a data source based on LINQ-to-SQL classes.
You need to use the Entity Data Model wizard to add an Entity Framework data model to the project. This data model will be used by the data service to access the database. When you create a new Entity Data Model in your project, the wizard also adds the appropriate assembly references to the project.
In this lesson, you will go through the following procedures:
To open the Golfer Message Board application
To create the Entity Data Model
Installed Templates
Visual C# , Data
Template
ADO.NET Entity Data Model
Name
MessageBoard.edmx
Which data connection should your application use to connect to the database?
GolferMessageBoardDBConnectionString
Yes, include the sensitive data in the connection string
Save entity connection settings in Web.config as:
GolferMessageBoardDBEntities
Security Note: To properly secure the SQL Database connection string, you should encrypt the Web.config file. Encrypting the Web.config file is outside the scope of this tutorial. For an example of how to do this, see the blog series on Windows Azure SQL Database
Enable these options:
GolferMessageBoardDBModel
Message2
Entity Set Name
Messages
Note: By default, both the Object Relational Designer (LINQ-to-SQL) and the Entity Designer (LINQ-to-Entities) generate the Message type in the default project namespace. To prevent errors when building the project, you need to rename one of the types to Message2. The entity set name is used by WCF Data Services as the name of the feed, which is changed to Messages.
To compile the MessageBoard_WebRole project
In Solution Explorer, right-click MessageBoard_WebRole, and then click Rebulid. Make sure the project is compiled successfully.
In this step, you modified the golfer message board application to add an Entity Framework data model. This data model and generated classes are used to connect to SQL Database by using the Entity Framework.
You will use the WCF Data Services item template to add the code to the project that defines the OData service. This data service uses the new Entity Framework data model.
Return to Top
In this lesson, you add the code for the OData service, which uses the data model created in the previous lesson.
This template adds both a markup page (.svc) and a code-behind page to the project. It also adds the appropriate assembly references to the project.
Note: When you create a data service such that the generic type of the DataService<T> class is the strongly-typed ObjectContext of an Entity Framework data model, WCF Data Services uses the Entity Framework provider to access and change data in the database. For more information, see Entity Framework Provider (WCF Data Services).
To add WCF Data Services code files to the project
Visual C# , Web
WCF Data Service
MessageBoard.svc
To modify the WCF Data Services code files to use the data model
/* TODO: put your data source class name here */
public class MessageBoard : DataService<GolferMessageBoardDBEntities>
// This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { // Grant only the rights needed to support client applications, // in this case read all and insert new messages. config.SetEntitySetAccessRule("Messages", EntitySetRights.AllRead | EntitySetRights.WriteAppend); }
In this step, you used WCF Data Services to add an OData feed to the golfer message board application, which accesses the SQL Database by using and the Entity Framework provider. This enables you to read messages as OData feeds and insert new messages by sending POST requests to the OData service.
You will test and deploy the application.
In this lesson, you test the application in Windows Azure computer emulator, package the application, and then deploy the application to Windows Azure.
Note: You must disable feed reading view in the browser to view the raw Atom XML feed returned from the OData service.
To disable feed reading view in Internet Explorer
Note: If you are using a Web browser other than Internet Explorer and if your browser cannot display the feed as raw XML data, you should still be able to view the feed as the source code for the page.
To test the application in the computer emulator
Note: If the feed returned appears empty, then you will need to first insert some data into the SQL Database. The easiest way to add new messages to the database is by entering them in the default.aspx page of the Web application.
After the application is tested successfully in the compute emulator environment, the next step is to create the service package and then deploy the application to Windows Azure.
To generate the service package
Service configuration
To sign in to Windows Azure
Note: If you haven’t had a Windows Azure Platform subscription, see the Provisioning Windows Azure section of this tutorial.
You need to create a cloud service for the Web role. If you have created a cloud service account in tutorial 1 or [[Windows Azure and SQL Database Tutorials - Tutorial 2: Using SQL Database (en-US)|tutorial 2]], you can skip this step.
To create a cloud service
Note: The URL prefix must be unique.
To deploy the application to the staging environment
To test the application in the staging environment
After the application is working correctly in the staging environment, you are ready to promote it to the production environment.
To promote the application to production
Note: Some DNS services take longer to replicate the records. If you get a page not found error, you might need to try browsing to the URL again in a few minutes.
In this step, you tested and deployed the golfer message board OData service to Windows Azure.
Congratulations! You have completed the tutorial 2.1. Now that you have deployed the data service, you can create client applications that consume the Messages OData feed. For more information on how to create client applications that access OData services, see one of the following topics in the MSDN library:
For a complete list of client libraries that support OData, see the OData SDK.
Richard Mueller edited Revision 13. Comment: Replace RGB values with color names in HTML to restore colors
Jonathan Gao edited Revision 10. Comment: New version
Jonathan Gao edited Revision 7. Comment: fix links
patmas57 edited Revision 5. Comment: Branding updates
Fernando Lugão Veltem edited Revision 4. Comment: added TOC and tags
Ed Price - MSFT edited Revision 1. Comment: Fixing the TOC links
Ed Price - MSFT edited Revision 2. Comment: Fixed links to top of page.
Fantastic article!!
The "WCF Data Services Client Library" links points to a staging environment that is not accessible to the public.
Glenn Gailey [MSFT] edited Revision 3. Comment: Fixed link to the WCF Data Services client docs
Thanks Thomas. I fixed that bad link.
Jonathan Gao edited Revision 8. Comment: fix links