Home » .NET » WCF Rest Services for Windows Phone

About Punit Ganshani

Punit Ganshani

WCF Rest Services for Windows Phone

So great, to create WCF Rest services for Windows Phone, you have to follow just 5 steps. This post will have more code than words, making it neat and to-the-point

Step 1: Create a WCF Service

Define the interface IMyService in ‘Services’ folder of YourWebsite

namespace YourWebsite.Services
{
[ServiceContract(Namespace = "http://YourWebsite.com/services", Name = "MyService")]
public interface IMyService
{
[OperationContract(Name="DoWork")]
[WebGet(UriTemplate = "?nm={name}",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
[FaultContract(typeof(ServiceError))]
bool DoWork(string name);
}
}

Add a service MyService.svc that inherits from IMyService

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public bool DoWork(string name)
{
return true;
}
}

Step 2: Modify the markup of MyService.svc

The markup of the MyService.svc should have Factory=”System.ServiceModel.Activation.WebServiceHostFactory”

<%@ ServiceHost Language="C#" Debug="true" Service="YourWebsite.Services.MyService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="MyService.svc.cs" %>

Step 3: Modify Web.Config of the Hosting Website

Add REST behavior (webHttp) to web.config

<system.serviceModel>
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://www.YourWebsite.com/Services"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<client>
<endpoint address="MyService.svc" binding="webHttpBinding" contract="YourWebsite.Services.IMyService" behaviorConfiguration="restBehavior" />
</client>

</system.serviceModel>

Step 4: Calling MyService in Windows Phone We don’t need to create any proxy class, and a simple WebClient can do the proxy stuff for us!

string url = string.Format("http://yourwebsite.com/services/MyService.svc?nm={0}", "My Name");
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
proxy.OpenReadAsync(new Uri(url, UriKind.Absolute));

Once the WCF service is invoked, proxy_OpenReadCompleted is called.

private void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
// do something
}

DataContractSerializer serializer = new DataContractSerializer(typeof(bool));
bool returnValue = (bool)serializer.ReadObject(e.Result);

}

Step 5: Execute And you are done! Just execute your Windows Phone application and you will have the RESTed output :)

Reference: WCF Rest Services for Windows Phone from our NCG partner Punit Ganshani at the Punit Ganshani blog.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a sysadmin Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Introduction to NGINX
2. Apache HTTP Server Cookbook
3. VirtualBox Essentials
4. Nagios Monitoring Cookbook
5. Linux BASH Programming Cookbook
6. Postgresql Database Tutorial
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments