在 Web 服务上下文中,我有以下继承自 Mammal 类的类。 Mammal 类在代理中定义。我无法更改该类的定义。因为需要在客户端给Mammal类添加一些方法,所以继承了Mammal创建了Giraffe。
namespace TestApplication
{
public class Giraffe : Mammal
{
public Giraffe()
{
}
}
}
当我调用需要 Mammal 类型对象的 WebMethod 时,出现以下异常,告诉我 Giraffe 不是预期对象。
Error: System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type Giraffe was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentRequestAuthorization.Write6_Tender(String n, String ns, Tender o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentRequestAuthorization.Write12_PaymentRequestAuthorization(String n, String ns, PaymentRequestAuthorization o, Boolean isNullable, Boolean needType)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterPaymentRequestAuthorization.Write13_PaymentRequestAuthorization(Object o)
--- End of inner exception stack trace ---
有解决办法吗?我真的不能添加 XmlInclude...
最佳答案
要添加方法,您应该使用分部类。尝试添加第二个类文件(在正确的命名空间等中):
partial class Mammal {
public void ExtraMethod() {...}
}
partial
关键字告诉编译器允许将类拆分为多个文件——代码生成场景的理想选择。但是,它确实要求在每个文件中使用 partial
关键字;成员只是简单组合(C# 3.0 中的部分方法除外,它们有自己的微妙行为)。我希望 wsdl.exe(或其他)包含这个!
关于c# - WebService、WebMethod 和继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/609152/