locked
Programmatically Generate a SOAP message from a WSDL for a specific method in asp.net

    Question

  • Hi,

        I have a WSDL for a web service. It could be a web service built using any technology, not just asmx. From this WSDL, I am able to get the methods defined. Now, for a specific web method, I want to get the SOAP envelope as xml programmatically, so that the user can simply replace or provide the required values in the same XML and I can invoke the method using HttpWebRequest or WebRequest. I having been trying for a while without much success. I did try SoapExtension, but it gives the XML for the entire WSDL and not the SOAP envelope for a specific method from the WSDL. Please help.

    Thursday, October 27, 2011 2:44 AM

All replies

  • Why do you want to do it this way? Why not just use "Add Service Reference" and let the clients call methods on the proxy class?

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Friday, October 28, 2011 2:40 PM
  • I have been asked to build a tool, which will take the WSDL url as the input and show all the methods defined in the service. Once the user selects a method, I need to show the SOAP envelope, so that the user can provide the required input parameters and invoke the method on the service. The tool has to work for any web service built on any technology. I have tried with ServiceDescription and Reflection also. But I am unable to get the raw SOAP envelope as xml for any method within a service. Please help.
    Saturday, October 29, 2011 11:37 PM
  • Don't re-invent the wheel. Use a tool that has already been built, and which has more features for free than your tool will ever have: http://soapui.org/.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Sunday, October 30, 2011 12:43 AM
  • I already know about this. I was wondering how to use this (tool) through my sharepoint 2010 web part. Also, I thought, there will be some way to achieve this through existing .net APIs.
    Monday, October 31, 2011 3:33 AM
  • I have a suggested approach, though I don't have the details handy.

     

    1. Don't use ASMX technology at all. It's out of date and will limit you unnecessarily.
    2. Find out which .NET code is used by svcutil.exe to generate a WCF client proxy class. I believe you should look into the System.ServiceModel.something.ServiceDescription namespace.
    3. Use this to generate proxy classes for the WSDL in-memory.
    4. Compile the classes in-memory, then load the resulting assembly
    5. Use reflection to look at the signature of the generated methods
    6. Use the information gathered from reflection to build input and output forms
    7. Display the input form to the user, and call the generated proxy method
    8. Take the results from that method and display to the user in the output form

    This will put the hard work on .NET. Among other things, it will allow you to consume a much wider set of services than ASMX would have done - you could not have called services which use WS-Security, for instance.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Monday, October 31, 2011 4:03 PM
  • Thanks a lot for the prompt response, John. I am already doing this in my code. The only thing I am not able to achieve is the following.

    Lets say, a method in a web service takes a complex object as input parameter. In this case, how can I capture the input parameter? Moreover, I need to get the SOAP envelope for every method. Please help me to achieve this using .Net APIs.

    Monday, October 31, 2011 5:08 PM
  • More detail fell into my lap. See http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicecontractgenerator.aspx.

    What problem are you having with the complex types? Also, what is it that you feel you need the actual SOAP envelope?


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Monday, October 31, 2011 8:16 PM
  • Hi John,

    Thanks for the help. I have been asked to build a Web Service testing tool, which will simply accept the WS (WSDL) url. This web service could be built using any technology (Java, ASMX, WCF and so on). I need to find out all the methods exposed by this service and show them in a drop down. Then the user will select a method from the drop down, and as soon as this happens, the input parameters for this method should be listed in a grid view control. This will be possible only if the input parameters are of simple types like int, string and so on. There are some methods, exposed by some services, which take custom objects as inputs. These custom objects in turn will have properties, which again, may be complex types, like List, Array and so on. In this scenario, I can only show the SOAP envelope template for that method, so that the user can simply replace / insert values, wherever required and then click on a Test button, to test the method and get the output. Hope this explains the purpose very clearly.  Now, to generate, the SOAP envelope template, I am looking for help. I referred the above link for the APIs and I am already using it, along with Code Dom and Reflection to get the required  things, except for the SOAP envelope. Now, this is where I need help.

    Tuesday, November 01, 2011 12:19 PM
  • Why not create an input form for the "custom type" and show that?

     

    Among other things, the "SOAP Envelope" for some services is far too complex for you to ever display to a user. Are you seriously expecting a user to edit the XML and get a complex type correct? How will they know how to translate from the complex parameter into correct XML?

    Do you believe that .NET uses a "SOAP Envelope Template", and that all you need to do is get it, then the user will be able to fill it in? That's not the case.

    Also, to reiterate, a service using WS-Security or several of the other WS-* protocols will produce SOAP that is too complex for a human to edit by hand.

     


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, November 01, 2011 2:08 PM
  • I understand your point, John. But the functionality is going to be used by technical users or they will be educated to use it. Assuming this, I prefer to get the SOAP envelope. We will create a template of the SOAP envelope for every method and will educate the user to replace / insert appropriate values in the XML and then invoke the method, something like, what a SOA Client does in FireFox.
    Tuesday, November 01, 2011 2:28 PM
  • Ok, so you're leaving out WS-Security and such? Just basic SOAP 1.1/1.2?

    If that's the case, then you may be able to do something with WCF. I suppose you could create a custom binding that writes out to an XmlWriter.

    There's no way to do this with ASMX technology.

     


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, November 01, 2011 4:21 PM
  • I didn't get this John. Is it possible to achieve something like what SOAP UI does (generating a sample SOAP envelope), in .Net using the existing APIs? If so, can please provide the code sample or the link which can give some pointers?
    Tuesday, November 01, 2011 6:29 PM
  • There is no single API for this. Almost nobody needs to do this. I don't believe that there's any place in ASMX or WCF where a "template" that .NET fills in.

    Does soapUI have an API?


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, November 01, 2011 6:39 PM
  • I am not sure whether SOAP UI has exposed any API. Moreover, its in Java. I am looking to achieve this in .Net. I am not knowledgeable to argue with you. But take a look at the following link. How are we getting the SOAP envelope (Request) in this case?

    http://services.aonaware.com/DictService/DictService.asmx?op=Define

     

    Tuesday, November 01, 2011 6:56 PM
  • Take a look in your C:\Windows\Microsoft.Net\Framework\<version>\Config\DefaultWsdlHelpGenerator.aspx.

     

    Note that it does not handle complex parameters, which is exactly your problem. It also only supports SOAP 1.1/1.2 with no WS-*.

     


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, November 01, 2011 6:59 PM
  • Thanks John. So it looks like there is no direct solution to this. On another note, could you be kind enough to throw some light on what is WS-*? Also, could you kindly provide some sample web service urls where this is implemented?
    Tuesday, November 01, 2011 7:51 PM
  • See http://en.wikipedia.org/wiki/WS-Security and http://en.wikipedia.org/wiki/List_of_Web_service_specifications. I have no idea which public services implement some of these specifications, but the out of the box default for a WCF service is to use WS-Security.

     


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, November 01, 2011 8:11 PM
  • HI.. I have exactly same requirement to generate  the SOAP envelope template from wsdl URL on the fly in .net . Can you please suggest if you have found any solution how to implement this. I am using dynamicProxy provided by wcfSamples to invoke wcf services on the fly ..

    Tuesday, January 17, 2012 5:30 AM
  • Hi Rahul,

    Looks like you have to write your own code to achieve it. You have to parse the WSDL (either using XMLDocument or XDocument(LINQ to XML)) and create your own method to build the SOAP envelop through code. But there is a catch here. If the web service / WCF service, doesn't allow anonymous access or if it doesn't implement BasicHttp Binding, then you won't be able to call the service method, even using the SOAP envelope. Moreover, if the web service / WCF service has complex (object graph) types, then your code needs to take care of it. Also, if you are looking for a very generic solution, I would recommend you to try with at least 4 or 5 different kinds of web services (may be built on other platforms like Java), so that your code is able to generate SOAP envelop successfully from the WSDL. Let me know if you need more info.

    Tuesday, January 17, 2012 4:23 PM
  • You should be able to leverage the ServiceDescription class and related classes.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, January 17, 2012 4:34 PM