New tools to solve an old (.NET Web Service) problem (and an apology)

I ran into an interesting problem today…  One of my clients is converting a Windows app’s database and business logic to web services.  The transition has been surprisingly smooth.  Well, smooth up until today.  Since we are using the old, .asmx web services, passing user defined classes via the parameter list is almost a black art.  We were trying to pass a class, BusinessObjectFoo, from the Windows application to the web service.  The web service has a parameter that accepts BusinessObjectFoo, and both the client and the server reference the same library that contains the definition for BusinessObjectFoo.  This should be a no-brainer, and just work, right?  Unfortunately, it doesn’t.  Microsoft abandoned .asmx improvements in .NET 3.0, and left some rather serious holes in the functionality of .asmx web services.  The ability to pass a user defined class via the web service parameters happened to be one of them.

The crux of the problem is that when the client application pulls in the service reference, Visual Studio creates a new definition of the class from the web service’s WSDL rather than matching the class up with the libraries.  (Apparently, this works correctly with WCF services).  We tried several different solutions to try to allow passing the object via the web service, but no luck.

Enter my apology… in reviewing code for my normal job, I’d run across something that had been added to the code base with no explanation.  That code was for Automapper.  I got aggravated at the developer for throwing another ‘new toy’ into our project.  I did do a little reading on understanding Automapper, but didn’t see why it had been added. After today, I apologize to those devs, this one looks pretty helpful.

Back to the story 🙂

What need to be done was to move the data from the normal, shared class into one of the web service / auto generated classes.  The first try was to use an object copy routine.  That didn’t go so well.  After that, I realized that I should try the AutoMapper NuGet package, as both objects had the same field structure / interface, just different implementations.  That’s when reading up a bit on Automapper, and trying it really saved the day.  Instead of writing a big, honkin’ copy class between my shared object and the Web Service object, I just mapped the two with Automapper.  Then, it was just a simple map call to populate the correct object with all of it’s data.

VERY easy, and saved a ton of coding!