如何将接口设置为 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.IBillerConnectAPIStatus。
描述:当前Web请求执行过程中发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.NotSupportedException:无法序列化接口 Billerconnect_BillerApp_Interfaces.IBillerConnectAPIStatus。
我已经在实现该接口的类上应用了 [Serializable] 属性,因为我知道我无法序列化接口。
I am using an interface as a return type of a web method in a webservice.
[WebMethod]
//[XmlInclude(typeof(BillerConnectAPIStatus))]
public IBillerConnectAPIStatus PerformInquiry()
{
BillerConnectAPIStatus oBillerConnectApitStatue = new BillerConnectAPIStatus();
return oBillerConnectApitStatue;
}
The Interface is :
public interface IBillerConnectAPIStatus
{
[XmlAttribute]
string Description { get; set; }
[XmlAttribute]
int Status { get; set; }
}
The class that implements the interface is :
[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();
}
}
But at run time it gives an error that is:
Cannot serialize interface Billerconnect_BillerApp_Interfaces.IBillerConnectAPIStatus.
Description: An unhandled exception occurred during the execution of the current web >request. Please review the stack trace for more information about the error and where it >originated in the code.
Exception Details: System.NotSupportedException: Cannot serialize interface Billerconnect_BillerApp_Interfaces.IBillerConnectAPIStatus.
I have applied a [Serializable] attribute on the class that implements the interface as i know i can not serialize an interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法返回接口,因为您无法使用 XML 序列化来序列化接口。
You cannot return an interface because you cannot serialize an interface with XML Serialization.