WCF入门实例(ZT)

2019/7/7 1

WCF服务用于两个不同项目中的调用,在这里我举例项目A调用WCF服务实现查询数据功能。

第一步:创建数据库,有点数据能展示出来就行。

  1. Create database SalesLibrary --创建库

  2. Create table SalesVolume --创建表
  3. (
  4. Id int,
  5. Num int,
  6. Product varchar(20)
  7. )

第二步:创建存储过程(可以没有此步,只是方便查询)。

  1. create proc proc_ShowSalesVolume
  2. as
  3. select * from dbo.SalesVolume

第三步:创建一个WCF解决方案。

删除掉默认的Iservice1和Service1,创建自己的WCF服务名称。

第四步:编写WCF服务。

  在IsalesVolumeOperation接口中写一个现实数据的方法ShowSalesVolume,一定要写上[OperationContract],如若不写外界无法对其进行调用。

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Data;

  8. namespace WCFServices
  9. {
  10. // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ISalesVolumeOperation”。
  11. [ServiceContract] // 服务合同 即提供服务的接口或类
  12. public interface ISalesVolumeOperation
  13. {

  14. [OperationContract] //服务契约 即提供服务的实现方法
  15. DataTable ShowSalesVolume();
  16. }
  17. }
在salesVolumeOperation中完善查询的的过程以及需要返回的参数。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7. using System.Data;
  8. using System.Data.SqlClient;

  9. namespace WCFServices
  10. {
  11. // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“SalesVolumeOperation”。
  12. // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 SalesVolumeOperation.svc 或 SalesVolumeOperation.svc.cs,然后开始调试。
  13. public class SalesVolumeOperation : ISalesVolumeOperation
  14. {
  15. public DataTable ShowSalesVolume()
  16. {
  17. DataSet ds = new DataSet();
  18. SqlConnection con = new SqlConnection("data source=.;initial catalog=SalesLibrary;user id=sa;password=sa123");
  19. string sql = "proc_ShowSalesVolume"; //存储过程名称
  20. using (SqlCommand cmd = new SqlCommand(sql, con))
  21. {
  22. con.Open();
  23. cmd.CommandType = CommandType.StoredProcedure;

  24. SqlDataAdapter da = new SqlDataAdapter(cmd);
  25. da.Fill(ds);
  26. }
  27. return ds.Tables[0];
  28. }
  29. }
  30. }
第五步:对WCF服务接口测试看看是否无误。

  选中SalesVolumeOperation.svc右键在浏览器中查看,然后复制其路径。

  打开测试工具SoapUI,将路径复制到initial WSDL 然后在路径结尾写上?wsdl。

  接着开始进行测试:

  看来WCF服务没有出现问题,那么我们就开始创建第二个程序来访问这个WCF服务。

第六步:创建ASP.NET Web 应用程序(和WCF不在同一个解决方案)。

  选择空版本就行,然后右键服务-->添加服务引用-->高级-->添加web引用:

  然后在解决方案中就可以看到:

第七步:实现调用WCF服务。

  新建一个页面用于展示数据,名为ShowData.aspx

前台代码:

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <title></title>
  5. </head>
  6. <body>
  7. <form id="form1" runat="server">
  8. <div>
  9. <asp:Repeater ID="Repeater1" runat="server">
  10. <ItemTemplate>
  11. <%#Eval("Id") %>
  12. <%#Eval("Num") %>
  13. <%#Eval("Product") %>
  14. <br />
  15. </ItemTemplate>
  16. </asp:Repeater>
  17. </div>
  18. </form>
  19. </body>
  20. </html>
后台代码:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;

  8. namespace WebUI
  9. {
  10. public partial class ShowData : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if(!IsPostBack)
  15. {
  16. GetSalesVolume();
  17. }
  18. }
  19. private void GetSalesVolume()
  20. {
  21. SalesVolumeOperation.SalesVolumeOperation sa = new SalesVolumeOperation.SalesVolumeOperation(); //实例化WCF接口
  22. DataTable dt = sa.ShowSalesVolume(); //接口下的方法
  23. List<SalesVolume> list = new List<SalesVolume>();
  24. SalesVolume sv;
  25. foreach(DataRow dr in dt.Rows)
  26. {
  27. sv = new SalesVolume();
  28. sv.Id = Convert.ToInt32(dr["Id"]);
  29. sv.Num = Convert.ToInt32(dr["Num"]);
  30. sv.Product = dr["Product"].ToString();
  31. list.Add(sv);
  32. }
  33. Repeater1.DataSource = list;
  34. Repeater1.DataBind();

  35. }
  36. }
  37. public class SalesVolume
  38. {
  39. public int Id { get; set; }
  40. public int Num { get; set; }
  41. public string Product { get; set; }
  42. }
  43. }