原創(chuàng)|其它|編輯:郝浩|2009-08-28 14:32:56.000|閱讀 1028 次
概述:在微軟應(yīng)用開(kāi)發(fā)平臺(tái)上WCF (Windows Communication Foundation)增加了很多新功能,特別是在應(yīng)用程序中實(shí)現(xiàn)如何相互溝通。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
在微軟應(yīng)用開(kāi)發(fā)平臺(tái)上WCF (Windows Communication Foundation)增加了很多新功能,特別是在應(yīng)用程序中實(shí)現(xiàn)如何相互溝通。
在本文中,我們將會(huì)看到WCF如何用于直接從客戶端調(diào)用JavaScript代碼。這是一個(gè)提供非常棒的ASP.NET AJAX的功能。在本文中,我們不關(guān)注WCF內(nèi)部機(jī)制,而我們將重點(diǎn)放在如何直接從Javascript調(diào)用一個(gè)服務(wù)。因此,不如就如何ASP.NET或的.NET運(yùn)行庫(kù)管理這個(gè)功能。
為了說(shuō)明這個(gè)想法正確性,我們創(chuàng)建一個(gè)解決方案。我們將看到兩個(gè)辦法來(lái)增加WCF的服務(wù),可以從JavaScript的調(diào)用。使用AJAX并啟用服務(wù)項(xiàng)目模板(Ajax-Enable WCF Service),使用服務(wù)接口(Service Interface)定義一個(gè)類庫(kù)。
如,下圖所示ToDoUpdateToDo時(shí)序圖:
在這里,我們將看到一個(gè)非常簡(jiǎn)單的方式使用JavaScript中的WCF服務(wù)。打開(kāi)vs2008sp1,右鍵單擊Web應(yīng)用程序項(xiàng)目,然后選擇添加新項(xiàng)。
選擇基于AJAX的WCF服務(wù)項(xiàng)目模板,并將其命名為“HelloWorldService.svc ”并單擊確定。該向?qū)⑻砑親elloWorldService.svc文件名的解決方案。該文件也有一個(gè)代碼文件。建立文件的過(guò)程我就不詳細(xì)說(shuō)了。如果您在一個(gè)XML文件編輯器打開(kāi)HelloWorldService.svc,
您將看到一個(gè)標(biāo)記,如下所示:
<%@ ServiceHost Language="C#" Debug="true"
Service="WebUI.HelloWorldService" CodeBehind="HelloWorldService.svc.cs" %>
如果你打開(kāi)代碼文件,你將會(huì)看到代碼如下所示:
namespace WebUI
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorldService
{
[OperationContract]
public void DoWork()
{
return;
}
}
}
Microsoft Visual Studio 2008會(huì)自動(dòng)的在Web.config文件中添加必要的配置。現(xiàn)在,先添加一個(gè)HelloWorld()方法返回一個(gè)字符串“HelloWorld ”,并添加一個(gè)[OperationContract]屬性的方法。現(xiàn)在,添加頁(yè)面的Web應(yīng)用程序項(xiàng)目,并將其命名為“HelloWorldTest.aspx ”。拖放腳本管理項(xiàng)目從Visual Studio工具箱。在ScriptManager標(biāo)記,增加服務(wù)范圍的服務(wù)。
實(shí)例代碼如下:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/HelloWorldService.svc" />
</Services>
</asp:ScriptManager>
現(xiàn)在,添加一個(gè)按鈕和一個(gè)TextBox頁(yè)面,并在按鈕Click事件中,使用JavaScript函數(shù)調(diào)用服務(wù)。當(dāng)你寫(xiě)的服務(wù)調(diào)用功能時(shí),在Visual Studio2008年HTML編輯器將提供智能寫(xiě)必要的函數(shù)調(diào)用。完整的HTML代碼如下:
<form id="form1" runat="server">
<div>
<script language="javascript" type="text/javascript">
function GetValueFromServer() {
HelloWorldService.HelloWorld(onSuccess, onFailure);
}
function onSuccess(result) {
document.getElementById('txtValueContainer').value = result;
}
function onFailure(result) {
window.alert(result);
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/HelloWorldService.svc" />
</Services>
</asp:ScriptManager>
<input id="btnServiceCaller" type="button" value="Get Value"
onclick="GetValueFromServer()"; />
<input id="txtValueContainer" type="text" value="" />
</div>
</form>
請(qǐng)注意,當(dāng)調(diào)用服務(wù)時(shí),我們可以通過(guò)兩種方法:一個(gè)是回調(diào)和其他的錯(cuò)誤回調(diào)。如果我們需要通過(guò)任何參數(shù)的函數(shù),參數(shù)將首先然后回調(diào)。
因此,如果我們有一個(gè)函數(shù)命名getvalue其中兩個(gè)字符串參數(shù),我們要調(diào)用的功能[NameSpaceName].[ServiceName].getvalue(“value one”,”value two”,on_success,on_error); 分別的在on_sucess和on_error的回調(diào)和錯(cuò)誤回調(diào)。下圖所示,類圖如下:
使用服務(wù)接口中定義的類庫(kù)
所以,我們看到如何使用AJAX-enabled WCF Service的項(xiàng)目模板。現(xiàn)在,我們也將看到我們?nèi)绾尾拍苁惯@項(xiàng)服務(wù)為ASP.NET AJAX的。當(dāng)我們創(chuàng)建類庫(kù)項(xiàng)目,默認(rèn)情況下,這不是說(shuō)的服務(wù)模式和運(yùn)行時(shí)支持是必要的運(yùn)行WCF序列化。因此,我們必須增加必要的服務(wù)參考。先右鍵單擊類庫(kù)項(xiàng)目,并選擇添加引用,然后選擇這些引用:
System.Runtime.Serialization
System.ServiceModel
在這一階段,我們將使用一個(gè)TODO管理為例,證明了整個(gè)想法。增加服務(wù)的數(shù)據(jù)庫(kù),然后創(chuàng)建一個(gè)TODO表ID,Description和Status字段。
現(xiàn)在,添加一個(gè)LINQ到SQL類文件從項(xiàng)目模板。拖放的TODO表從數(shù)據(jù)庫(kù)的LINQ到SQL類文件設(shè)計(jì)。現(xiàn)在,請(qǐng)點(diǎn)擊設(shè)計(jì)器到屬性窗口中,改變單向序列模式。現(xiàn)在,我們的設(shè)計(jì)器生成的LINQ到SQL類是準(zhǔn)備用于WCF。如果您想使用自定義用戶定義的類型,您必須設(shè)置[DataContract]層次屬性到您的類,還必須添加[DataMember]特殊屬性。 現(xiàn)在,我們要添加這樣的一個(gè)服務(wù)接口:
namespace ServiceLibrary
{
[ServiceContract(Namespace = "ServiceLibrary")]
interface IToDoService
{
[OperationContract]
ToDo GetToDo(long ID);
[OperationContract]
ToDo AddToDo(ToDo toDo);
[OperationContract]
bool DeleteToDo(ToDo todo);
[OperationContract]
ToDo UpdateToDo(ToDo todo);
[OperationContract]
List GetAllToDo();
}
}
請(qǐng)注意,我們已經(jīng)提到了一個(gè)名字空間內(nèi)ServiceContract接口屬性。這是非常重要的。我們將使用這個(gè)名稱的服務(wù)名稱內(nèi)的JavaScript代碼來(lái)訪問(wèn)服務(wù)。現(xiàn)在,我們要執(zhí)行此服務(wù)界面;代碼如下。請(qǐng)注意,在代碼中,我使用了[AspNetCompatibilityRequirements( RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed ) ]類屬性,這是必須要求顯示服務(wù)作為一個(gè)ASP.NET AJAX技術(shù)調(diào)用WCF Service。
namespace ServiceLibrary
{
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class ToDoService : IToDoService
{
#region IToDoService Members
public ToDo GetToDo(long ID)
{
DataClasses1DataContext context = new DataClasses1DataContext();
var toDos = from p in context.ToDos
where p.ID == ID
select p;
List listTodos = toDos.ToList();
if (listTodos != null && listTodos.Count > 0)
{
return listTodos[0];
}
else
{
return null;
}
}
#endregion
}
}
配置Web應(yīng)用程序使用的TODO服務(wù)
現(xiàn)在我們已經(jīng)確定的所有必要的東西,來(lái)運(yùn)行TODO的應(yīng)用程序,現(xiàn)在是時(shí)候解開(kāi)服務(wù)到客戶端作為一個(gè)ASP.NET AJAX技術(shù)調(diào)用WCF Service。
為此,我們要添加一個(gè)基于AJAX的WCF Service的.SVC文件,不使用它的代碼文件。或者,我們可以添加一個(gè)XML文件或文本文件,然后將其重新命名為T(mén)oDoService.svc 。打開(kāi)一個(gè)XML編輯器,并新增了一條指令,如下所示:
<%@ ServiceHost Language="C#" Debug="true" Service="ServiceLibrary.ToDoService" %>
現(xiàn)在,我們得進(jìn)行必要的配置,以運(yùn)行此服務(wù)的Web.config中。代碼如下:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AspNetAjaxBehavior">
<enableWebScript />
</behavior>
<behavior name="WebUI.HelloWorldServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="ServiceLibrary.ToDoService">
<endpoint behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding"
contract="ServiceLibrary.IToDoService" />
</service>
<service name="WebUI.HelloWorldService">
<endpoint address="" behaviorConfiguration="WebUI.HelloWorldServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="WebUI.HelloWorldService" />
</service>
</services>
</system.serviceModel>
現(xiàn)在,右鍵點(diǎn)擊文件并選擇在瀏覽器中查看看到的服務(wù)啟動(dòng)和運(yùn)行良好。到下一個(gè)階段,您必須添加一個(gè)serviceHostingEnvironment并設(shè)置aspNetCompatibilityEnabled = “true”,以便能夠使用ASP.NET中的WCF service的功能,比如它的HTTP環(huán)境,Session等。
在JavaScript使用此服務(wù)
現(xiàn)在,使用該服務(wù)的HelloWorldService就像我們以前使用的。下面,我給出實(shí)例代碼在ScriptManager標(biāo)記中來(lái)解釋清楚。請(qǐng)注意,我們?cè)黾恿薱lientServiceHelper.js文件。我們已經(jīng)把所有的客戶端JavaScript函數(shù)到WCF在該文件中通信。
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Path="~/Script/ClientServiceHeler.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/ToDoService.svc" />
</Services>
</asp:ScriptManager>
我們使用了ASP.NET AJAX的客戶端模型,面向?qū)ο蟮腏avaScript客戶端編寫(xiě)代碼clientServiceHelper.js 。
Type.registerNamespace("ServiceClients");
ServiceClients.ToDoClient = function() {
}
ServiceClients.ToDoClient.prototype = {
AddToDo: function(todo, callback, errorCallBack) {
ServiceLibrary.IToDoService.AddToDo(todo, callback, errorCallBack);
},
DeleteToDo: function(todo, callback, errorCallBack) {
ServiceLibrary.IToDoService.DeleteToDo(todo, callback, errorCallBack);
},
UpdateToDo: function(todo, callback, errorCallBack) {
ServiceLibrary.IToDoService.UpdateToDo(todo, callback, errorCallBack);
},
GetAllToDo: function(callback, errorCallBack) {
ServiceLibrary.IToDoService.GetAllToDo(callback, errorCallBack);
},
dispose: function() {
}
}
ServiceClients.ToDoClient.registerClass('ServiceClients.ToDoClient', null, Sys.IDisposable)
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
總結(jié):
在本文中,我們看到了我們?nèi)绾文軌蚴褂没?于AJAX的WCF service項(xiàng)目模板。然后,我們看到我們?nèi)绾文軌蚴褂梅?wù)接口的WCF服務(wù)。我們還看到了如何配置在Web.config使用服務(wù)。最后,我們看到我們?nèi)绾?添加服務(wù)引用ScriptManager 。我們還可以添加一個(gè)服務(wù)中提及的C #代碼。
簡(jiǎn)單的代碼示例如下:
ScriptManager manager = ScriptManager.GetCurrent(Page);
ServiceReference reference = new ServiceReference("ToDoService.svc");
manager.Services.Add(reference);
Microsoft Visual Studio 2008 SP1 ,.NET Framework 3.5 SP1 ,ASP.NET AJAX ,IIS 7 or VS Integrated Web Server
[WCF and SVS file configured]
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:IT專家網(wǎng)