天天看點

化零為整WCF(1) - 不能免俗,我也從Hello開始

<a href="http://webabcd.blog.51cto.com/1787395/343999" target="_blank">[索引頁]</a>

化零為整WCF(1) - 不能免俗,我也從Hello開始

介紹

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 

        /// &lt;summary&gt; 

        /// IHello接口 

        /// &lt;/summary&gt; 

        [ServiceContract] 

        public interface IHello 

        { 

                /// &lt;summary&gt; 

                /// 打招呼方法 

                /// &lt;/summary&gt; 

                /// &lt;param name="name"&gt;人名&lt;/param&gt; 

                /// &lt;returns&gt;&lt;/returns&gt; 

                [OperationContract] 

                string SayHello(string name); 

        } 

}

Hello.cs

        /// Hello類 

        public class Hello : IHello 

                public string SayHello(string name) 

                { 

                        return "Hello: " + name; 

                } 

2、宿主

Hello.svc

&lt;%@ ServiceHost Language="C#" Debug="true" Service="WCF.ServiceLib.Sample.Hello" %&gt;

Web.config

&lt;?xml version="1.0"?&gt; 

&lt;configuration&gt; 

    &lt;system.serviceModel&gt; 

        &lt;behaviors&gt; 

            &lt;serviceBehaviors&gt; 

                &lt;behavior name="SampleBehavior"&gt; 

                    &lt;!--httpGetEnabled - 使用get方式提供服務--&gt; 

                    &lt;serviceMetadata httpGetEnabled="true" /&gt; 

                &lt;/behavior&gt; 

            &lt;/serviceBehaviors&gt; 

        &lt;/behaviors&gt; 

        &lt;services&gt; 

            &lt;!--name - 提供服務的類名--&gt; 

            &lt;!--behaviorConfiguration - 指定相關的行為配置--&gt; 

            &lt;service name="WCF.ServiceLib.Sample.Hello" behaviorConfiguration="SampleBehavior"&gt; 

                &lt;!--address - 服務位址--&gt; 

                &lt;!--binding - 通信方式--&gt; 

                &lt;!--contract - 服務契約--&gt; 

                &lt;endpoint address="" binding="basicHttpBinding" contract="WCF.ServiceLib.Sample.IHello" /&gt; 

            &lt;/service&gt; 

        &lt;/services&gt; 

    &lt;/system.serviceModel&gt; 

&lt;/configuration&gt;

3、用戶端

Hello.aspx

&lt;%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Hello.aspx.cs" 

        Inherits="Sample_Hello" Title="不能免俗,我也從Hello開始" %&gt; 

&lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"&gt; 

&lt;/asp:Content&gt; 

&lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"&gt; 

        &lt;asp:TextBox ID="txtName" runat="server" Text="webabcd" /&gt; 

        &lt;asp:Button ID="btnSayHello" runat="server" Text="Hello" OnClick="btnSayHello_Click" /&gt; 

&lt;/asp:Content&gt;

Hello.aspx.cs

using System.Collections; 

using System.Configuration; 

using System.Data; 

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(); 

        &lt;client&gt; 

            &lt;!--address - 服務位址--&gt; 

            &lt;!--binding - 通信方式--&gt; 

            &lt;!--contract - 服務契約--&gt; 

            &lt;endpoint address="http://localhost:3502/ServiceHost/Sample/Hello.svc" binding="basicHttpBinding" contract="Sample.IHello" /&gt; 

        &lt;/client&gt; 

運作結果:

單擊"btnSayHello"後彈出提示框,顯示"Hello: webabcd"

OK

<a href="http://down.51cto.com/data/100781" target="_blank">[源碼下載下傳]</a>

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344102,如需轉載請自行聯系原作者

繼續閱讀