化零为整WCF(1) - 不能免俗,我也从Hello开始

本文通过一个简单的 WCF 服务示例介绍了如何创建、部署及调用一个基于 Windows Communication Foundation 的服务。示例包括服务接口定义、实现类、宿主配置及客户端调用过程。
[索引页]
[源码下载]


化零为整WCF(1) - 不能免俗,我也从Hello开始


作者: webabcd


介绍
WCF(Windows Communication Foundation) - 废话不多说,俗也不能免,我也从Hello开始


示例
1、服务
IHello.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

using  System.ServiceModel;

namespace  WCF.ServiceLib.Sample
{
    
/// <summary>
    
/// IHello接口
    
/// </summary>

    [ServiceContract]
    
public interface IHello
    
{
        
/// <summary>
        
/// 打招呼方法
        
/// </summary>
        
/// <param name="name">人名</param>
        
/// <returns></returns>

        [OperationContract]
        
string SayHello(string name);
    }

}

Hello.cs
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

using  System.ServiceModel;

namespace  WCF.ServiceLib.Sample
{
    
/// <summary>
    
/// Hello类
    
/// </summary>

    public class Hello : IHello
    
{
        
/// <summary>
        
/// 打招呼方法
        
/// </summary>
        
/// <param name="name">人名</param>
        
/// <returns></returns>

        public string SayHello(string name)
        
{
            
return "Hello: " + name;
        }

    }

}


2、宿主
Hello.svc
<% @ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Sample.Hello"  %>

Web.config
<? xml version="1.0" ?>
< configuration >
  
< system .serviceModel >
    
< behaviors >
      
< serviceBehaviors >
        
< behavior  name ="SampleBehavior" >
          
<!-- httpGetEnabled - 使用get方式提供服务 -->
          
< serviceMetadata  httpGetEnabled ="true"   />
        
</ behavior >
      
</ serviceBehaviors >
    
</ behaviors >
    
< services >
      
<!-- name - 提供服务的类名 -->
      
<!-- behaviorConfiguration - 指定相关的行为配置 -->
      
< service  name ="WCF.ServiceLib.Sample.Hello"  behaviorConfiguration ="SampleBehavior" >
        
<!-- address - 服务地址 -->
        
<!-- binding - 通信方式 -->
        
<!-- contract - 服务契约 -->
        
< endpoint  address =""  binding ="basicHttpBinding"  contract ="WCF.ServiceLib.Sample.IHello"   />
      
</ service >
    
</ services >
  
</ system.serviceModel >
</ configuration >


3、客户端
Hello.aspx
<% @ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs"
    Inherits
="Sample_Hello" Title="不能免俗,我也从Hello开始" 
%>

< asp:Content  ID ="Content1"  ContentPlaceHolderID ="head"  runat ="Server" >
</ asp:Content >
< asp:Content  ID ="Content2"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
    
< asp:TextBox  ID ="txtName"  runat ="server"  Text ="webabcd"   />
    
&nbsp;
    
< asp:Button  ID ="btnSayHello"  runat ="server"  Text ="Hello"  OnClick ="btnSayHello_Click"   />
</ asp:Content >

Hello.aspx.cs
using  System;
using  System.Collections;
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  Sample_Hello : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }


    
protected void btnSayHello_Click(object sender, EventArgs e)
    
{
        Sample.HelloClient proxy 
= new Sample.HelloClient();

        Page.ClientScript.RegisterStartupScript(
            
this.GetType(),
            
"js",
            
string.Format("alert('{0}')", proxy.SayHello(txtName.Text)),
            
true);

        proxy.Close();
    }

}


Web.config
<? xml version="1.0" ?>
< configuration >
  
< system .serviceModel >
    
< client >
      
<!-- address - 服务地址 -->
      
<!-- binding - 通信方式 -->
      
<!-- contract - 服务契约 -->
      
< endpoint  address ="http://localhost:3502/ServiceHost/Sample/Hello.svc"  binding ="basicHttpBinding"  contract ="Sample.IHello"   />
    
</ client >
  
</ system.serviceModel >
</ configuration >


运行结果:
单击" btnSayHello"后弹出提示框,显示"Hello: webabcd"


OK
[源码下载]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值