如何将接口设置为Web服务中Web方法的返回类型
问题描述:
我正在使用接口作为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]属性,因为我知道我不能序列化一个接口。
答
由于无法使用XML序列化序列化接口,因此无法返回接口。
我可以使用Abstract类来实现相同的功能吗?正如我已经知道,我无法使用接口 – Riky 2012-02-13 14:04:32
Web服务通过线路发送XML。 XML不是抽象的,它是具体的,所以,不,也不是抽象类。参见[基础:Web服务如何工作](http://johnwsaunders3.wordpress.com/2008/09/23/basics-how-web-services-work/)。 – 2012-02-13 15:10:28