In this section, we're going to go through the steps required to make a WCF Web Service in Visual
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
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.
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:
Click on the Create Virtual Directory button.
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
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.
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
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:
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:
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>
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:
However, if you're running Internet Explorer, you'll probably see this instead:
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.
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
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.