Adding Web References at Run Time

I'm working on a program and need to be able to add web references at runtime and haven't been able to find anything that would let me do this. Basically this is the way the program will work:

1. Allow user to enter asmx URL.

2. During runtime process the web service so it is accessible.

3. For now lets just say I want to print out all of the methods available from the Web Service.

I've found examples of the AddWebReference method using an Add-In but can't seem to find anything explaining how to use this from a c# program.

Any ideas or thoughts would be helpful.

- Chris



Answer this question

Adding Web References at Run Time

  • jsedlak

    What happens when you add a webreference in Visual Studio is completely different from what happens when just adding a reference to an assembly: A proxy-class gets generated, and when building the project, this class will get compiled with your code.
    The tool used by VS to accomplish this is wsdl.exe [1], but if you're trying to reproduce the behavior you get from Visual Studio at runtime, it will involve, at the least, some code-compilation at runtime.

    1 - http://msdn.microsoft.com/library/en-us/cptools/html/cpgrfwebservicesdescriptionlanguagetoolwsdlexe.asp frame=true


  • Sakil

    Thanks Ernst. I figured I'd end up having to do some pretty manual stuff (either compiling at runtime or parsing the WSDL file myself).
  • NeederOfVBHelp

    Yeah, I actually came across the same problem a couple years ago, and ended up manually parsing the WSDL to get access to the service methods/inputs. In retrospect though, it might be easier to dynamically compile the proxy class, since it'd be much easier to deal with complex types, and involve much less XML parsing.

    Is there a solution for this built into WCF I'd think that binding to a web service at runtime would be a fairly common request.

  • Rubal Jain

    I know that I could always go to the WSDL and look through the xml myself, but I'd ideally not have to do the processing. I don't know if 2.0/3.0 has the capability to dynamically add web references but thats what I want to do here.
  • oaix

    WSDL describes the methods and document-types utilised/exposed by a webservice, and you can obtain the WSDL for most asmx webservices (and most standard-based webservices) by querying <webserviceurl.asmx> WSDL.

    Read more about WSDL here.



  • Adding Web References at Run Time