In the Web Services method, a specific type of parameter is often used. This parameter is generally a data object. ASP. NET web services can use polymorphism in Web Services methods by declaring xmlincludeattribute.
Xmlincludeattribute allows xmlserializer to identify the type when serializing a fire deserialization object. When xmlincludeattribute is applied, you must 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 object types.
First, define the basic class vehicle and the derived class car:
1: public abstract class Vehicle
2: {
3: public string LicenseNumber{get;set;}
4: public DateTime MakeTime { get; set; }
5: }
6:
7: public class Car : Vehicle
8: {
9: public int DoorNum { get; set; }
10: }
11:
Define the web method of addvehicle and declare xmlinclude to add references to the namespace system. xml. serialization:
1: [WebMethod]
2: [XmlInclude(typeof(Car))]
3: public void AddVehicle(Vehicle vehicle)
4: {
5:
6: }
7:
View the generated WSDL. WSDL uses the base attribute of extension to describe car inheritance vechicle.
View the reference. CS file generated by referencing Web services. The vehicle class will have the xmlincludeattribute declaration:
1: [System.Xml.Serialization.XmlIncludeAttribute(typeof(Car))]
2: [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")]
3: [System.SerializableAttribute()]
4: [System.Diagnostics.DebuggerStepThroughAttribute()]
5: [System.ComponentModel.DesignerCategoryAttribute("code")]
6: [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
7: public abstract partial class Vehicle : object
8:
Client test code:
1: static void Main(string[] args)
2: {
3: localhost.WebService1SoapClient c = new localhost.WebService1SoapClient();
4: localhost.Car car = new localhost.Car() {
5: LicenseNumber="0001",
6: MakeTime=DateTime.Now,
7: DoorNum=2
8: };
9: c.AddVehicle(car);
10: }
11:
You can view the passed parameters in the addvehicle method of Web Services:
Web services can support polymorphism, but it is only allowed to be used when web services can be directly referenced to generate code that can be serialized. Other clients still have to be used at a fraction of the cost.