程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WCF示例(1) - 不能免俗,我也從Hello開始

WCF示例(1) - 不能免俗,我也從Hello開始

編輯:關於.NET

介紹

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved