在這裡,WCF實現Ajax相同的效果。使用的是啟動了Ajax的WCF服務,即在頁面訪問的時候不會重新整理頁面。本執行個體是為了方便WCF和Ajax的比較,對WCF服務和Ajax的原理沒有進行講解,只是簡單的使用執行個體代碼而已。
今天寫WCF服務代碼的時候突然短路了,寫這篇也為了以後方便查看樣本,快速熟悉。
一、Ajax的源碼
1、建立一個Handler或者aspx頁面,這裡建立一個Handler頁面:NowTimeHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace MyWeb
{
/// <summary>
/// $codebehindclassname$ 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class NowTimeHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string nowtime = DateTime.Now.ToString("yyyy年MM月dd日hh時mm分ss秒fff毫秒");
context.Response.Write(nowtime);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
2、訪問頁面:Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyWeb._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">4
function getTime() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "NowTimeHandler.ashx?date=" + new Date().getSeconds(), false);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
document.getElementById("lblTime").innerHTML = xmlhttp.responseText;
}
}
}
xmlhttp.send();
}
setInterval("getTime()", 1);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label id="lblTime"></label>
</div>
</form>
</body>
</html>
二、WCF實現相同的Ajax功能
1、建立一個啟動了Ajax服務的WCF服務項:ServiceNowTime.svc
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace MyWeb.MyWCF
{
[ServiceContract(Namespace = "tnj")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceNowTime
{
// 添加 [WebGet] 屬性以使用 HTTP GET
[OperationContract]
public void DoWork()
{
// 在此處添加操作實現
return;
}
[OperationContract]
public string GetNowTime()
{
//string nowtime = DateTime.Now.ToString();
string nowtime = DateTime.Now.ToString("yyyy年MM月dd日hh時mm分ss秒fffff毫秒");
return nowtime;
}
// 在此處添加更多操作並使用 [OperationContract] 標記它們
}
[DataContract]
public class Person
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Address { get; set; }
}
}
2、建立訪問WCF服務的頁面:WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyWeb.MyWCF.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function getNowTime() {
tnj.ServiceNowTime.GetNowTime(function(data) {
document.getElementById("lblNowTime").innerHTML = data;
}, function(data) {
alert(data.get_message());
});
window.setTimeout("getNowTime()", 1);
}
</script>
</head>
<body onload="getNowTime()">
<form id="form1" runat="server">
<asp:ScriptManager ID="sm1" runat="server">
<Services>
<asp:ServiceReference Path="~/MyWCF/ServiceNowTime.svc" />
</Services>
</asp:ScriptManager>
<div>
<label id="lblNowTime"></label>
</div>
</form>
</body>
</html>