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.
namespace CompanyWeb
{
public class CompanyWcfDataService : DataService<CompanyEntities>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
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
http://go.microsoft.com/fwlink/?LinkId=169433
–>
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" />
</system.web>
<system.webServer>
<httpErrors errorMode="Detailed" />
<asp scriptErrorSentToBrowser="true"/>
</system.webServer>
<connectionStrings>
<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" />
</connectionStrings>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
<baseAddressPrefixFilters>
<clear/>
<add prefix="http://www.yoursite.com/yourservicefolder/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
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.
When all said and done, I am testing the service in IQ using the address of my service http://www.yoursite.com/yourservicefolder/CompanyWcfDataService.svc/ContactRoles
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.
Question, there is some words missing from a paragraph above, ends with ” I am just looking for number of records those, since I”.
Anyway, I am trying to set up a hi score wcf on godaddy for my windows phone 7 games so I have two tables open on my wcf, games and hiscores. When I browse my service, and then tack on games, so the url looks like this :
http://localhost:51892/SSGHiScores.svc/games
I get webpage can not be found, I noticed that the visual studio renamed games to game and tried that with the same result. The browse looks good with the atom page, but beyond that I am not sure it is working correctly. Do you have any ideas?
I am not quite sure what is going on. Maybe your portal number changed? I typicall hard-code port number in web project properties so that is stays the same. Also, make sure svc is not misspelled. You can email me the code if you would like, I’ll take a look.