WCF服務系統主要組成部分詳解
WCF開發工具的應用,在實際程序開發中起到了非常大的作用,能為編程人員輕松的創建一個企業級的安全性極高的互聯應用解決方案。在這里,WCF服務由IIS托管,且客戶端也是asp.net應用。其實,這只不過是WCF服務系統的一種特殊的實現方式,即Client和Host都是Asp.net。#t#
和其它WCF系統一樣,它包括三個部分:服務(Service)、主機(Host)和客戶端(Client)。
一、WCF服務系統之服務和主機
1、在VS2008中,新建一個Asp.net網站:WCFserver。
2、在WCFserver工程中添加新的WCF服務:ServiceWCF,其實也可以直接創建WCF服務網站。
3、 在ServiceWCF中添加一個操作:OnHello()用以返回"Hello World!!”
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.Text;
- public class ServiceWCF : IServiceWCF
- {
- public string OnHello()
- {
- return "Hello World!!";
- }
- }
4、發布網站
在IIS中創建相應的虛擬目錄,發布網站,即把WCF服務交由IIS托管
二、WCF服務系統之客戶端
1、創建一個Asp.net網站:WEBAJAX,在default.aspx中添加一個Textbox和一個Button控件。
2、在工程WEBAJAX中添加“服務引用”。
3、創建客戶端代理,調用WCF服務的操作。
- using System;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- ServiceReference1.ServiceWCFClient client =
new ServiceReference1.ServiceWCFClient();- string Msg = client.OnHello();
- this.TextBox1.Text = Msg;
- }
- }
以上就是針對WCF服務系統做的詳細介紹。