WebService簡單實現

來源:互聯網
上載者:User

標籤:

1. WebService  

SOAP、WSDL、UDDI
SOAP(Simple Object Access Protocal,簡易物件存取通訊協定 (SOAP)),是在分散或在分布式環境中交換資訊的簡單協議。
WSDL(Web Services Description Language,Web服務描述語言) 對WebService 的解釋說明文檔,描述Web服務發布的XML格式
UDDI 統一描述、發現和整合(Universal Description, Discovery, and Integration)的縮寫,是Web服務的黃頁,它是一個基於XML的跨平台的描述規範,可以使世界範圍內的企業在互連網上發布自己所提供的服務供其他客戶查詢使用。

 WebService工作原理:用戶端向伺服器段發出調用請求,發出一個SOAP訊息;服務端對這SOAP訊息處理後返回結果;基於SOAP協議。

  優點:跨語言、跨平台、不局限於區域網路和廣域網路、分布式異地調用。  SOAP協調使用通過HTTP 和 XML(文本語言) 進行通訊

  缺點:傳輸的資料量過大、冗餘資料過多、速度感覺延遲。

Web Service :是可互操作的分布式應用程式 Web Service :使用 HTTP 和 XML 進行通訊 Web Service :可以穿越防火牆,真正實現自由通訊 通過 SOAP 實現異地調用 預設 80 連接埠

Web Service(Web 服務)提供以下屬性。

Namespace:預設是"http://tempuri.org/",此屬性的值包含 XML Web Service 的預設命名空間。XML 命名空間提供了一種在 XML 文檔中建立名稱的方法,該名稱可由統一資源識別項(URI)標識。使用XML命名空間,可以唯一標識XML文檔中的元素或屬性因而,在 XML Web Service 的服務說明中,Namespace 被用做與 XML Web Service 直接相關的XML 元素的預設命名空間。如果不指定命名空間,則使用預設命名空間http://tempuri.org/。

Name:此屬性的值包含 XML Web Service 的名稱。在預設情況下,該值是實現 XML Web Service 的類的名稱。

Description:此屬性的值包含描述性訊息,此訊息將在 XML Web Service 的說明檔案(例如服務說明和服務協助頁)產生後顯示給 XML Web Service 的潛在使用者。

 

WebMethod(Web 服務方法)有以下 6 個屬性。

Description:是對 Web Service 方法的描述資訊。就像 Web Service 方法的功能注釋,可以讓調用者看見的注釋。

EnableSession:指示 Web Service 是否啟動 Session 標誌,主要通過 Cookie 完成,預設為 false。

MessageName:主要實現方法重載後的重新命名:

TransactionOption:指示 Web Service 方法的事務支援。

CacheDuration:設定響應應在緩衝中保留的秒數。這樣 Web Service 就不需要重複執行多遍,可以提高訪問效率,而 CacheDuration 就是指定緩衝時間的屬性。

簡單代碼實現如下:

using System;using System.Collections.Generic;using System.EnterpriseServices;using System.Linq;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;namespace TestWebService1{    /// <summary>    /// WebService1 的摘要說明    /// </summary>    [WebService(Namespace = "http://yxl.org/",Name="MyWebService",Description="我的webservice")]    [WebServiceBinding(ConformsTo = WsiProfiles.None)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消注釋以下行。     // [System.Web.Script.Services.ScriptService]    public class WebService1 : System.Web.Services.WebService    {        public MySoapHeader myHeader;        [SoapHeader("myHeader")]        [WebMethod( MessageName = "GetTime")]        public string GetTime()        {            if (!myHeader.IsValid())            {                return "對不起,您沒有許可權訪問";            }            return DateTime.Now.ToString();        }        [WebMethod(Description="擷取所有的學生",EnableSession=false,TransactionOption=TransactionOption.Required,CacheDuration=10)]        public List<Student> GetStudent()        {                      return new List<Student>()            {                new Student() {Id = 1, Name = "張三1"},                new Student() {Id = 2, Name = "張三2"},                new Student() {Id = 3, Name = "張三3"}            };        }         [WebMethod(Description = "擷取所有的學生1", EnableSession = false,MessageName="GetStudent2")]        public List<Student> GetStudent(string name)        {            return new List<Student>()            {                new Student() {Id = 1, Name = "張三1"},                new Student() {Id = 2, Name = "張三2"},                new Student() {Id = 3, Name = "張三3"}            };        }    }    public class Student    {        public int Id { get; set; }        public string Name { get; set; }    }    public class MySoapHeader : SoapHeader    {        public string UserName { get; set; }        public string Password { get; set; }        public bool IsValid()        {            if (UserName == "admin" && Password == "123")            {                return true;            }            else            {                return false;            }        }    }}

 

發布後調用實現如下:

using ShowStudentInfo.ServiceReference1;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace ShowStudentInfo{    public partial class _Default : Page    {        protected void Page_Load(object sender, EventArgs e)        {            if(!IsPostBack){                loadInfo();            }        }        public void loadInfo() {            using (MyWebServiceSoapClient client = new MyWebServiceSoapClient())            {                //Student[] students = client.GetStudent();                //foreach (var student in students)                //{                //    Response.Write(student.Name + "</br>");                //}                //Response.Write("<hr/>");                MySoapHeader myHeader = new MySoapHeader();                myHeader.UserName = "admin";                myHeader.Password = "123";//使用者名稱和密碼要和調用的Web Service 一致                Response.Write(client.GetHello(myHeader));            }        }    }}

 

WebService簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.