Posts Tagged 'iPhone'

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

Learning Tree International

.NET & Visual Studio Courses

Learning Tree offers over 210 IT training and Management courses, including a full curriculum of .NET training courses.

Free White Papers

Questions on current IT or Management topics? Access our Complete Online Resource Library of over 65 White Papers, Articles and Podcasts

Enter your email address to subscribe to this blog and receive notifications of new posts by e-mail.

Join 29 other followers

Follow Learning Tree on Twitter


Do you need a customized .NET training solution delivered at your facility?

Last year Learning Tree held nearly 2,500 on-site training events worldwide. To find out more about hosting one at your location, click here for a free consultation.
Live, online training

%d bloggers like this: