[索引页]
[×××] 


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


作者:webabcd


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


示例
1、服务
IHello.cs
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Collections.Generic;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Linq;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Text;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.ServiceModel;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整namespace WCF.ServiceLib.Sample
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整{
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        /// <summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        /// IHello接口
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        /// </summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        [ServiceContract]
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        public interface IHello
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        {
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// <summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// 打招呼方法
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// </summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// <param name="name">人名</param>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// <returns></returns>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                [OperationContract]
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                string SayHello(string name);
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        }
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整}
 
Hello.cs
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Collections.Generic;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Linq;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Text;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.ServiceModel;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整namespace WCF.ServiceLib.Sample
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整{
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        /// <summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        /// Hello类
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        /// </summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        public class Hello : IHello
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        {
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// <summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// 打招呼方法
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// </summary>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// <param name="name">人名</param>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                /// <returns></returns>
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                public string SayHello(string name)
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                {
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                        return "Hello: " + name;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                }
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        }
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整}
 
 
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
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Collections;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Configuration;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Data;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Linq;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Web;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Web.Security;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Web.UI;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Web.UI.HtmlControls;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Web.UI.WebControls;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Web.UI.WebControls.WebParts;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整using System.Xml.Linq;
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整public partial class Sample_Hello : System.Web.UI.Page
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整{
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        protected void Page_Load(object sender, EventArgs e)
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        {
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        }
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        protected void btnSayHello_Click(object sender, EventArgs e)
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        {
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                Sample.HelloClient proxy = new Sample.HelloClient();
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                Page.ClientScript.RegisterStartupScript(
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                        this.GetType(),
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                        "js",
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                        string.Format("alert('{0}')", proxy.SayHello(txtName.Text)),
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                        true);
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整                proxy.Close();
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整        }
化零为整WCF(1) - 不能免俗,我也从Hello开始_化零为整}
 
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
[×××]