Combining multiple WSDLs for consumption in a C# client project

So I added a WebReference for nine different web services. These were all web services for the same product. I will call them from the same client code. So it makes sense to consume them all. Each service got it’s own namespace. Immediately there were problems. Multiple web services exposed the same object. Each instance of the object was in a different namespace. This created a bunch of ambiguous reference errors. Fixing those errors made a mess of the code as I then had to include the namespaces when calling the objects or else have object using statements to determine which object to use in which namespace.

The I got smart and researched combining WSDLs. I found this MSDN site: Web Services Description Language Tool (Wsdl.exe)

Turns out that Visual Studio comes with a WSDL.exe. It allows for easily combining multiple WSDLs into one large auto-generated code file. It also has a nice parameter called sharetypes. I bet you can guess what sharetypes does.

Step 1 – Create a wsdl parameters file

Here is an exmaple of mine.

  1. I left the settings from the MSN example for nologo, parsableerrors, and sharetypes.
    Note: Setting sharetypes to true is the feature I most needed.
  2. Set it to get the URL from the web.config or app.config.
  3. Set the namespace.
  4. Add a document for each wsdl file.
  5. Set out file.
    <wsdlParameters xmlns="">

Step 2 – Run wsdl.exe

  1. Open a Developer Command Prompt.
  2. Run this command:
    wsdl.exe /Parameters:FnoSharpWsdls.xml

Now you have a single Reference.cs file that includes code for all your WSDLs.

Step 3 – Update your Visual Studio Project

  1. Delete all your web references.
  2. Add the Reference.cs to your project
  3. Fix your namespaces.

Step 4 – Update URL to use an appSetting

Having a hard coded URL in the code is not a good idea. The Reference.cs file you created will have static URLs. Unfortunately the wsdl.exe doesn’t seem to support doing this for multiple urls. So you should manually replace them.

  1. Add a variable to your appSettings in your web.config or app.config.
    <?xml version="1.0" encoding="utf-8" ?>
        <add key="FnoSharpHost" value="http://MyServer:8888"/>
      <!-- Other config stuff -->
  2. Find and replace all the lines that call your hard coded URL. They look like this. I had 9 WSDLs so I had 9 of these to replace.
        public ActivationService() {
            this.Url = "http://MyServer:8888/flexnet/services/ActivationService";

    Here is your find and replace string:

       Find:  this.Url = "http://MyServer:8888
    Replace:  this.Url = ConfigurationManager.AppSettings["FnoSharpHost"] + "

    Your methods should now look like this:

        public ActivationService() {
            this.Url = ConfigurationManager.AppSettings["FnoSharpHost"] + "/flexnet/services/ActivationService";

Your done. You now have all your WSDLs combined into one Reference.cs file.

Leave a Reply

How to post code in comments?