Monday, December 1, 2014

Adding SOAP prefix to ASMX service using SOAP extensions

Just a preamble here I received the requirement from a third party vendor to add a specific prefix to my soap response. Even know in XML these two formats are equal.

I was required to add it regardless because I assume they were parsing this manually with java or some other client. I came upon these two articles by one of my favorite developers/bloggers Scott Hanselman really the basis of everything I've done here so here is credit where it is do.

Modifying the namespace PREFIX of the root node of the body of a SOAP Web Services Response....whew!
[XmlRootForcePrefix(Prefix='foo')

He apparently ran into the same exact issues with a client who was not satisfied with the soap response he was giving them even know it was exactly the same as the one they were requesting. Anyone that makes you add a prefix to your soap response is probably doing something terribly wrong at the risk of my soul going to hell I was not about to email Scott to confirm the fact I am a scrub and indeed was in need of a fix I did some research and was able to create my own implementation. ***WARNING DO NOT USE UNLESS YOU ARE BETWEEN A ROCK AND A HARD PLACE GO WCF INSTEAD***

First you will want to register the soap extension in your web.config file under the system.web tag as follows:

Just replace the items accordingly and know your assembly file name is most likely your project name if your project is named test then test is your most likely candidate.

Here is the code and once again credit to Scott Hanselman because I used another article he wrote for the implementation of removing white spaces to write this extension with some minor changes in naming convention and the addition of serialization to the xmldocument object and then writing it back to the response stream.

Last of all just add the attribute to your webmethods that you would like to use this prefix on. [Prefix] also make sure you are referencing System.ComponentModel.DataAnnotations

Hopefully this helps some poor guy out there that has been put in a hard spot like myself last week. I could not find anywhere this information was in one place and was accessible.