共用方式為


程式碼模型:以 Managed 程式碼建立 Web 服務

更新:2007 年 11 月

Web 服務是由兩個部分組成:Web 服務進入點及實作 Web 服務功能的程式碼。在 ASP.NET 中,.asmx 檔案是文字檔,可做為 Web 服務的可定址進入點。它參考預先編譯組件中的程式碼、程式碼後置的檔案或 .asmx 檔案本身所內含的程式碼。

注意事項:

如果程式碼直接內含於 .asmx 檔案中,則 ASP.NET 會在伺服器上視需要編譯該程式碼。

位於 .asmx 檔案上方的 WebService 處理器指示詞會決定於何處尋找 Web 服務的實作。依預設,當您使用 ASP.NET Web 服務專案範本建立 Web 服務時,Visual Studio 會使用程式碼後置的檔案,如 Service1.asmx.vb 或 Service1.asmx.cs。

注意事項:

Visual Studio 不支援跨越程式設計語言的編譯。例如,Visual Basic ASP.NET Web 應用程式專案不能包含 Visual C# Web 服務,反之亦然。

當您以 Managed 程式碼建置 Web 服務時,ASP.NET 自動提供基礎架構並處理 Web 服務要求及回應的處理,包括剖析及建立 SOAP 訊息。

WebService 處理指示詞

位於 .asmx 網頁上方的是 WebService 處理器指示詞,它包含關於 Web 服務實作之屬性格式的資訊。這個處理器指示詞將為 ASP.NET 環境提供必要資訊,如哪一種類別會實作 Web 服務功能。下列顯示 WebService 處理器指示詞的範例:

[Visual Basic]
<%@ WebService Language="vb" Codebehind="Service1.asmx.vb"
    Class="WebService1.Service1" %>


[C#]
<%@ WebService Language="c#" Codebehind="Service1.asmx.cs"
    Class="WebService1.Service1" %>

Language 屬性指示用來開發 Web 服務的程式語言。您可用任何 .NET 相容語言來建立 Web 服務,如 Visual Basic .NET 或 Visual C#。

當使用 Visual Studio .NET 以 Managed 程式碼建立 Web 服務時,Web 服務的實作會存放於程式碼後置的檔案中。程式碼後置的檔案與使用 Codebehind 屬性的 .asmx 網頁關聯。

注意事項:

這個屬性會協助 Visual Studio 管理您的 Web 服務專案,但在執行階段它是不需要的。

使用 ASP.NET Web 服務專案範本時,Class 屬性會指出程式碼後置檔中的哪一種類別會實作 Web 服務的功能。

當您使用 ASP.NET Web 服務專案範本時,Visual Studio .NET 會將這個處理器指示詞自動插入 .asmx 檔案。

秘訣

如果您選擇重新命名類別,請確定變更 WebService 指示詞 Class 屬性中的類別名稱。

注意事項:

若要檢視 .asmx 檔案的內容,請在 [方案總管] 中以滑鼠右鍵按一下 .asmx 檔案,然後按一下快速鍵功能表上的 [開啟方式]。在 [開啟方式] 對話方塊中,選取 [原始程式碼 (文字) 編輯器],然後按一下 [開啟]。

如需詳細資訊,請參閱逐步解說:使用 ASP.NET 建置基本 XML Web Service

System.Web.Services.WebService 類別

定義 Web 服務之選擇性基底類別的 System.Web.Services.WebService 類別,能夠直接存取通用 ASP.NET 物件,例如應用程式和工作階段狀態的物件。依預設,使用 Visual Studio 以 Managed 程式碼建立的 Web 服務會繼承自這個類別。Web 服務可繼承自這個類別以取得 ASP.NET 內建物件的存取權限,如要求和工作階段。如需詳細資訊,請參閱 WebService 類別

如果 Web 服務並非繼承自此類別,則可以從 System.Web.HttpContext.Current 存取 ASP.NET 內建物件。實作 Web 服務的類別必須是公用的,而且必須有公用的預設建構函式 (沒有參數的建構函式)。這樣使得 ASP.NET 能夠建立 Web 服務類別的執行個體,以處理連入的 Web 服務要求。如需詳細資訊,請參閱 HttpContext.Current 屬性 (Property)

[Visual Basic]
Imports System.Web.Services
Public Class Service1
   Inherits System.Web.Services.WebService
   ' Implementation code.
End Class


[C#]
using System.Web.Services;
public class Service1 : System.Web.Services.WebService
{
   // Implementation code.
}

如需詳細資訊,請參閱從 WebService 類別繼承

WebService 屬性

每個 Web 服務都需要一個唯一的命名空間,讓用戶端應用程式能夠區別可能使用同一方法名稱之 Web 服務的差異。Visual Studio .NET 中建立的 Web 服務之預設命名空間是 "http://tempuri.org/WebService1/Service1",其中 WebService1 是專案名稱,而 Service1 是類別名稱。雖然命名空間與典型的 URL 類似,但是不應假設可在 Web 瀏覽器中檢視,它僅僅是唯一識別項。

注意事項:

您可能想要在該位置提供 Web 網頁,其中包含有關您所提供的 Web 服務資訊。

使用 WebService 屬性,您可以指定命名空間,並提供 Web 服務的簡短描述。當您從瀏覽器呼叫 Web 服務且未指定查詢字串時,則會在服務說明網頁上顯示此簡短描述:

[Visual Basic]
<System.Web.Services.WebService( _ 
   Namespace:="http://tempuri.org/WebService1/Service1", _ 
   Description:="A short description of the Web service.")> _
Public Class Service1
   Inherits System.Web.Services.WebService
   ' Implementation code.
End Class


[C#]
[System.Web.Services.WebService( 
   Namespace="http://tempuri.org/WebService1/Service1",  
   Description="A short description of the Web service.")] 
public class Service1 : System.Web.Services.WebService
{
   // Implementation code.
}

如需詳細資訊,請參閱 WebServiceAttribute 類別使用 WebService 屬性

套用 WebMethod 屬性

若要公開方法做為 Web 服務的一部分,您必須在要公開的每個公用方法的宣告之前放置 WebMethod 屬性。如需詳細資訊,請參閱HOW TO:建立 Web 服務方法

[Visual Basic]
<System.Web.Services.WebMethod()> _
Public Function MyString(ByVal x as string) As String
   ' Implementation code.
End Function


[C#]
[System.Web.Services.WebMethod()] 
public string MyString(string x) 
{
   //implementation code
}

WebMethod 屬性包含數個屬性,能夠設定 Web 服務的行為。如需詳細資訊,請參閱 WebMethodAttribute 類別使用 WebMethod 屬性。例如,您可使用這個屬性來提供將要顯示在關聯的服務說明網頁上的簡短描述:

[Visual Basic]
<System.Web.Services.WebMethod( _ 
   Description:="A short description of this method.")> _
Public Function MyString(ByVal x as string) As String
   ' Implementation code.
End Function


[C#]
[System.Web.Services.WebMethod( 
   Description="A short description of this method.")] 
public string MyString(string x) 
{
   // Implementation code.
}

當有多數個屬性 (Property) 時,請以逗號分隔。例如,若要提供描述並快取 Web 服務結果長達 60 秒鐘:

[Visual Basic]
<System.Web.Services.WebMethod( _ 
   Description:="A short description of this method.", _ 
           CacheDuration:=60)> _
Public Function MyString(ByVal x as string) As String
   ' Implementation code.
End Function


[C#]
[System.Web.Services.WebMethod( 
   Description="A short description of this method.", 
   CacheDuration=60)] 
public string MyString(string x) 
{
   // Implementation code.
}

摘要

每個 Web 服務都是由一個 .asmx 檔案及一個 Web 服務類別所組成。.asmx 檔案包括用來參考類別的 WebService 處理器指示詞。Web 服務類別是公用類別,具有公用的預設建構函式,並且包含一個或多個使用 WebMethod 屬性加以標記的公用方法。下列圖表顯示專案、類別、類別的方法與產生的 Web 服務之間的關聯性。

WebService 類別和產生的項目

請參閱

概念

程式碼模型:以 Managed 程式碼存取 Web 服務

其他資源

以 Managed 程式碼建立 Web 服務