WebService, WebMethod and Inheritance

In the context of a web service, I have the following class that inherits from the Mammal class. The Mammal class is defined in the proxy. I cannot change the definition of this class. Since I need to add some methods to the client-side Mammal class, I inherited Mammal and created Giraffe.

namespace TestApplication
{  
    public class Giraffe : Mammal
    {
        public Giraffe()
        {
        }
    }
}

When I call WebMethod, which expects an object of type Mammal, I get the following exception telling me that Giraffe is not expected.

  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 ---

Is there a workaround? I can not add XmlInclude ...

+3
source share
4 answers

, . ( ..):

partial class Mammal {
    public void ExtraMethod() {...}
}

partial - . partial ; ( # 3.0, ). , wsdl.exe( - ) !

+2

, , .

public static class MammalExt {
public static void ExtraMethod(this Mammal mammal) {...}}

, - , .

+2

XmlInclude. . , , .

, , Giraffe IXmlSerializable, .

+1

. -, Visual Studio WSDL Mammal. , .

, - MyWebService. MyWebService.Mammal. , , , , :

namespace MyWebService {
    public partial class Mammal {
        public void ExtraMethod() { ... }
    }
}

When you call a method in a web service that returns Mammal, the web link code will create a new instance of your "improved" mammalian class and copy the values ​​from xml to wire into the instance.

+1
source

Source: https://habr.com/ru/post/1704178/


All Articles