test

Page 220

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.

Server-Side Validation Throughout this chapter, I create a definition for a strongly typed PurchaseOrder XML datatype. I also create a strongly typed interface for the AcceptPO Web method. You might be surprised by the fact that the ASP.NET runtime will not validate incoming requests against the schema I have so painstakingly created. For example, the following SOAP request message will be parsed by the ASP.NET runtime without throwing an exception. <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap=http://schemas.xmlsoap.org/soap/envelope"> <soap:Body> </soap:Body> </soap:Envelope> As you can see, the elements and attributes that are not allowed to be null are not contained within the preceding document. However, the document will be accepted and deserialized by the ASP.NET runtime. This is an obvious problem because you cannot count on the ASP.NET runtime to perform any validation on your behalf.

220


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.