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

简介:
[索引页]
[源码下载] 


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


作者: webabcd


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


示例
1、服务
IHello.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Sample 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// IHello接口 
InBlock.gif         /// </summary> 
InBlock.gif        [ServiceContract] 
InBlock.gif         public  interface IHello 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 打招呼方法 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">人名</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                [OperationContract] 
InBlock.gif                 string SayHello( string name); 
InBlock.gif        } 
InBlock.gif}
 
Hello.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Sample 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Hello类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Hello : IHello 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 打招呼方法 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">人名</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  string SayHello( string name) 
InBlock.gif                { 
InBlock.gif                         return  "Hello: " + name; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
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" /> 
          
        <asp:Button ID="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" /> 
</asp:Content>
Hello.aspx.cs
InBlock.gif using System; 
InBlock.gif using System.Collections; 
InBlock.gif using System.Configuration; 
InBlock.gif using System.Data; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Web; 
InBlock.gif using System.Web.Security; 
InBlock.gif using System.Web.UI; 
InBlock.gif using System.Web.UI.HtmlControls; 
InBlock.gif using System.Web.UI.WebControls; 
InBlock.gif using System.Web.UI.WebControls.WebParts; 
InBlock.gif using System.Xml.Linq; 
InBlock.gif 
InBlock.gif public partial  class Sample_Hello : System.Web.UI.Page 
InBlock.gif
InBlock.gif         protected  void Page_Load( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif 
InBlock.gif        } 
InBlock.gif 
InBlock.gif         protected  void btnSayHello_Click( object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                Sample.HelloClient proxy =  new Sample.HelloClient(); 
InBlock.gif 
InBlock.gif                Page.ClientScript.RegisterStartupScript( 
InBlock.gif                         this.GetType(), 
InBlock.gif                         "js"
InBlock.gif                         string.Format( "alert('{0}')", proxy.SayHello(txtName.Text)), 
InBlock.gif                         true); 
InBlock.gif 
InBlock.gif                proxy.Close(); 
InBlock.gif        } 
InBlock.gif}
 
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
[源码下载]
 



     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344102 ,如需转载请自行联系原作者



相关文章