Creating Web Services for the iPhone with .NET and WCF

Recently, I’ve been writing iPhone applications. The iPhone development tools are great and Objective-C is fun. Sooner or later though, an iPhone app will need to get data from a server. That’s where .NET and WCF come in.

WCF is a full-featured, highly automated way of creating Web services that run on Windows servers. However, if you are writing a service for the iPhone, there is one important thing to be aware of. In iOS, there is no built-in support for SOAP services which are the default type of services in WCF. So, your WCF services will need to be RESTful. Luckily, WCF makes this simple. I’ll walk you through the easiest way I know of to do this.

First, create a project with Visual Studio and choose the ASP.NET Empty Web Application template as shown below.

Once you have the project, select the Project | New Item menu. Then, choose AJAX-enabled WCF Service from the list of templates as shown below.

By choosing that template, the service is automatically configured as a RESTful service using the webHttpBinding binding and supports simple http get requests. An example Web.config file is shown below. Note the binding attribute in the endpoint configuration.

Now it’s time to write a service. Just create a function in the service’s code behind file and mark it with two attributes, OperationContract and WebGet. See the example code below.

public string Square(string number)
double num;
double.TryParse(number, out num);
return (num * num).ToString();

One thing to note is the default return type when using the WebGet attribute is JSON. If you prefer to return XML, you can change the attribute as shown below.

[WebGet(ResponseFormat = WebMessageFormat.Xml)]

That’s it. Now you can invoke that service using a standard http request from Objective-C. If you want to learn how to do that though, you’ll need to come to Learning Tree’s course, iPhone® and iPad® Programming (this blog is about .NET programming after all).

Doug Rehnstrom

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.