In a Web services method, a parameter of a specific type is often used, which is generally a data object. asp.net Web services can implement polymorphism in Web services methods by declaring XmlIncludeAttribute.
XmlIncludeAttribute allows XmlSerializer to recognize types when serializing an object in a serialized fire. When you apply XmlIncludeAttribute, you specify the type of the derived class. After XmlSerializer serializes objects that contain both the base class and the derived class, it can recognize two types of objects.
First define the base class vehicle and the derived class car:
Public abstract class Vehicle
{public
string Licensenumber{get;set;}
Public DateTime maketime {get; set;}
}
public class Car:vehicle
{public
int doornum {get; set;}
}
Defines a addvehicle Web method that declares that XmlInclude needs to add a reference to a namespace System.Xml.Serialization:
[WebMethod]
[XmlInclude (typeof (Car))]
public void Addvehicle (Vehicle Vehicle)
{
}
View the generated wsdl,wsdl use the extension base property to describe the car inheritance vechicle.
To view the Reference.cs files that are generated by referencing Web services, the vehicle class will have XmlIncludeAttribute declarations:
[System.Xml.Serialization.XmlIncludeAttribute (typeof (Car))]
[System.CodeDom.Compiler.GeneratedCodeAttribute ("System.Xml", "4.0.30319.1")]
[System.SerializableAttribute ()]
[System.Diagnostics.DebuggerStepThroughAttribute ()]
[System.ComponentModel.DesignerCategoryAttribute ("code")]
[System.Xml.Serialization.XmlTypeAttribute (namespace= "http://tempuri.org/")]
Public abstract Partial class Vehicle:object
Client Test Code:
static void Main (string[] args)
{
localhost. Webservice1soapclient c = new localhost. Webservice1soapclient ();
localhost. Car car = new localhost. Car () {
licensenumber= ' 0001 ',
maketime=datetime.now,
doornum=2
c.addvehicle (car);
}
The Addvehicle method of Web Services allows you to view the parameters passed:
Web services can support polymorphism, but it can be a struggle to use on other clients simply by limiting the ability to generate serializable code when you can directly reference Web services.
The above is the entire content of this article, I hope to help you learn.