Friday, January 27, 2006
« New Toy - PSP | Main | Rating Control v1.5 Released »

I had an interesting dilemma today involving web services which I'd like to share.  One might argue that the vast majority of web services out there are well defined.  By that, I mean that they have expected parameters (inputs) and return expected, predefined, structured results (outputs).  These inputs and outputs (i.e. Requests and Responses) are defined within the WSDL associated with the web services.  Generally speaking you can create a proxy to a web service either in Visual Studio via the 'Add Web Reference...' dialog or manually via the WSDL.exe utility and feel pretty confident that that is all you have to do.  Beyond that, you generally don't need to worry whether or not you're actually dealing with a web service because it's just a function call and you get the expected results serialized to you.

I have a different situation, however.  Allow me to give a little background that might help illuminate the issue at hand.

I have an application that I'm developing that interfaces with a web service that has a single point of entry called 'GetEntity'.  This method will return one of 91 (yes 91!) object types depending on the parameters provided.  Of these 91 objects I really only care about 4 (so far).  Each of these objects ultimately derives from a class called 'Entity'.  None of this is the issue - I in fact LOVE the web service as it's defined - it's VERY cool.  Here's the issue:  Each Entity has a predefined set of properties, but users of the system can extend the business entities as they see fit (even create their own, custom entities).  One company may add a 'TotalWeight' field to a Quote object for instance, where another may add a 'LineNumber' field to a QuoteDetail object.

This makes it a little more difficult for me who distributes an application to the disparate users of the system.  I have no control over the return schema, but my application needs to potentially update any of these fields based on a completely flexible and arbitrary set of rules and formulas.  It is pretty much ruled out that I am unable to use WSDL.exe to create the proxy class and schemas because the schemas will differ from client to client.  So here's the dilemma, how do I take the result of an XML Web Service call (over which I can exert ZERO control) and still access the fields for which my schema has no definition?

I might try to cheat and alter the proxy class's method that calls the Web Method to return a different type.  If I could just retrieve the XML from the Response I could parse it out and read the custom properties into a collection of some sort.  I might, therefore, try to change the method to return a string rather than an Entity...or an XmlDocument.  There are issues with this however.

Suppose I have the following:

Sample web method result

<soap:Envelope mlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetEntityResult xmlns="
http://schemas.devstone.com/WebServices" xsi:type="person">
         <id>{B14C25BC-AB83-DA11-93D2-0003FF770FD4}</id>
         <name>R. Aaron Zupancic</name>
         <hairColor>Purple</hairColor>
      </GetEntityResult>
   </soap:Body>
</soap:Envelope>

Sample proxy class method to invoke the web method

[SoapDocumentMethod("http://schemas.devstone.com/WebServices/GetEntity", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Bar)]
[return: XmlElement(Namespace="
http://schemas.devstone.com/WebServices")]
public Entity GetEntity([XmlElement(Namespace="
http://schemas.devstone.com/WebServices")] Guid id, [XmlElement(Namespace="http://schemas.devstone.com/WebServices")] string entityType) {
   object[] results = this.Invoke("GetEntity", new object[] { id, entityType });
   return (Entity)results[0];
}

Sample classes

[XmlInclude(typeof(person))]
public abstract class Entity { }

[XmlType(Namespace="http://www.devstone.com/WebServices")]
public class person : Entity {
   public int    id;
   public string name;
   public string hairColor;
}

This looks pretty typical.  However, if a customer were to customize the person entity schema to include an 'age' field, my proxy definition would not match and I'd not be able to retrieve the age property after the response is deserialized into a person object...my proxy class would be useless except for these known fields, which is of little use to me.  So, as mentioned above I might change the proxy method to return a string rather than an Entity.  If I do that I get the value of the first Xml element (e.g. '{B14C25BC-AB83-DA11-93D2-0003FF770FD4}').  Hmm, well make an XmlDocument.  If I do that, I get the first result element (e.g. '<id>{B14C25BC-AB83-DA11-93D2-0003FF770FD4}</id>').  The result is pretty flat...it's a series of nodes and that doesn't constitute a valid document - a valid document has only one root node.

My solution, therefore is to make it so that before the result gets back to my application for deserialization each of the nodes is placed within a parent node.  However, as mentioned above, I have no control over the web service nor its WSDL.

To make this work, this I create a custom SoapExtension-derived class.  This class will intercept and rewrite the XML as it flows back from the server.  My class might resemble the following:

public sealed class EntityTransformExtension : SoapExtension {
   private Stream _origStream;
   private String _customStream;
  
   public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute) { return null; }
   public override object GetInitializer(Type serviceType) { return null; }
   public override void Initialize(object initializer) { }
  
  
   public override Stream ChainStream(Stream stream) {
      // cache off the original stream provided
      _origStream = stream;
      // create a custom stream and return it
      // this is the stream to which the request will be written first
      // and the stream to which the response will be written last
      _customStream = new MemoryStream();
      return _customStream;
   }
  
  
   public override void ProcessMessage(SoapMessage message) {
      switch ( message.Stage ) {
         case SoapMessageStage.AfterSerialize:
            // the request message is about to be sent to the web server
            // copy the information from our custom stream to the original to be
            // sent to the server

            _customStream.Position = 0;
            copyStream(_customStream, _origStream);
            break;
           
         case SoapMessageStage.BeforeDeserialize:
            // the response message just arrived from the web server
            // delegate control off to a helper method to modify the XML, inserting
            // each of the child XML elements into a new parent node.
            rewriteResponse();
            break;
           
         case SoapMessageStage.BeforeSerialize:
         case SoapMessageStage.AfterDeserialize:
         default:
            // we're not concerned with these message stages as they don't apply to what we're doing client-side.
            break;
      }
   }
  
  
   private void copyStream(Stream src, Stream dst) {
      // UPDATE: I inadvertently had the TextReader and TextWriter in using blocks
      // while this worked with this particular example, it's not generally a good
      // idea to do it in the SoapExtension because it has the nasty side-effect
      // of closing down the stream being read from/written to.
      TextReader reader = new StreamReader(src);
      TextWriter writer = new StreamWriter(dst);
      writer.WriteLine(reader.ReadToEnd());
      writer.Flush();
   }
  
  
   private void rewriteResponse() {
      // rather than load the document directly into an XmlDocument, or load it in an XPathDocument for an XSLT
      // transformation (which are both possible), simply read the source XML in and spit it out with a writer
      // to the destination stream with our own custom transformation (which simply takes all the returned nodes
      // and embeds them into another node).

      _customStream.Position = 0;
      XmlTextReader reader = new XmlTextReader(_origStream);
      XmlTextWriter writer = new XmlTextWriter(_customStream, UTF8Encoding.UTF8);
      writer.WriterStartDocument();
      while ( reader.Read() ) {
         if ( reader.IsStartElement("GetEntityResult") ) {
            writeNode(reader, writer);
            writer.WriteStartElement(reader.Prefix, "entity", reader.NamespaceURI);
         }
         else if ( XmlNodeType.EndElement == reader.NodeType && "GetEntityResult" == reader.Name ) {
            writer.WriteEndElement();
            writeNode(reader, writer);
         }
         else
            writeNode(reader, writer);
      }
      writer.Flush();
      _customStream.Position = 0;
   }
  
  
   private void writeNode(XmlReader reader, XmlWriter writer) {
      switch ( reader.NodeType ) {
         case XmlNodeType.Element:
            writer.WriteStartElement(reader.Prefix, reader.Localname, reader.NamespaceURI);
            writer.WriteAttributes(reader, true);
            if ( reader.IsEmptyElement ) writer.WriteEndElement();
            break;
           
         case XmlNodeType.Text:
            writer.WriteString(reader.Value);
            break;
           
         case XmlNodeText.EndElement:
            writer.WriteFullEndElement();
            break;
           
         default:
            // there are many more, but this covers the very basic, and works well enough for this
            // web service for now.
    
      }
   }
}

Once the SoapExtension class is created, I simply need to create a SoapExtensionAttribute that I can apply to my web method caller function that inserts the SoapExtension onto the stack.  Once in place I should get back an altered result set that I can then parse independent of the Entity class as defined above.

[AttributeUsage(AttributeTargets.Method)]
public sealed class EntityTransformationExtensionAttribute : SoapExtensionAttribute {
   private int _priority = 1;
  
   public override int Priority {
      get { return _priority; }
      set { _priority = value; }
   }
  
  
   public override Type ExtensionType {
      get { return typeof(EntityTransformationExtension); }
   }
}

I then alter my proxy class to return an XmlDocument and I tack this new attribute to it as follows:

[EntityTransformationExtension()]
[SoapDocumentMethod("
http://schemas.devstone.com/WebServices/GetEntity", Use=SoapBindingUse.Literal, ParameterStyle=SoapParameterStyle.Bar)]
[return: XmlElement(Namespace="
http://schemas.devstone.com/WebServices")]
public XmlDocument GetEntity([XmlElement(Namespace="
http://schemas.devstone.com/WebServices")] Guid id, [XmlElement(Namespace="http://schemas.devstone.com/WebServices")] string entityType) {
   object[] results = this.Invoke("GetEntity", new object[] { id, entityType });
   return (XmlDocument)results[0];
}

My resulting XML now has a new structure:

<soap:Envelope mlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetEntityResult xmlns="
http://schemas.devstone.com/WebServices" xsi:type="person">
         <entity>
            <id>{B14C25BC-AB83-DA11-93D2-0003FF770FD4}</id>
            <name>R. Aaron Zupancic</name>
            <hairColor>Purple</hairColor>
         </entity>
      </GetEntityResult>
   </soap:Body>
</soap:Envelope>

This slick little trick allows me to take control of the results of the web method (without touching the web service) and adapt my application to its ever-varying resultset.

Friday, January 27, 2006 7:32:00 PM (Mountain Standard Time, UTC-07:00)  #    Disclaimer  |  Comments [2]  |  Trackback