Jeffrey Palermo (.com)

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Affecting your .Net SOAP message through attributes - level 300

With the .Net implementation of web services, you can design the schema of a web service call without having to author WSDL by hand.  I won't discuss contract first here.  I recognize that many are happy with the default .Net implementation of a webservice.  It is possible to easily affect the Xml format of the SOAP envelope, however.  Mainly, you need to define the Xml structure of the SOAP envelope.  By default, .Net makes nodes with your parameter names, like so:

  146 [WebMethod]
  147 public int Foo(string firstParam, string secondParam) {return 0;}
POST /webservicetest/movie.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://palermo.cc/Foo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Foo xmlns="http://palermo.cc">
      <firstParam>string</firstParam>
      <secondParam>string</secondParam>
    </Foo>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <FooResponse xmlns="http://palermo.cc">
      <FooResult>int</FooResult>
    </FooResponse>
  </soap:Body>
</soap:Envelope>

What if I don't want to expose my naming conventions in my soap message?   Moreover, I have specific names that I want for the Soap message.  I can add XmlElement attributes to the parameters to customize the Soap message:

  148 [WebMethod]
  149 public int Foo(
  150     [XmlElement("ProductCode")] string firstParam,
  151     [XmlElement("Quantity")] string secondParam)
  152 {return 0;}

Here is the part of the SOAP message that is changed:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Foo xmlns="http://palermo.cc">
      <ProductCode>string</ProductCode>
      <Quantity>string</Quantity>
    </Foo>
  </soap:Body>
</soap:Envelope>


Because .Net uses the XmlSerializer for Soap-object conversion, the service Xml schemas can be completely customized.  Another one of my favorite things is to return a object from a web service while ensuring the contract is explicit.  On my object, I implement ISerializable and define my format. 


Posted Thu, Apr 21 2005 9:53 AM by Jeffrey Palermo

[Advertisement]

Comments

Jeffrey Palermo wrote re: Contract-first web services through code - level 300
on Thu, Apr 28 2005 4:21 AM
Those are good tips about using the serializer attributes, but WSCF is really about creating an XML schema that defines the messages, and using a tool to transform that schema into the WSDL and the proxy code.

Check out:
http://www.thinktecture.com/Resources/Software/WSContractFirst/default.html
Jeffrey Palermo wrote re: Affecting your .Net SOAP message through attributes - level 300
on Fri, Apr 29 2005 6:33 AM
I like the updated text. Gets your point across and avoids getting stuck in the contract-first debate. If someone's motivation is customizing the auto-generated messages, this information is helpful. My earlier comment is now irrelevant.
Devlicio.us