如何在windows 8 metro app上连接wcf

原文:http://debugmode.net/2011/12/23/consuming-json-in-windows-8-metro-style-application-using-winjs/


Consuming JSON in Windows 8 Metro style Application using WinJS

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

01 <!DOCTYPE html>
02 <html>
03 <head>
04 <meta charset="utf-8" />
05 <title>HelloWorldJavaScript</title>
06 <!-- WinJS references -->
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>
10 <!-- HelloWorldJavaScript references -->
11 <link rel="stylesheet" href="/css/default.css" />
12 <script src="/js/default.js"></script>
13 </head>
14 <body>
15 <label >First Number</label>
16 <input id="TextboxFirstNumber" type="text" />
17 <br/>
18 <label >Second Number</label>
19 <input id="TextboxSecondNumber" type="text" />
20 <br />
21 <button id="ButtonAdd">+</button>
22 <br />
23 <span id="SpanResult" />
24 </body>
25 </html>

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

In above snippet

  1. url is URL of REST Service
  2. Using JSON.parse we are parsing returned text
  3. ResultSpan is span in which result is being displayed.

Putting all code together as below,

default.js

01 (function () {
02 'use strict';
03  
04  
05 WinJS.Application.onmainwindowactivated = function (e) {
06 if (e.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
07  
08  
09  
10 Calculator();
11  
12 }
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;
24 });
25  
26  
27 });
28  
29 }

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.

01 [ServiceContract]
02 public interface IService2
03 {
04  
05 [OperationContract]
06 [WebGet(UriTemplate="/Add/{Number1}/{Number2}",RequestFormat=WebMessageFormat.Json,
07 ResponseFormat = WebMessageFormat.Json
08 )]
09 int Add(string  Number1, string Number2);
10  
11  
12 }

Service is implemented as below,

01 using System;
02  
03 namespace MultipleBindingWCF
04 {
05  
06 public class Service1 : IService2
07 {
08  
09  
10 public int Add(string Number1, string Number2)
11 {
12 int num1 = Convert.ToInt32(Number1);
13 int num2 = Convert.ToInt32(Number2);
14 return num1 + num2;
15 }
16  
17  
18 }
19 }

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.

01 <?xml version="1.0"?>
02 <configuration>
03 <system.web>
04 <compilation debug="true" targetFramework="4.0" />
05 </system.web>
06 <system.serviceModel>
07 <behaviors>
08 <serviceBehaviors>
09 <behavior name ="servicebehavior">
10 <serviceMetadata httpGetEnabled="true"/>
11 <serviceDebug includeExceptionDetailInFaults="false"/>
12 </behavior>
13 </serviceBehaviors>
14 <endpointBehaviors>
15 <behavior name="restbehavior">
16 <webHttp/>
17 </behavior>
18 </endpointBehaviors>
19 </behaviors>
20 <services>
21 <endpoint name ="RESTEndPoint"
22 contract ="MultipleBindingWCF.IService2"
23 binding ="webHttpBinding"
24 address ="rest"
25 behaviorConfiguration ="restbehavior"/>
26 </service>
27 </services>
28 </system.serviceModel>
29 <system.webServer>
30 <modules runAllManagedModulesForAllRequests="true"/>
31 </system.webServer>
32 </configuration>

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.




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值