最近看到網上招聘有許多都需要WCF技術的人員,我之前一直沒接觸過這個東西,以后工作中難免會遇到,所謂笨鳥先飛,於是我就一探究竟,便有了這邊文章。由於是初學WCF沒有深入研究其原理,只是寫了一個demo留着以后,如果哪里寫的不對希望大佬們能指出批評。個人認為WCF類似於Web Services(類似,肯定是有區別的,至於啥區別可以搜搜資料),不多說了,接下來看我簡單實現的demo吧。
WCF服務用於兩個不同項目中的調用,在這里我舉例項目A調用WCF服務實現查詢數據功能。
第一步:創建數據庫,有點數據能展示出來就行。
Create database SalesLibrary --創建庫 Create table SalesVolume --創建表 ( Id int, Num int, Product varchar(20) )
第二步:創建存儲過程(可以沒有此步,只是方便查詢)。
create proc proc_ShowSalesVolume as select * from dbo.SalesVolume
第三步:創建一個WCF解決方案。
刪除掉默認的Iservice1和Service1,創建自己的WCF服務名稱。
第四步:編寫WCF服務。
在IsalesVolumeOperation接口中寫一個現實數據的方法ShowSalesVolume,一定要寫上[OperationContract],如若不寫外界無法對其進行調用。
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Data; namespace WCFServices { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼和配置文件中的接口名“ISalesVolumeOperation”。 [ServiceContract] // 服務合同 即提供服務的接口或類 public interface ISalesVolumeOperation { [OperationContract] //服務契約 即提供服務的實現方法 DataTable ShowSalesVolume(); } }
在salesVolumeOperation中完善查詢的的過程以及需要返回的參數。
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; using System.Data; using System.Data.SqlClient; namespace WCFServices { // 注意: 使用“重構”菜單上的“重命名”命令,可以同時更改代碼、svc 和配置文件中的類名“SalesVolumeOperation”。 // 注意: 為了啟動 WCF 測試客戶端以測試此服務,請在解決方案資源管理器中選擇 SalesVolumeOperation.svc 或 SalesVolumeOperation.svc.cs,然后開始調試。 public class SalesVolumeOperation : ISalesVolumeOperation { public DataTable ShowSalesVolume() { DataSet ds = new DataSet(); SqlConnection con = new SqlConnection("data source=.;initial catalog=SalesLibrary;user id=sa;password=sa123"); string sql = "proc_ShowSalesVolume"; //存儲過程名稱 using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(ds); } return ds.Tables[0]; } } }
第五步:對WCF服務接口測試看看是否無誤。
選中SalesVolumeOperation.svc右鍵在瀏覽器中查看,然后復制其路徑。
打開測試工具SoapUI,將路徑復制到initial WSDL 然后在路徑結尾寫上?wsdl。
接着開始進行測試:
看來WCF服務沒有出現問題,那么我們就開始創建第二個程序來訪問這個WCF服務。
第六步:創建ASP.NET Web 應用程序(和WCF不在同一個解決方案)。
選擇空版本就行,然后右鍵服務-->添加服務引用-->高級-->添加web引用:
然后在解決方案中就可以看到:
第七步:實現調用WCF服務。
新建一個頁面用於展示數據,名為ShowData.aspx
前台代碼:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <%#Eval("Id") %> <%#Eval("Num") %> <%#Eval("Product") %> <br /> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>
后台代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace WebUI { public partial class ShowData : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { GetSalesVolume(); } } private void GetSalesVolume() { SalesVolumeOperation.SalesVolumeOperation sa = new SalesVolumeOperation.SalesVolumeOperation(); //實例化WCF接口 DataTable dt = sa.ShowSalesVolume(); //接口下的方法 List<SalesVolume> list = new List<SalesVolume>(); SalesVolume sv; foreach(DataRow dr in dt.Rows) { sv = new SalesVolume(); sv.Id = Convert.ToInt32(dr["Id"]); sv.Num = Convert.ToInt32(dr["Num"]); sv.Product = dr["Product"].ToString(); list.Add(sv); } Repeater1.DataSource = list; Repeater1.DataBind(); } } public class SalesVolume { public int Id { get; set; } public int Num { get; set; } public string Product { get; set; } } }
最后頁面上的展示結果:
好了,到這里就完事兒了,哪里寫的不對希望大家指正~