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

 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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值