Creating an Open PurchaseOrder Schema

Defining the AcceptPO Web Method

Now that I have defined the PurchaseOrder type, I need to define the Web method that will accept the purchase order. The body of the incoming SOAP message will contain an instance of the PurchaseOrder XML datatype. It needs to be decorated with the properly initialized SoapDocumentMethod attribute, as discussed in the previous chapter.

The root element of the body of the SOAP document must be named PurchaseOrder. By default, XML serialization will give the root element the same name as the input parameter of the Web method. Calling the parameter PurchaseOrder would create a name conflict with my class definition, so you must override how parameters are serialized using one of two approaches.

The first approach is to use the XmlElement attribute to decorate the parameters of a Web method to control how they will be serialized. The following code defines a Web method that accepts an instance of the PurchaseOrder XML datatype:

[WebMethod] [SoapDocumentMethod(ParameterStyle=SoapParameterStyle.Bare)] public void AcceptPO([XmlElement("PurchaseOrder", IsNullable=false)]  PurchaseOrder param1) {     // Implementation... }

The XmlAttribute attribute can be applied to Web method parameters as well, but these parameters must be parameters of “wrapped” methods.

Because the PurchaseOrder class will be serialized as the root of the SOAP message body, I have a second option in addition to using the XmlElement attribute: I can decorate the class with the XmlRoot attribute. This attribute controls how a .NET type is serialized as the root of the document. Here is an example:

[XmlRoot("PurchaseOrder", IsNullable=false)] [XmlInclude(typeof(CommentedPurchaseOrder))] public class PurchaseOrder {     // Definitions... }

Other attributes that you can apply to Web method parameters include XmlAnyElement, XmlAnyAttribute, and XmlText.



Building XML Web Services for the Microsoft  .NET Platform
Building XML Web Services for the Microsoft .NET Platform
ISBN: 0735614067
EAN: 2147483647
Year: 2002
Pages: 94
Authors: Scott Short

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net