Subscribe by Email

Your email:

Contact Us

AVAI Mobile Solutions Blog

Current Articles | RSS Feed RSS Feed

Creating an iPhone friendly web service in .NET with WCF

  | Share on Twitter Twitter | Share on Facebook Facebook | Submit to Digg digg it |  Add to delicious  delicious |  Submit to StumbleUpon StumbleUpon |  Share on LinkedIn LinkedIn |  Share On Technorati Technorati | Submit to Reddit reddit 

With the introduction of Windows Communication Foundation in the .NET Framework 3.0, the responsiblity of building your own web service interface protocol has been lifted from the developer. DataContracts now provide a type-specific utility for easily sending and receiving data in either XML or Javascript Object Notation (JSON).  In our iPhone Apps, we prefer to use JSON since it is both less verbose than XML (meaning less data to transfer on cellular networks) and easier to parse on the iPhone side.

Setting up a JSON WCF service requires only a few simple steps. 

First, create a new WCF Service Library Project in your Visual Studio solution and name it WcfServiceLibrary1.  For demonstration purposes, we'll modify the sample code generated by Visual Studio. 

There are main parts to the code that is generated:

  1. The service interface definition
  2. The service implementation
  3. The data object definitions

The IService1 interface contains all of the good stuff about WCF service.  All service methods are decorated with the [OperationContract] attribute.

The Service1 class contains the business logic implementation of the IService1 interface.

The CompositeType class is the custom class returned by one of the IService1 service methods.  This class uses the DataContractAttribute we mentioned before.  All properties decorated with [DataMember] will be serialized (including properties marked private, so be careful) according to the serialization format you specify.

How do you specify the serialization format?  Well that's the interesting part.

Let's create a new service method in IService1 like so:

[OperationContract]
CompositeType GetTestData();

Now add the attribute that specifies the output format that we want:

[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "test/random"
)]
CompositeType GetTestData();

This requires that a reference be added to the project to System.ServiceModel.Web and add a using statement for System.ServiceModel.Web.  You can read more about these attributes here, but this combination of attribute values will return our data in JSON format with no wrapper.

Next, implement the GetTestData method in Service1 like this:

public CompositeType GetTestData()
{
return new CompositeType();
}

Now we can host the service library we've created in a standard ASP.NET web project.   So create a new ASP.NET Web Application.  Now add a reference to the WcfServiceLibrary1 project that we just created.  Then create a new WCF service named Service1.svc.  Right click on Service1.svc and choose "View Markup".  Now we can simply configure the service to create a new instance of the Service1 class using the WebServiceHostFactory.  Below is all the markup that needs to be pasted in this file:

<%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary1.Service1" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Now it's time to see it all in motion.  Set the web application as the startup project and run it.  Append Service1.svc/test/random to the path in the address bar (e.g. http://localhost:52265/Service1.svc/test/random), press enter, and take a look at the sweet JSON file that comes out, just the way the iPhone likes it.

Download the full sample source code here.

Another hot tip:  Download and install the JSONView add in for Firefox to see your JSON in the browser.

 -Jason

Tags: ,

Comments

There are some nice beginners screencasts that take you through the basics on MSDN as well: http://msdn.microsoft.com/en-us/netframework/dd939784.aspx
Posted @ Tuesday, October 20, 2009 12:05 AM by Joss
Thanks for nice article. 
I followed these instructions, but <%@ ServiceHost Language="C#" Debug="true" Service="WcfServiceLibrary1.Service1" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>  
 
it gives me erros, saying Factory is not an attribute of ServieHost. 
 
Can you please help me out, I will be very thankful.Thanks
Posted @ Friday, November 20, 2009 5:37 PM by Mohamad
@Mohamad, do you have VS2008 SP1 installed? 
Here seems to be a similar problem.
Posted @ Friday, November 20, 2009 6:12 PM by AVAI Mobile Developer
Yeah I did install sp1, but still not working.
Posted @ Saturday, November 21, 2009 12:24 PM by Mohamad
Has anyone implemented this using certificate based security on the WCF side?
Posted @ Tuesday, November 24, 2009 9:33 AM by Javier
The author of this article should re-write it. It is not well done and so far I do not think anyone has benefitted from it... it does not work, but may for him. He should have included all his source code. You simply can't follow his code in his documentation here.
Posted @ Tuesday, April 20, 2010 7:23 AM by iPhoneMagician
@iPhoneMagician, thank you for your feedback.  
 
We've slightly revised the article for clarity. A necessary step to add a project reference of the WCF library to the web application was not explicitly mentioned and has been added.  
 
@Mohamad, perhaps this was actually your issue too, but Visual Studio was not giving you a very good error message?  
 
Also, the full source code has been uploaded <a ref="http://avaimobile.com/Portals/32035/blogcontent/wcfservicelibrary1.zip">here.  
 
Please let us know if you have any further issues.
Posted @ Wednesday, April 21, 2010 1:53 PM by AVAI Mobile Developer
Is this possible to do without hosting the svc on a asp.net environment, but rather create one also by yourself? 
 
In other words, is it possible to combine the work you've done here with this? http://idunno.org/archive/2008/05/11/web-services---we-dont-need-no-web-server.aspx 
 
In that case, you can easily create an executable file, that creates the server using ServiceHost, creates the service and simply query it from the iphone, without having to worry if IIS or Cassini is installed on the server machine. 
 
Also a tiny iphone sample would be great. 
 
Cheers
Posted @ Saturday, June 19, 2010 1:38 PM by Kostas
Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics