Client和Host均为Asp.net的WCF服务系统

来源:互联网 发布:光纤 网络销售 编辑:程序博客网 时间:2024/06/09 17:07

 Client和Host均为Asp.net的WCF服务系统

  本文中WCF服务由IIS托管,且客户端也是asp.net应用。其实,这只不过是WCF的一种特殊的实现方式,即Client和Host都是Asp.net。

  和其它WCF系统一样,它包括三个部分:服务(Service)、主机(Host)和客户端(Client)。

  一、服务和主机

  1、在VS2008中,新建一个Asp.net网站:WCFserver。

创建WCF服务网站

  2、在WCFserver工程中添加新的WCF服务:ServiceWCF,其实也可以直接创建WCF服务网站。

添加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托管

二、客户端 

 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;    }}
 

 运行结果

 

 

文中代码下载地址:http://download.csdn.net/source/540466

原创粉丝点击