[WebServices] 之二:支援的資料類型

來源:互聯網
上載者:User
ASP.NET WebService 支援絕大多數的基元類型及其數組,另外還支援自訂的結構(Struct)、類型(Class)、枚舉(Enum)、DataSet、XmlElement、XmlNode、集合(IEnumerable/ICollection)等。

ASP.NET WebService 使用 XmlSerializer 進行序列化操作,對於自訂類型要注意以下幾點:

1. 只能序列化可讀寫公用屬性和欄位。唯讀屬性(get;)、唯讀欄位(readonly)、常量(const)以及所有的非 public 資料成員都不會被序列化。
2. 自訂類型必須具有不接受任何參數的預設建構函式。
3. 不能序列化方法。用戶端產生的代理對象不包含任何自訂類型方法(不是WebMethod)。

基於以上幾點,因此我們最好只定義純粹用來傳輸複合資料的資料類型(Data Object)。

以下是一些示範代碼。

基元類型[WebService(Namespace = "http://www.rainsts.net/", Description="我的Web服務")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

 public WebService () {
 }

  [WebMethod]
  public DateTime GetNowTime()
  {
    return DateTime.Now;
  }

  [WebMethod]
  public string[] GetStringArray()
  {
    return new string[] { "a", "b", "c"};
  }

  [WebMethod]
  public float[] GetFloatArray()
  {
    return new float[]{1F, 2F, 3F};
  }

  [WebMethod]
  public byte[] GetBytes()
  {
    return System.IO.File.ReadAllBytes(@"c:/windows/notepad.exe");
  }
}

枚舉 Enumpublic enum Sex
{
  Female,
  Male
}

[WebService(Namespace = "http://www.rainsts.net/", Description="我的Web服務")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

 public WebService () {
 }

  [WebMethod]
  public Sex GetSex()
  {
    return Sex.Female;
  }

  [WebMethod]
  public Sex[] GetAllSex()
  {
    return Enum.GetValues(typeof(Sex)) as Sex[];
  }
}

結構 Struct

結構體預設就會建立無參數構造方法,且不允許自訂。public struct MyStruct
{
  public int X;
  public int Y;

  public MyStruct(int x, int y)
  {
    this.X = x;
    this.Y = y;
  }
}

[WebService(Namespace = "http://www.rainsts.net/", Description="我的Web服務")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

 public WebService () {
 }

  [WebMethod]
  public MyStruct GetMyStruct()
  {
    MyStruct st = new MyStruct(15, 16);
    return st;
  }

  [WebMethod]
  public MyStruct[] GetMyStructs()
  {
    return new MyStruct[] { new MyStruct(1, 2), new MyStruct(3, 4) }; ;
  }
}

類型 Classpublic struct MyStruct
{
  public int X;
  public int Y;

  public MyStruct(int x, int y)
  {
    this.X = x;
    this.Y = y;
  }
}

public class MyClass
{
  public MyClass()
  {
    myStruct = new MyStruct();
  }

  public MyClass(int x, int y, string name) : this()
  {
    myStruct.X = x;
    myStruct.Y = y;
    this.name = name;
  }

  private string name;

  public string Name
  {
    get { return name; }
    set { name = value; }
  }

  private MyStruct myStruct;

  public MyStruct MyStruct
  {
    get { return myStruct; }
    set { myStruct = value; }
  }

  public void Test() // 用戶端代理不會產生該方法。
  {
    Console.WriteLine(name);
  }
}

[WebService(Namespace = "http://www.rainsts.net/", Description="我的Web服務")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

 public WebService () {
 }

  [WebMethod]
  public MyClass GetMyClass()
  {
    return new MyClass(1, 2, "name1");
  }

  [WebMethod]
  public MyClass[] GetMyClassArray()
  {
    return new MyClass[] { new MyClass(1, 2, "name1"), new MyClass(2, 3, "name2") }; ;
  }
}

有關資料類型的更詳細資料,請查看 MSDN 文檔。

ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxwebservices/html/70567d9f-6e53-42a8-bbd5-aee42b25dd28.htm  

聯繫我們

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