web服務器(Web Services)的小介紹(4)--(C#)

                                  web服務器(webservices)
        webservices即Web Service, Web Service技術,是應用於第三方軟件的,我們可以通過配置它,給第三方配置一個對外的可以調用的外部的一個 接口。在我們開發中是經常需要用到的重要的知識內容,所以在這裏和大家探究一下它的主要用法:

    1、web服務的頁面都是以 .asmx結尾的.首先是在framework3.5版本以下,因爲在4.0版本中微軟是集成到wcf中

       

wKioL1L7gPfR0_E-AAJlguN4U8Q091.jpg

    

  2、.asmx是web服務的前臺頁面文件,會使用一個叫做.asmx.cs的文件來存放頁面類文件,例如:getpig.asmx 會有一個對應的getpig.asmx.cs文件。

wKiom1L7hGWywJxpAACN9hXGxeE505.jpg

    3、getpig.asmx.cs 中的內容說明:
         3.1、所有web服務頁面類一定要集成                                   System.Web.Services.WebService public class GetPig : System.Web.Services.WebService 
         3.2、要想使某個方法做爲web服務的方式暴露出去一定在此方法上添加: [WebMethod] 特性,這樣才能在其他站點中訪問到,否則是作爲此web服務中的內部方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Web服務
{
/// <summary>
/// Service1 的摘要說明
/// </summary>
[WebService(Namespace = "http://itcast.cn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消註釋以下行。
// [System.Web.Script.Services.ScriptService]
public class GetPig : System.Web.Services.WebService
{
[WebMethod] // 注意:在web服務器類中如果要使某個方法能夠被外部調用則一定要加上 [WebMethod]特性
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public Pig GetPigInfo(int age)
{
List<Pig> list = new List<Pig>() {
new Pig(){ Name="豬豬",Age=2},
new Pig(){ Name="小豬",Age=1},
new Pig(){ Name="八戒",Age=500}
};
return list.FirstOrDefault(c => c.Age == age);
}
}
}

   4、在一個web站點的項目中要想使用web服務必須按照以下步驟來執行:
         4.1、在web站點的【引用】上右鍵點擊 【添加服務引用】,這時VS 自動打開服務面板,輸入你發佈的web服務的地址(例如:http://127.0.0.1/GetPig.asmx)

wKiom1L7gh7CKeL3AABrIy-23Jg761.jpg

  

  後自動生成web服務對於的代理類,同時會向web站點的web.config中註冊 <system.serviceModel>
   <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="GetPigSoap" />
            </basicHttpBinding>
        </bindings>
        <client>
        <endpoint  address="http://192.168.10.2:8085/GetPig.asmx"   //web服務的發佈地址
                 binding="basicHttpBinding"                       //web服務的綁定方式
                 bindingConfiguration="GetPigSoap"                //web服務的配置類型名稱
                 contract="WbS.GetPigSoap"                        //web服務的契約
                name="GetPigSoap" />                              //web服務的名稱
        </client>
    </system.serviceModel>

        4.2、在web站點中調用web服務的寫法:

           先導入命名空間(查看方式在:網站項目的 Service References 下的代理對象中查看,例如演示項目中的命名空間爲:WebSite.Webs) 比如要調用 http://192.168.10.2:8085/GetPig.asmx 中的方法 GetPigInfo 的寫法:
  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebSite
{
//1.0 導入web服務器代理類的命名空間
using WebSite.WbS;
using WebSite.WebsDog;
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GetDogInfoSoapClient client = new GetDogInfoSoapClient();
Dog dog = client.GetDog();
}
protected void Button1_Click(object sender, EventArgs e)
{
GetPigSoapClient client = new GetPigSoapClient();
Pig pig = client.GetPigInfo(int.Parse(TextBox1.Text));
Response.Write(pig.Name + "  ,Age=" + pig.Age);
}
}
}

 

 

j_0050.gif

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章