WCF Web Services & iOS - Part 1


In this section, we're going to go through the steps required to make a WCF Web Service in Visual Studio 2013.
As you'll see, it's quite straight forward if you get all the steps right.

Personally, I found it hard to find one up-to-date resource which would take me through these steps.

Our ultimate goal is to send data from a SQL Server database to an iOS device.
We will create a WCF Web Service to read in data from the SQL Server database, and return it in a generic, non-Microsoft (JSON) format, which the iOS device will be able to understand.

SQL Server, to iOS, via WCF Web Services

This section deals with setting up and testing a very basic WCF Web Service, which returns JSON data, and is ready for us to link to the Northwind SQL Server database in the next section.

To follow this tutorial, you will need the following:

  • A copy of Microsoft Visual Studio 2012 or 2013
    (Older versions should work, with minor alterations though.)

Creating your first WCF Web Service

1. Start Visual Studio
2. Create a new project
Click on File \ New \ Project.
In the New Project window, click on Templates \ Visual C# \ WCF.
We're going to create a .Net Framework 4.5 web service, so make sure this is selected at the top of the dialog.
Select the project type WCF Service Application.
In the Name textbox, type in JSONWebService.
Then click on OK.
Browser list of files, or WCF Test Client
3. Set the Port number
Note: this step isn't actually needed, but it will help you follow this tutorial, and use the same URL links.
In your Solution Explorer window, right-click on your project name and select "Properties".
Click on the Web tab on the left.
In the Project Url text box, change the URL to: http://localhost:15021/
Click on the Create Virtual Directory button.
Project properties
I'll say it again - in real world apps, you don't need to do this step !
Changing this URL value will simply make it easier this walkthrough, as you'll be able to use the same URLs for development that our tutorial uses.
4. Run the project
Click on Debug \ Start Debugging to run this sample project.
You should now see a new WCF Test Client window, showing a few example web services which Visual Studio included in your project.
Browser list of files, or WCF Test Client
5. Run one of the sample web services
Visual Studio has provided us a few example web services, so let's see what happens when we run one of them.
Double-click on the GetData() web service, type in a number in the Value column, then hit the "Invoke" button.
The WCF Test Client
In the Response section, you'll see the response which the web service returned for us.
"You entered: 13"

6. Change the web service to return JSON results
Next, we're going to change this GetData web service, so we can call it directly from a web browser.
Once we've done this, we'll be able to call the web service, and see it's results, simply by passing in the web service name (e.g. GetData) and our numeric value (e.g. 13) within the URL:
Our modified web service
To do this, we need to make some changes to the sample code. First, we need to change GetData's parameter type from "int" to "string"
Open up the Service1.svc.cs file, and change the "value" parameter to be a string:

public string GetData(string value)
    return string.Format("You entered: {0}", value);

Now, hop across to the IService1.cs file, and replace the original lines

string GetData(int value);

...with this..

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getData/{value}")]
string GetData(string value);

The "ResponseFormat" attribute tells WCF that we want our service to return a JSON string. Without this, the web service would return your data in XML format instead.
Note also that we set the "UriTemplate" attribute, to specify that we will always be passing a parameter when we call this URL. So, we've set the UriTemplate as this..

UriTemplate = "getData/{value}"

..as we are going to call this function and always pass it a parameter, like this..


Now, we need to change our web.config file.
This is the fiddly bit, and you might need to apply different changes if you are using an older version of Visual Studio.
First, we need to add the following <services> tag inside the <serviceModel> tag.

    <service name="JSONWebService.Service1">
      <endpoint address="../Service1.svc"
        behaviorConfiguration="webBehaviour" />


Next, in the <behaviors> tag, add the following <endpointBehaviors> tag:
(If your web.config file doesn't have a serviceBehaviors section, then you'll need to add these lines aswell.)

      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    <behavior name="webBehaviour">


And that's it.
Let's run the project again.
This time, ignore the WCF Test Client window, and instead, open up a browser, and try running the web service with your own parameters, for example:


If you're running the Chrome browser, you'll see the results straightaway:
Our modified web service
However, if you're running Internet Explorer, you'll probably see this instead:
Do you want to save this file?
Annoyingly, Internet Explorer doesn't display JSON data by default, and insists on saving it to an external file.
Thankfully, there's a simple solution to this.
Download and run this reg file to change the two registry settings on your PC, which will allow IE to automatically display JSON results. Then, close and re-run Internet Explorer, and try the URL again.
Internet Explorer, tamed.
(This excellent fix was take from this Stack Overflow page.)
So, the good news is that we can now call our web services directly from a browser, and instantly see the results. The bad news is that we can no longer use the WCF Test Client, as it doesn't work with JSON/REST services like these.


Brilliant !
We haven't actually started writing our Northwind web services yet, but at least the pieces are in place for us to begin. (And what you don't realise is that it took me hours of experimenting and Googling to get this far myself !)
In the next section, we'll take this a step further, and get our JSON services to return some database records.


You'll notice I'm deliberately not explaining what all of these changes are for, or what they mean. There is a wealth of bulky articles out there to explain all of this (for example, try Googling "web.config endpoints" if you want to understand why we added this change to the web.config file) but my aim is to simply show you how to create a complete, general purpose, reuseable web service project with as little pain as possible.
This is the article I wish that I'd had, when I faced all of these challenges.
< Previous Page
Next Page >


blog comments powered by Disqus