開發環境:Windows server 2008 Enterprise,Microsoft Visual Studio 2008 SP1,.NET Framework 3.5 SP1,Microsoft SQL Server 2008
開發架構: ASP.NET AJAX,WCF,ADO.NET Entity Framework
開發步驟:
1、建立一個空白解決方案:JXCSln;
2、添加一個類庫項目,名稱為:Jxc.DAL,刪除產生的Class1.cs,接著引用一下:System.Data.Entity,否則在建立資料庫連接時會出現錯誤,無法串連到資料庫。然後添加新項:ADO.NET Entity Date Model。名稱為:NorthwindDbModel.edmx;在彈出的實體模型嚮導的第一個視窗中,選擇“從資料庫產生”,然後下一步,資料庫連接設定如:
設定好後,單擊下一步,出現如所示視窗,這時,我們只選擇表:
最後單擊無成,產生的實體模型圖如下:
三、選擇Jxc.DAL類庫,產生一下。
四、添加一個新項目(類庫項目),名稱為:Jxc.BLL,刪除產生的Class1.cs檔案。添加引用,選擇項目,引用Jxc.DAL,引用System.Data.Entity。
五、添加一個新類,名稱:EmployeesInfo。
六、開啟EmployeesInfo.cs檔案,輸入如下代碼:
(需要 using Jxc.DAL;)
NorthwindEntities EmployeesContent = new NorthwindEntities();
public string GetEmployeeNameByID(int EmployeeID)
{
var query = EmployeesContent.Employees.First(p => p.EmployeeID == EmployeeID);
return query.LastName + query.FirstName;
}
public Employees[] GetAllEmployees()
{
var query = from emp in EmployeesContent.Employees select emp;
return query.ToArray();
}
(時間關係,待續...)
接著上面的
七、添加一個新項目:ASP.NET Web 應用程式,並命名為:Jxc.Web。
八、添加引用——>項目:Jxc.BLL,Jxc.DAL,System.Data.Entity。
九、添加新項,WCF服務,名稱:EmployeeService.svc; 刪除檔案IEmployeeService.cs,並將以下代碼粘貼到EmployeeService.svc.cs檔案中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Jxc.DAL;
using Jxc.BLL; //記得引用
namespace Jxc.Web
{
[ServiceContract(Namespace = "WcfService")]
public class EmployeeService
{
EmployeesInfo employeefobl = new EmployeesInfo();
int recordCount = 0;
[OperationContract]
public string GetEmployeeNameByID(int empid)
{
return employeefobl.GetEmployeeNameByID(empid);
}
[OperationContract]
public Employees[] GetAllEmployees()
{
return employeefobl.GetAllEmployees();
}
}
}
十、接著刪除Web.Config檔案中生氣的代碼:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Jxc.Web.EmployeeServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="Jxc.Web.EmployeeServiceBehavior"
name="Jxc.Web.EmployeeService">
<endpoint address="" binding="wsHttpBinding" contract="Jxc.Web.IEmployeeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
十一、選擇檔案EmployeeService.svc,右鍵——>查看標記:
將代碼::<%@ ServiceHost Language="C#" Debug="true" Service="Jxc.Web.EmployeeService" CodeBehind="EmployeeService.svc.cs" %>
改成如下代碼:<%@ ServiceHost Language="C#" Debug="true" Service="Jxc.Web.EmployeeService" CodeBehind="EmployeeService.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
十二、開啟Default.aspx視窗,加入Asp.net Ajax功能:ScriptManager代碼如下:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/EmployeeService.svc" />
</Services>
</asp:ScriptManager>
<br />
<h2>
ASP.NET AJAX+WCF+ADO.NET Entity 架構執行個體</h2>
<p>
輸入一個員工ID調用WCF並返回對應的名字!</p>
請輸入部門ID:<input id="txtdeptID" name="txtdeptID" type="text" />
<input id="btnTransfer" onclick="ClientGetEmployeeID()" type="button"
value="調用" /> <span id="Results"></span>
</form>
</body>
十三、編寫javascript代碼,如下:
<script type="text/javascript">
function ClientGetEmployeeID() {
var txtdeptID = document.getElementById("txtdeptID");
if (isNaN(txtdeptID.value)) {
alert('部門ID必須是數字!')
txtdeptID.focus();
return;
}
var proxy = new WcfService.EmployeeService();
proxy.GetEmployeeNameByID(txtdeptID.value, OnSucceeded, OnFailed, "");
}
function OnSucceeded(result) {
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = result;
}
function OnFailed(error) {
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = "調用失敗!";
}
</script>
十四、在Default.aspx.cs檔案using一下引用項目:
using Jxc.BLL;
using Jxc.DAL;
十五、程式運行如下:
至此,已經可以正常運行,測試通過。
請路過的朋友發表高見,當然也為像我一樣想學這些技術的朋友指引一下,不過,還得大家多發表意見,謝謝