Wednesday, 19 December 2012

Connecting to a WCF RESTful service from a Windows 8 Store App: Part 1 Creating the WCF Service

Update:
Part 2: Creating the Windows 8 App Now Up

This is part 1 of a simple blog to give you guys a heads up on the best way to connect a Windows 8 store app to a WCF RESTful service. I had some problems trying to get this working for a project I am on at work so thought I would share. All the source code for this project is available from my GitHub here.

This project was designed to be a quick a simple example to help others that maybe having the same problem. I started but firstly creating a Windows 8 Store application (we will look at this in more detail in part 2). If you haven't already you will need to ensure that you have a developer licence which is really easy to set up, if you don't have one Visual Studio will ask you to sign in when trying to create this application type. Some help with acquiring a developer licence.

Once I had the solution created I then went and added an additional project of WCF Service Application.In the past when I have done this I have used a specific project type for WCF RESTful service template that you can add to Visual Studio, but for this example I didn't have that to hand.



By choosing this template I had to do some more work to get it working the way I wanted. First things first I added a Global.asax top the service project and this is because I wanted to take advantage of the routing in ASP.NET and also because I have done  previous WCF RESTful services this way and I like the way it works. I then added a new route to the route table in the Global.asax.

        private void RegisterRoutes()
        {
            RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service1)));
        }

Note: When adding this you may need to add a reference to System.ServiceModel.Activation to allow for the new Service Route.

The next thing I did was to update the config file to include the bindings, endpoints and behaviorConfigurations. Below is a small snippet of the config service endpoint for the full config check the solution.

      
        
        
      

The final thing on the service I did was implement the Service and operation contract. For this example I created an additional layer that generated 3 Goat objects that I would then be able to return from the service depending on the id passed.

Service Contract:
[ServiceContract]
public interface IService1
{
    [WebGet (UriTemplate="Goat/{id}", ResponseFormat= WebMessageFormat.Json)]
    Goat GetGoatForId(string id);
}

Important things to note in the above code:
  • The use of WebGet on the method signature this is to specify the HTTP verb used for this method.
  • The URI template. This specifies the url you can use to access this method.
  • Note the type of the parameter in the signature. its string and this is because everything with URI's are string. however the id type of the object is an int, so we simple parse the id in the service method.
Service:
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    GoatService goatService = new GoatService();

    public Goat GetGoatForId(string id)
    {
        return goatService.GetGoatFromID(int.Parse(id));    
    }
}

A quick test in the browser to make sure that it is working with the URI template highlighted.


There we go a Simple WCF RESTful service ready and waiting for our Windows 8 Store app to consume, which I will follow up in Part 2 in the next few days.  

No comments:

Post a Comment