本文的主要內容是通過(guò)ASP.NET Ajax調用WCF服務(wù)的代碼示例。開(kāi)發(fā)環(huán)境是:.NET Framework 3.5 Beta 2+Visual Studio 2005。
準備:
1、安裝.NET Framework 3.5 Beta 2。
ASP.NET Ajax調用WCF服務(wù)需要.NET Framework 3.5 Beta 2中的System.Web.Extensions.dll(3.5.0.0),System.ServiceModel.Web.dll支持。
開(kāi)始我安裝的是.NET Framework 3.5 June 2007 Community Technology Preview (CTP),走了一些彎路。
2、安裝Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF)。
3、檢查IIS是否有.svc到c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll的映射,如果沒(méi)有,建立映射,建立時(shí)取消“檢查文件是否存在”的選擇。
開(kāi)始:
1、在VS 2005中新建一個(gè)Web Site項目。
添加web.config,將改為。
2、在該項目中添加一個(gè)WCF Service,命名為CNBlogsWCFService.svc。
3、修改App_Code中CNBlogsWCFService.cs的代碼:
[ServiceContract(Namespace = "http://www.cnblog.com/")]
public interface ICNBlogsWCFService
{
[OperationContract]
string AddToFavorites(string blogID, string postID);
}
public class CNBlogsWCFService : ICNBlogsWCFService
{
public string AddToFavorites(string blogID, string postID)
{
return string.Format("收藏成功!BlogID:{0},PostID:{1}", blogID, postID);
}
}
如果想進(jìn)一步了解上述代碼,請參考:
Artech:[原創(chuàng )]我的WCF之旅(1):創(chuàng )建一個(gè)簡(jiǎn)單的WCF程序 Bruce Zhang:Windows Communication Foundation入門(mén)(Part Two)
4、修改CNBlogsWCFService.svc的代碼:
增加:
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory。
改為:
<%@ ServiceHost Language="C#" Debug="true" Service="CNBlogsWCFService" CodeBehind="~/App_Code/CNBlogsWCFService.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"%><%@ ServiceHost Language="C#" Debug="true" Service="CNBlogsWCFService" CodeBehind="~/App_Code/CNBlogsWCFService.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"%>
Factory是.NET Framework 3.5 Beta 2中增加的,而我們用的是Visual Studio 2005 extensions for .NET Framework 3.0,所以要手動(dòng)加上。
如果不通過(guò)Ajax調用WCF,需要設置為:Factory="System.ServiceModel.Web.WebServiceHostFactory"。
5、開(kāi)始第一次運行,訪(fǎng)問(wèn)http://localhost/AjaxWCFDemo/CNBlogsWCFService.svc,會(huì )出現如下頁(yè)面:
6、繼續運行,訪(fǎng)問(wèn)
http://localhost/AjaxWCFDemo/CNBlogsWCFService.svc/js,你會(huì )看到自動(dòng)生成訪(fǎng)問(wèn)WCF的客戶(hù)端代理腳本。
7、OK!服務(wù)器端的WCF已經(jīng)準備好了,下面就開(kāi)始客戶(hù)端的訪(fǎng)問(wèn)。
8、配置ASP.NET Ajax,在web.config中進(jìn)行設置:
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false">
<assemblies>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<authentication mode="Forms" />
<httpHandlers>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web>
</configuration>
注意:要設置為3.5版本的System.Web.Extensions,如果使用asp.net ajax 1.0會(huì )得不到調用WCF服務(wù)返回的結果。
9、修改default.aspx的代碼:
1)添加ScriptManager,將ServiceReference設置為:~/CNBlogsWCFService.svc。
2)將
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
改為:
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
Namespace="System.Web.UI" TagPrefix="asp" %>
2)添加調用WCF服務(wù)的代碼,完整代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "