ASP.NET Ajax調(diào)用WCF服務(wù)正確實現(xiàn)方法淺談
我們今天主要為大家介紹的就是有關(guān)ASP.NET Ajax調(diào)用WCF服務(wù)的具體實現(xiàn)方法,主要的應(yīng)用開發(fā)環(huán)境是:.NET Framework 3.5 Beta 2+Visual Studio 2005。那么接下來就讓我們一起來看一下相關(guān)的操作步驟吧。 #t#
準(zhǔn)備:
1、安裝.NET Framework 3.5 Beta 2。
ASP.NET Ajax調(diào)用WCF服務(wù)需要.NET Framework 3.5 Beta 2中的System.Web.Extensions.dll(3.5.0.0),System.ServiceModel.Web.dll支持。
開始我安裝的是.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的映射,如果沒有,建立映射,建立時取消“檢查文件是否存在”的選擇。
開始:
1、在VS 2005中新建一個Web Site項目。
添加web.config,將改為。
2、在該項目中添加一個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);
- }
- }
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,所以要手動加上。
如果不通過Ajax調(diào)用WCF,需要設(shè)置為:Factory="System.ServiceModel.Web.WebServiceHostFactory"。
5、開始***次運行,訪問http://localhost/AjaxWCFDemo/CNBlogsWCFService.svc,會出現(xiàn)如下頁面:
6、繼續(xù)運行,訪問http://localhost/AjaxWCFDemo/CNBlogsWCFService.svc/js,你會看到自動生成訪問WCF的客戶端代理腳本。
7、OK!服務(wù)器端的WCF已經(jīng)準(zhǔn)備好了,下面就開始客戶端的訪問。
8、配置ASP.NET Ajax調(diào)用WCF服務(wù),在web.config中進行設(shè)置:
- < ?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>
注意:要設(shè)置為3.5版本的System.Web.Extensions,如果使用asp.net ajax 1.0會得不到ASP.NET Ajax調(diào)用WCF服務(wù)返回的結(jié)果。
9、修改default.aspx的代碼:
1)添加ScriptManager,將ServiceReference設(shè)置為:~/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)添加調(diào)用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"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">- < html xmlns="http://www.w3.org/1999/xhtml" >
- < head runat="server">
- < title>Ajax WCF 演示 < /title>
- < /head>
- < body>
- < form id="form1" runat="server">
- < div align="center" style="margin-top:50px">
- < asp:ScriptManager ID="ScriptManager1" runat="server">
- < Services>
- < asp:ServiceReference Path="~/CNBlogsWCFService.svc" />
- < /Services>
- < /asp:ScriptManager>
- < a href="#" onclick="AddToFavorites('1','2')">收藏< /a>< br />
- < br />
- < span style="color:Red" id="Msg">< /span>
- < script type="text/javascript">
- function AddToFavorites(blogID,postID)
- {
- var wcf = new www.cnblog.com.ICNBlogsWCFService();
- wcf.AddToFavorites(blogID,postID,OnSucceeded);
- }
- function OnSucceeded(result)
- {
- document.getElementById("Msg").innerHTML = result;
- }
- < /script>
- < /div>
- < /form>
- < /body>
- < /html>
10、一切就緒,體驗一下ASP.NET Ajax調(diào)用WCF服務(wù)的快樂!