Web services use polymorphism (xmlinclude)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.