2012-02-13 36 views
0

我正在使用接口作爲Web服務中Web方法的返回類型。如何將接口設置爲Web服務中Web方法的返回類型

[WebMethod] 
    //[XmlInclude(typeof(BillerConnectAPIStatus))] 
    public IBillerConnectAPIStatus PerformInquiry() 
    { 
     BillerConnectAPIStatus oBillerConnectApitStatue = new BillerConnectAPIStatus(); 
     return oBillerConnectApitStatue; 
    } 

接口是:

public interface IBillerConnectAPIStatus 
{ 
    [XmlAttribute] 
    string Description { get; set; } 
    [XmlAttribute] 
    int Status { get; set; } 
} 

實現該接口的類是:

[Serializable] 
public class BillerConnectAPIStatus : IBillerConnectAPIStatus 
{ 
    string _description; 
    int _status; 
//[XmlElement] 
    [XmlAttribute] 
    public string Description 
    { 
     get 
     { 
      return _description; 
     } 
     set 
     { 
      _description = value; 
     } 
    } 

//[XmlElement] 
    [XmlAttribute] 
    public int Status 
    { 
     get 
     { 
      return _status; 
     } 
     set 
     { 
      _status = value; 
     } 
    } 

    public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) 
    { 
     throw new NotImplementedException(); 
    } 
} 

但是在運行時它給出了一個錯誤是:

無法序列化Billerconnect_BillerApp_Interfaces.IBillerConnect接口APIStatus。

說明:執行當前web>請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及它在源代碼中的位置。

異常詳細信息:System.NotSupportedException:無法序列化接口Billerconnect_BillerApp_Interfaces.IBillerConnectAPIStatus。

我已經在實現接口的類上應用了一個[Serializable]屬性,因爲我知道我不能序列化一個接口。

回答

3

由於無法使用XML序列化序列化接口,因此無法返回接口。

+0

我可以使用Abstract類來實現相同的功能嗎?正如我已經知道,我無法使用接口 – Riky 2012-02-13 14:04:32

+0

Web服務通過線路發送XML。 XML不是抽象的,它是具體的,所以,不,也不是抽象類。參見[基礎:Web服務如何工作](http://johnwsaunders3.wordpress.com/2008/09/23/basics-how-web-services-work/)。 – 2012-02-13 15:10:28