WEB Services Using polymorphism methods _ Practical Tips

Source: Internet
Author: User
Tags serialization wsdl

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.

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.