6

I am creating an iOS application that I need to connect to a database through a web service. I only know basic knowledge about using RESTful web services, I have never written my own before and was wondering if you can give me any advice on where I can find out how to write my own RESTful web service.

In my iOS program I will be sending a part number to the web service the web service will then need to return color and size information on the part. I'm not sure if XML is the best format or is there something better?

I guess my question is twofold here:

  1. Is this something I should be doing with a RESTful web service?
  2. Where can I find tutorials on creating a .NET-based RESTful web service?
Farray
  • 8,290
  • 3
  • 33
  • 37
ios85
  • 2,104
  • 7
  • 37
  • 55

6 Answers6

6

You can use WCF for creating RESTful services, and you could use Nancy:

I'd recomend using json as a data format see here for some excelent links: iPhone/iOS JSON parsing tutorial

in wcf you'd go about creating a service like this: see here for a reasonable example: http://blogs.msdn.com/b/kaevans/archive/2008/04/03/creating-restful-services-using-wcf.aspx

[ServiceContract]
public interface IServeStuff
{
    [OperationContract]
    [WebGet(UriTemplate = "/stuff/{id}", 
            ResponseFormat = WebMessageFormat.Json)]
    Stuff GetStuff(string id);
}

public class StuffService : IServeStuff
{
    public Stuff GetStuff(string id)
    {
         return new Stuff(id);
    }
}

Or with nancy http://www.nancyfx.org/ like this:

public MyModule : NancyModule
{
    public MyModule()
    {  
        Get["/stuff/{id}"] = parameters => {
            return new Stuff(parameters.id).AsJson();
        };
    }
}

But before all this listen to @PeterKelly because he's right

Community
  • 1
  • 1
albertjan
  • 7,739
  • 6
  • 44
  • 74
  • Nancy is awesome, it really is low ceremony web programming. Very intuitive way (for me) of writing web apps. We do almost all frontend development in javascript and provide the data with nancy. – albertjan Nov 29 '11 at 15:47
5

My advice would be to implement this using ASP.NET MVC3 - as this provides a nice controller-action paradigm which is excellent for implementing a REST service. You could use WCF, and I am sure it would work fine, but from personal experience I found MVC3 to be very easy to use to write a REST back-end for an iOS client.

I would recommend using JSON and not XML, primarily as it is more concise than XML, but it has other advantages should you decide later to implement a web front end for your database, as Javascript has good support for JSON.

There are a number of JSON libraries for iOS, including SBJSON and YAJL

There is also a well regarded framework for iOS REST implementations called RestKit.

As for tutorials for implementing REST using the MVC famework, this might be one to look at.

iandotkelly
  • 9,024
  • 8
  • 48
  • 67
  • Reason for downvote? I ask not because of the (minor) reputation damage - its just to inform the debate? – iandotkelly Dec 02 '11 at 13:59
  • PS - the Nancy answer to this question is also good. I have used Nancy since writing this answer and it was also excellent. – iandotkelly Aug 07 '12 at 15:24
3

Seeing as you have little experience with REST, I would first learn about the concept. It is important to understand what it is (it is not just pretty URLs) fundamentally before proceeding to design your service.

  1. I would start with reading Chapter 5 of Roy Fielding's dissertation (where the term REST originated - read the whole paper if you like).
  2. I would then move on to the excellent RESTful web services.
  3. Finally, I would then read RESTful .NET.

If you gave yourself 2 days you could read, understand and digest all of these resources no problem.

You will probably end up using WCF - you can get the REST Starter Kit from here

Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • 1
    I can't stress this enough V important to understand rest before making a service implementation that you call rest but is just RPC over http. – albertjan Nov 29 '11 at 15:27
  • +1 - also read ReST in practice. Even if you don't want/need a full on HATEOAS service you should at least know your options. – Steven Robbins Dec 01 '11 at 08:17
1

You can pretty much use anything.

If that's truly your only requirement, it would be pretty easy to just use an ASP.NET 'Generic Handler', pull the information out of the request query parameters and write the JSON/XML to the response.

But if you expect things to get even a smidge more complex in the future, you'll want to use some sort of framework like 'Bas B' and 'iandotkelly' recommend.

Antoine Latter
  • 1,545
  • 10
  • 13
0

Use WCF Data Services. This supports XML and json (json is more efficient).

Use this with Entity Framework for the least amount of development time.

Bas
  • 26,772
  • 8
  • 53
  • 86
  • WCF Data Services - even the latest CTP version - is **dreadfully awful**. This was part of a recent project and we ended up ripping the whole mess out to replace it with a basic WCF service. – Yuck Nov 29 '11 at 15:10
  • @Yuck is it that bad? We've been considering it for a while but went with nancy. – albertjan Nov 29 '11 at 15:21
  • You can only do the simplest CRUD operations with it. LINQ doesn't (can't, probably because of what OData is) have translations for any aggregation functions (`Sum()`, `Count()`, etc) and getting others like `Contains()` to work requires some unintuitive coding. You also can't build queries in steps taking advantage of LINQ's deferred execution because it doesn't do sub-queries; it strictly enforces all filtering be completed before any `Select()` or `select` calls are made. – Yuck Nov 29 '11 at 15:30
0

WCF Data Services will help you here.

Check out This beginners guide to WCF

Matt
  • 3,664
  • 3
  • 33
  • 39
  • See my comment to @Bas B. The time WCF Data Services claims to save you will only handcuff and limit you in the end. – Yuck Nov 29 '11 at 15:10
  • @"Yuck - thanks for those comments, I'll be sure to investigate it a bit – Matt Dec 02 '11 at 15:32