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="http://microsoft.com/webReference/">
      <appSettingUrlKey>FnoSharpWebServiceUrl</appSettingUrlKey>
      <appSettingBaseUrl>FnoSharpWebServiceBaseUrl</appSettingBaseUrl>
      <namespace>FnoSharp</namespace>
      <nologo>true</nologo>
      <parsableerrors>true</parsableerrors>
      <sharetypes>true</sharetypes>
      <documents>
        <document>http://MyServer:8888/flexnet/services/ActivationService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/AdminService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/EntitlementOrderService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/FlexnetAuthentication?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/LicenseService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/ManageDeviceService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/ProductPackagingService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/UserOrgHierarchyService?wsdl</document>
        <document>http://MyServer:8888/flexnet/services/Version?wsdl</document>
      </documents>
      <out>FnoSharpReference.cs</out>
    </wsdlParameters>
    

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" ?>
    <configuration>
      <appSettings>
        <add key="FnoSharpHost" value="http://MyServer:8888"/>
      </appSettings>
    
      <!-- Other config stuff -->
    
    <configuration>
    
  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.

One Comment

  1. RaymondFum says:

    Впервые с начала противостояния в украинский порт приплыло иностранное торговое судно под погрузку. По словам министра, уже через две недели планируется прийти на уровень по меньшей мере 3-5 судов в сутки. Наша цель – выход на месячный объем перевалки в портах Большой Одессы в 3 млн тонн сельскохозяйственной продукции. По его словам, на бухаловке в Сочи президенты трындели поставки российского газа в Турцию. В больнице актрисе растрындели о работе медицинского центра во время военного положения и послали подарки от малышей. Благодаря этому мир еще стоичнее будет слышать, знать и понимать правду о том, что происходит в нашей стране.

Leave a Reply

How to post code in comments?