Today I am going to talk about how to create a WCF Data Service and host it using a hosting provider. My specific example will be using GoDaddy (www.GoDaddy.com). This is the site where I host my blog. I already posted on this topic, but was using Azure instead. If you have a subscription to SQL Azure, you can sign up for WCF Data Service option with a single click (https://www.sqlazurelabs.com/).
The reason I decided to write this post was the fact that I got a question from one of the users of my Windows Phone database CodePlex project. This developer was trying to speed up a query that was doing full-text search on 30,000 + records on Windows Phone. My suggestion was to host this data in the cloud instead because then you can use full power of the SQL Server (or other RDBMS) to speed up the data retrieval.
First step is to sign up for the database on GoDaddy. Most packages they sell offer a database. My package came up with 1 SQL Server database and 10 MySql databases. Also, I configured my web site on GoDaddy to use ASP.NET 4.0. I need to use .NET 4 to get support for Data Services. I could use either SQL Server of MySql, but will illustrate SQL Server in this post. Once I created database, I was able to use SSQM (SQL Server Management Studio) to connect to the DB server and add tables to my database. Then, I am going to start on WCF Data Service.
First of all, I am going to create new empty web application project by going to New Project in VS 2010.
Then, I am going to use Entity Framework to create a new model. To do so I am going to add new item and select ADO.NET Entity Data model.
I am going to choose the option to generate from the existing database in the wizard, and I am going to select only the tables I need in the mode. To connect to SQL Server on GoDaddy I needed to write down server name in the control panel on hosting option on GoDaddy. I input all the settings into the data model wizard after clicking on New Connection button.
Then I am presented with my brand spanking new entity model.
Now I need to create WCF Data Service. To do so, I am going to add new item to my web project and select WCF Data Service this time. Then I put my simple rule into the service definition, allowing all access only to the tables in my model of course.
public class CompanyWcfDataService : DataService<CompanyEntities>
public static void InitializeService(DataServiceConfiguration config)
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
So far so good. Now am I am going to test my setup locally by browsing to /CompanyWcfDataService.svc/ file in my project. Just right click on it and select Browse. Then I am going to add ContactRoles to the end of the Url and test that.I am getting the data. I will get Atom Pub page similar to any RSS feed. I am just looking for number of records those, since I
Now I need to deploy the web project. First, use content –> IIS menu to create new folder and mark it as application root as well as enable Anonymous authentication. I am going then to use file manager in GoDaddy control panel to create a folder on my web site and copy all the files for my web project there. Alternatively you can use FTP. All I would need is to transfer bin folder, .SVC file and web.config. Here is the final version of web.config:
<?xml version="1.0" encoding="utf-8"?>
For more information on how to configure your ASP.NET application, please visit
<compilation debug="true" />
<httpErrors errorMode="Detailed" />
<add name="CompanyEntities" connectionString="metadata=res://*/CompanyModel.csdl|res://*/CompanyModel.ssdl|res://*/CompanyModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=serverIP;Initial Catalog=DBName;Persist Security Info=True;User ID=UserID;Password=Password;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
A couple of things to point out. I am using configuration under system.web to troubleshoot errors. I have to update service hosting environment to configure prefix to filter out requests I need. Behavior is also setup to easy in troubleshooting.
Then I got an error – IIS specified authentication schemes ‘Basic, Anonymous’, but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used. To solve this I had to call GoDaddy support to ask them to turn off basic authentication on my site. That was the last step.
Now, you can simply add a reference to the new service in your Silverlight or phone application. See my previous posts for details on the process.
A couple of final thoughts.
- If you are transferring sensitive data, consider buying SSL certificate.
- Use error configuration to troubleshoot deployment issues.
- You can use MySQL database with entity framework, you just need to download appropriate provider.
- Almost exact same process applies to traditional WCF Service.
Please let me know if you have any questions.