新建一個空網站項目,添加新建項 “ Web 服務 ”。
一、WebServiceDemo.asmx 文件,默認內容如下:
<%@ WebService Language="C#" CodeBehind="WebServiceDemo.asmx.cs" Class="WebServie.WebServiceDemo" %>
二、WebServiceDemo.asmx.cs 文件,默認內容如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebServie { /// <summary> /// WebServiceDemo 的摘要說明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。 // [System.Web.Script.Services.ScriptService] public class WebServiceDemo : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } }
如上源碼中,黃色標記 “ using System.Web.Services; ” 為必須引入的命名空間。
繼承層次:WebServiceDemo 繼承自 System.Web.Services 命名空間下的 WebService 類 → 繼承自 System.Web.Services 命名空間下的MarshalByValueComponent 類 → 繼承自 System.ComponentModel 命名空間下的 IComponent, IDisposable, IServiceProvider 三個接口。
WebService 主要包含 WebService 、SoapDocumentService、WebServiceBinding三個屬性。若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,需取消對ScriptService 屬性的注釋。
1、WebService 屬性:用於向 XML Web services 添加附加信息,如描述其功能的字符串。
using System; namespace System.Web.Services { // 用於向 XML Web services 添加附加信息,如描述其功能的字符串。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] public sealed class WebServiceAttribute : Attribute { // Namespace的默認值 // property.此字段為常數。 public const string DefaultNamespace = "http://tempuri.org/"; // 初始化 System.Web.Services.WebServiceAttribute 類的新實例。 public WebServiceAttribute(); // XML Web services 的描述性消息。 // 對 XML Web services 的功能進行描述的文本。 public string Description { get; set; } // 獲取或設置 XML Web services 的名稱。 // 默認值是實現 XML Web services 的類的名稱。 public string Name { get; set; } // 獲取或設置用於 XML Web services 的默認 XML 命名空間。 // 默認值在 System.Web.Services.WebServiceAttribute.DefaultNamespace屬性中指定。 public string Namespace { get; set; } } }
2、SoapDocumentServiceAttribute 屬性:
using System; using System.Web.Services.Description; namespace System.Web.Services.Protocols { // 設置在 XML Web services 內發送到 XML Web services 方法的 SOAP 請求和從 XML Web services 方法發回的 SOAP 響應的默認格式。 [AttributeUsage(AttributeTargets.Class)] public sealed class SoapDocumentServiceAttribute : Attribute { //初始化一個 SoapDocumentServiceAttribute 類的新實例,將所有屬性都設置為其默認值。 public SoapDocumentServiceAttribute(); //初始化一個 SoapDocumentServiceAttribute 類的新實例,設置參數的格式設置。 // 參數: // use:XML Web services 的參數格式設置。設置 SoapDocumentServiceAttribute.Use 屬性。 public SoapDocumentServiceAttribute(SoapBindingUse use); //初始化 SoapDocumentServiceAttribute 類的新實例,該類設置參數的格式設置並設置參數是否封裝在 SOAP 消息中的 Body 元素下的單個 XML 元素中。 // 參數:
// use:參數格式設置樣式。設置 SoapDocumentServiceAttribute.Use 屬性。 // paramStyle:設置參數是否封裝在 XML Web services 中發往和來自 XML Web services 方法的 SOAP 消息中的 Body 元素下的單個 // XML 元素中。設置 ParameterStyle 屬性。 public SoapDocumentServiceAttribute(SoapBindingUse use, SoapParameterStyle paramStyle); //獲取或設置用來控制參數是否封裝在 XML Web services 的 XML Web services 方法的 SOAP 消息 XML 部分中 <Body> 元素之后的單個元素中的默認設置。 // 返回結果: // 在 XML Web services 中發往 XML Web services 方法的 SOAP 的請求和從 XML Web services 中的 // XML Web services 方法發回的 SOAP 響應的默認 SoapParameterStyle。如果未設置,則默認為 SoapParameterStyle.Wrapped。 public SoapParameterStyle ParameterStyle { get; set; } //獲取或設置將 SOAP 消息發送到 XML Web services 的方式。 // 返回結果:SoapServiceRoutingStyle,表示將 SOAP 消息發送到 XML Web services 的方式。默認值為 SoapServiceRoutingStyle.SoapAction。 public SoapServiceRoutingStyle RoutingStyle { get; set; } //獲取或設置 XML Web services 的默認參數格式設置。 // 返回結果:XML Web services 的默認 SoapBindingUse。如果未設置,則默認為 SoapBindingUse.Literal。 public SoapBindingUse Use { get; set; } } }
3、WebServiceBinding 屬性:聲明定義一個或多個 XML Web services 方法的綁定。無法繼承此類。
using System; namespace System.Web.Services { //聲明定義一個或多個 XML Web services 方法的綁定。無法繼承此類。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, AllowMultiple = true)] public sealed class WebServiceBindingAttribute : Attribute { // 初始化一個 WebServiceBindingAttribute 的實例
public WebServiceBindingAttribute(); // 通過設置 XML Web services 方法正在實現的綁定的名稱初始化 System.Web.Services.WebServiceBindingAttribute 類的新實例 // 參數: // name:XML Web services 方法為其實現操作的綁定的名稱。設置 System.Web.Services.WebServiceBindingAttribute.Name 屬性。 public WebServiceBindingAttribute(string name); //初始化 System.Web.Services.WebServiceBindingAttribute 類的新實例。 // 參數: // name:XML Web services 方法為其實現操作的綁定的名稱。設置 System.Web.Services.WebServiceBindingAttribute.Name 屬性。 // ns:與該綁定關聯的命名空間。設置 System.Web.Services.WebServiceBindingAttribute.Namespace 屬性。 public WebServiceBindingAttribute(string name, string ns); //初始化 System.Web.Services.WebServiceBindingAttribute 類的新實例。
// 參數: // name:XML Web services 方法為其實現操作的綁定的名稱。設置 System.Web.Services.WebServiceBindingAttribute.Name 屬性。 // ns:與該綁定關聯的命名空間。設置 System.Web.Services.WebServiceBindingAttribute.Namespace 屬性。 // location:定義綁定的位置。 public WebServiceBindingAttribute(string name, string ns, string location); // 摘要:獲取或設置綁定聲稱所符合的 Web 服務互操作性 (WSI) 規范。 // 返回結果:System.Web.Services.WsiProfiles 值之一,指示 WSI 規范。 public WsiProfiles ConformsTo { get; set; } // 摘要:獲取或設置一個值,該值指示綁定是否發出一致性聲稱。 // 返回結果:如果綁定發出一致性聲稱,則為 true;否則為 false。 public bool EmitConformanceClaims { get; set; }
// 摘要:獲取或設置定義綁定的位置。 // 返回結果:定義綁定的位置。默認為此屬性應用到的 XML Web services 的 URL。 public string Location { get; set; } // 摘要:獲取或設置綁定的名稱。 // 返回結果:綁定的名稱。默認為追加了“Soap”的 XML Web services 名稱。 public string Name { get; set; } // 摘要:獲取或設置與該綁定關聯的命名空間。
// 返回結果:綁定的命名空間。默認為 http://tempuri.org/。 public string Namespace { get; set; } } }
4、ScriptServiceAttribute 屬性:指示某個 Web 服務可從腳本調用。
using System; using System.Runtime; namespace System.Web.Script.Services { //指示某個 Web 服務可從腳本調用。無法繼承此類。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)] public sealed class ScriptServiceAttribute : Attribute { //初始化 System.Web.Script.Services.ScriptServiceAttribute 類的新實例。 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] public ScriptServiceAttribute(); } }
5、WebMethod 屬性:
WebService 通過 [WebMethod] 屬性將方法暴露給調用者,有6個屬性:Description、EnableSession、MessageName、TransactionOption、CacheDuration、BufferResponse。
using System; using System.EnterpriseServices; namespace System.Web.Services { //向使用 ASP.NET 創建的 XML Web services 中的某個方法添加此特性后,就可以從遠程 Web 客戶端調用該方法。無法繼承此類。 [AttributeUsage(AttributeTargets.Method)] public sealed class WebMethodAttribute : Attribute { // 初始化一個 WebMethodAttribute 新實例。 public WebMethodAttribute(); // 初始化一個 WebMethodAttribute 新實例。
// 參數: // enableSession:初始化是否為 XML Web services 方法啟用會話狀態。 public WebMethodAttribute(bool enableSession); //初始化 WebMethodAttribute 類的新實例。 // 參數: // enableSession:初始化是否為 XML Web services 方法啟用會話狀態。 // transactionOption:初始化 XML Web services 方法的事務支持。 public WebMethodAttribute(bool enableSession, TransactionOption transactionOption); // 初始化 WebMethodAttribute 類的新實例。 // 參數: // enableSession:初始化是否為 XML Web services 方法啟用會話狀態。 // transactionOption:初始化 XML Web services 方法的事務支持。 // cacheDuration:初始化響應的緩存秒數。 public WebMethodAttribute(bool enableSession, TransactionOption transactionOption, int cacheDuration); // 初始化 WebMethodAttribute 類的新實例。 // 參數: // enableSession:初始化是否為 XML Web services 方法啟用會話狀態。 // transactionOption:初始化 XML Web services 方法的事務支持。 // cacheDuration:初始化響應的緩存秒數。 // bufferResponse:初始化是否緩存該請求的響應。 public WebMethodAttribute(bool enableSession, TransactionOption transactionOption, int cacheDuration, bool bufferResponse); //獲取或設置是否緩存該請求的響應。 // 返回結果:如果緩存該請求的響應,則為 true;否則為 false。默認為 true。 public bool BufferResponse { get; set; } //獲取或設置響應應在緩存中保留的秒數。 // 返回結果:響應應在緩存中保留的秒數。默認值為 0,表示不緩存響應。 public int CacheDuration { get; set; } //描述 XML Web services 方法的描述性消息。 // 返回結果:描述 XML Web services 方法的描述性消息。默認值為 System.String.Empty。 public string Description { get; set; } //指示是否為 XML Web services 方法啟用會話狀態。 // 返回結果:如果為 XML Web services 方法啟用會話狀態,則為 true。默認為 false。 public bool EnableSession { get; set; } //在傳遞到 XML Web services 方法和從 XML Web services 方法返回的數據中用於 XML Web services 方法的名稱。 // 返回結果: // 在傳遞到 XML Web services 方法和從 XML Web services 方法返回的數據中用於 XML Web services 方法的名稱。默認值是XML Web services 方法的名稱。 public string MessageName { get; set; } //指示 XML Web services 方法的事務支持。 // 返回結果:XML Web services 方法的事務支持。默認為 TransactionOption.Disabled。 public TransactionOption TransactionOption { get; set; } } }
摘要: // Initializes a new instance of the System.Web.Services.WebMethodAttribute // class.
