Jason Connery

no time.

.Net : Reusing Types Across Multiple WebServices (Using svcutil.exe)

For a long time I had an issue. I had a server .net project that had multiple .asmx web services in it. In the server project, these different services shared and returned various types: for example a Product class. Just say we have Service1.asmx and Service2.asmx

When consuming these in another solution by adding a service reference in VS2008, The problem was that Visual Studio was creating a name space and types for each service, and not reusing them, so you’d end up with the classes

MyNamespace.Service1.Product
MyNamespace.Service2.Product

and Soap clients like

MyNamespace.Service1.Service1SoapClient
MyNamespace.Service2.Service2SoapClient

Which was annoying, as you would have to be sure to create the correct type of product object depending on what service you were about to use. What I wanted was

SomeNamespace.API.Product 
SomeNamespace.API.Service1SoapClient
SomeNamespace.API.Service2SoapClient

Turning to google, I found many suggestions to add a dll with the orginal type to the project, and that the services would reuse this type. They didn’t. I found tip after tip referring to WCF, or DataContact and various attributes, but that didn’t work either.

I found suggestions to use commands like :

svcutil /r:servertypes.dll /out:Service1.cs http://localhost/Service1.asmx
svcutil /r:servertypes.dll /out:Service2.cs http://localhost/Service2.asmx

which would reuse types. It didn’t. I’m putting it down to not using WCF on the server, but that’s out of my hands.

But I figured it out. And the answer is fairly simple.

svcutil http://localhost/Service1.asmx http://localhost/Service2.asmx /out:api.cs /namespace:\*,SomeNamespace.API

Now, when the code is generated, you’ll get a error that Product is already defined, but it’s more of a warning. But because the output is combined, it works! Unlike calling it once for each service. Perhaps I’ll look at newer solutions for newer projects, but thankfully, the annoyance is less for now.