Thursday, October 4, 2012

WCF REST Data service in Visual Studio 2012 Express for Web

If you are using Visual Studio Express for Web, you might notice that the WCF Data Service template is not available to use.


I’m going to explain a step-by-step guide to create a WCF Data Service.


  • Create an empty Web project with the name WcfDataServiceInExpressEditions:

  • Add the following nuget packages:


  • Add the following settings to the web.config right under <configuration>


    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"></modules>
    </system.webServer>
    <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    
  • Create an ADO.NET Entity Data Model and connect to any database you own and drag a couple of tables to the designer

  • Add a new WCF Service to your project name it: PubsService

  • Delete the file containing the interface IPubsService.cs

  • Change the declaration of your service as follows:


    public class PubsService : DataService<Pubs>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            config.SetEntitySetAccessRule("jobs", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        }
    }
    
  • Change the markup of the .svc file to include the correct factory:


    Factory="System.Data.Services.DataServiceHostFactory, Microsoft.Data.Services"
    

That’s it, run the application, and you will see something like this:



This is the list of all the tables configured in the InitializeService method


To test the queries try something like this:


http://localhost:55575/PubsService.svc/jobs/?$filter=min_lvl gt 150



Download the code of this article


Happy coding

3 comments:

  1. I can run your project, alternative ?

    ReplyDelete
    Replies
    1. The Visual Studio version used in this article is:

      Microsoft Visual Studio Express 2012 for Web
      Version 11.0.50727.1 RTMREL
      Microsoft .NET Framework
      Version 4.5.50709

      What's the exact error you are receiving?

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

If you found this post useful please leave your comments, I will be happy to hear from you.