In this post I will discuss way of consuming JSON service in Windows 8 metro style application. In this post,
- Metro app is created using HTML 5 and Service is being consumed using WinJS
- Service is created service using WCF and REST is on service using webHttpBinding.
Since creating simple REST Service returning JSON is not main focus of this post, so I will show creation WCF REST Service returning JSON in later part of this post.
Consuming JSON using WinJS
We need to consume service in HTML 5 Metro application. Very first let us design HTML page
default.html
04 | < meta charset = "utf-8" /> |
05 | < title >HelloWorldJavaScript</ title > |
07 | < link rel = "stylesheet" href = "/winjs/css/ui-dark.css" /> |
08 | < script src = "/winjs/js/base.js" ></ script > |
09 | < script src = "/winjs/js/wwaapp.js" ></ script > |
11 | < link rel = "stylesheet" href = "/css/default.css" /> |
12 | < script src = "/js/default.js" ></ script > |
15 | < label >First Number</ label > |
16 | < input id = "TextboxFirstNumber" type = "text" /> |
18 | < label >Second Number</ label > |
19 | < input id = "TextboxSecondNumber" type = "text" /> |
21 | < button id = "ButtonAdd" >+</ button > |
23 | < span id = "SpanResult" /> |
I have put a button and two text boxes. User will input number to be added in textboxes and on click event of the button service would get called.
In JS file to make call to REST Service returning JSON, we need to make call using WinJS.xhr.
![image image](http://dhananjay25.files.wordpress.com/2011/12/image_thumb22.png?w=494&h=137)
In above snippet
- url is URL of REST Service
- Using JSON.parse we are parsing returned text
- ResultSpan is span in which result is being displayed.
Putting all code together as below,
default.js
05 | WinJS.Application.onmainwindowactivated = function (e) { |
06 | if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) { |
13 | function Calculator() { |
14 | var Number1 = document.getElementById( 'TextboxFirstNumber' ); |
15 | var Number2 = document.getElementById( 'TextboxSecondNumber' ); |
16 | var ResultSpan = document.getElementById( 'SpanResult' ); |
17 | var ButtonToAdd = document.getElementById( 'ButtonAdd' ); |
18 | ButtonToAdd.addEventListener( 'click' , function () { |
20 | var url = baseURl+Number1.value+ "/" +Number2.value; |
21 | WinJS.xhr({ url: url }).then( function (r) { |
22 | var result = JSON.parse(r.responseText); |
23 | ResultSpan.innerHTML = result; |
Creating simple Add Service
To start with let us create a WCF REST Service returning JSON as below. There is one operation contact named Add in the service and that is taking two strings as input parameters and returning integer.
02 | public interface IService2 |
06 | [WebGet(UriTemplate= "/Add/{Number1}/{Number2}" ,RequestFormat=WebMessageFormat.Json, |
07 | ResponseFormat = WebMessageFormat.Json |
09 | int Add( string Number1, string Number2); |
Service is implemented as below,
03 | namespace MultipleBindingWCF |
06 | public class Service1 : IService2 |
10 | public int Add( string Number1, string Number2) |
12 | int num1 = Convert.ToInt32(Number1); |
13 | int num2 = Convert.ToInt32(Number2); |
Next in this section we need to configure service. We need to configure service webHttpBinding to eanble it as REST Service. So in Web.Config we need to do the below changes.
04 | < compilation debug = "true" targetFramework = "4.0" /> |
09 | < behavior name = "servicebehavior" > |
10 | < serviceMetadata httpGetEnabled = "true" /> |
11 | < serviceDebug includeExceptionDetailInFaults = "false" /> |
15 | < behavior name = "restbehavior" > |
21 | < endpoint name = "RESTEndPoint" |
22 | contract = "MultipleBindingWCF.IService2" |
23 | binding = "webHttpBinding" |
25 | behaviorConfiguration = "restbehavior" /> |
28 | </ system.serviceModel > |
30 | < modules runAllManagedModulesForAllRequests = "true" /> |
After configuring, Service is ready for hosting. After hosting consume service in Windows 8 Metro application.
I hope this post is useful. Thanks for reading.