ASP.NET發布WebService

來源:互聯網
上載者:User
文章目錄
  • 測試
  • SOAP 1.1
  • SOAP 1.2
  • HTTP POST

ASP.NET發布WebService

一.發布

  1. 建立WebApplication的工程。
  2. 在剛才工程TestWebService中添加WebService檔案(尾碼為asmx),填寫名稱。

 開啟剛才添加的檔案可以看到代碼如下:

 

代碼

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

using System.Web.Script.Services;



namespace Sample

{

/// <summary>

/// Summary description for Service1

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ToolboxItem(false)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]

public class Service1 : System.Web.Services.WebService

{



[WebMethod]

public string HelloWorld()

{

return "Hello World";

}



[WebMethod]

public int Add(int a, int b)

{

return (a + b);

}



[WebMethod]

public int Minus(int a, int b)

{

return (a - b);

}

}

}

 

以上代碼需要注意的地方有:

(1).發現該頁面中產生了一個類的代碼,該類繼承自System.Web.Services.WebService

例如:

 

public class Service1 : System.Web.Services.WebService

(2).如果要使用asp.net ajax[System.Web.Script.Services.ScriptService]

    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]

(3).如果需要將一個函數發布成WebService,需要在public的函數的開頭加上[WebMethod]

 

二.測試

直接運行該頁面,出現以下頁面:

可以看到 Service1是該類的名稱,所有發布的WebService名稱列在頁面首部,下面的文字是對WebService的說明,如果您在asmx的代碼中沒有修改命名空間,則建議您修改該命名空間。

Service1

 

支援下列操作。有關正式定義,請查看服務說明

  • Add
  • HelloWorld
  • Minus

Web 服務使用 http://tempuri.org/ 作為預設命名空間。 

建議: 公開 XML Web services 之前,請更改預設命名空間。 

每個 XML Web services 都需要一個唯一的命名空間,以便用戶端應用程式能夠將它與 Web 上的其他服務區分開。http://tempuri.org/ 可用於處於開發階段的 XML Web services,而發行的 XML Web services 應使用更為永久的命名空間。

應使用您控制的命名空間來標識 XML Web services。例如,可以使用公司的 網際網路網域名稱作為命名空間的一部分。儘管有許多 XML Web services 命名空間看似 URL,但它們不必指向 Web 上的實際資源。(XML Web services 命名空間為 URI。)

使用 ASP.NET 建立 XML Web services 時,可以使用 WebService 特性的 Namespace 屬性更改預設命名空間。WebService 特性適用於包含 XML Web services 方法的類。下面的代碼執行個體將命名空間設定為“http://microsoft.com/webservices/”:

C#

[WebService(Namespace="http://microsoft.com/webservices/")]

public class MyWebService {

    // 實現

}

Visual Basic

<WebService(Namespace:="http://microsoft.com/webservices/")> Public Class MyWebService

    ' 實現

End Class

C++

[WebService(Namespace="http://microsoft.com/webservices/")]

public ref class MyWebService {

    // 實現

};

有關 XML 命名空間的更多詳細資料,請參閱 Namespaces in XML (XML 命名空間)上的 W3C 建議。

有關 WSDL 的更多詳細資料,請參閱 WSDL Specification (WSDL 規範)

有關 URI 的更多詳細資料,請參閱 RFC 2396

 

點擊Add串連可以開啟Add的測試頁面,輸入參數,單擊[調用]按鈕即可得到值。

Service1

單擊此處,擷取完整的巨集指令清單。

Add測試

若要使用 HTTP POST 協議對操作進行測試,請單擊“調用”按鈕。

SOAP 1.1

以下是 SOAP 1.2 請求和響應樣本。所顯示的預留位置需替換為實際值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Add"
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Add xmlns="http://tempuri.org/">
      <a>int</a>
      <b>int</b>
    </Add>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <AddResponse xmlns="http://tempuri.org/">
      <AddResult>int</AddResult>
    </AddResponse>
  </soap:Body>
</soap:Envelope>
SOAP 1.2

以下是 SOAP 1.2 請求和響應樣本。所顯示的預留位置需替換為實際值。

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <Add xmlns="http://tempuri.org/">
      <a>int</a>
      <b>int</b>
    </Add>
  </soap12:Body>
</soap12:Envelope>
HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <AddResponse xmlns="http://tempuri.org/">
      <AddResult>int</AddResult>
    </AddResponse>
  </soap12:Body>
</soap12:Envelope>
HTTP POST

以下是 HTTP POST 請求和響應樣本。所顯示的預留位置需替換為實際值。

POST /Service1.asmx/Add HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
 
a=string&b=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
 
<?xml version="1.0" encoding="utf-8"?>
<int xmlns="http://tempuri.org/">int</int>

在IE中您應當看到以下表格中的如果您在Google Chrome中訪問會發現只有3,需要查看原始碼才能看到以下內容。

<?xml version="1.0" encoding="utf-8" ?>

  <int xmlns="http://tempuri.org/">3</int>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.