DJ.
DJ.

Reputation: 131

How to call a webservice programmatically in asp.net

How to call webservice programmatically in asp.net without using add web reference? My webservice url keeps on changing. Hence i need to capture the url at runtime and display the results. Please advice.

Upvotes: 13

Views: 15997

Answers (4)

Adeel
Adeel

Reputation: 19228

you need to do the following steps.

PreReq : First of all, you know the URL of web service.

Solution: use wsdl.exe to create a proxy class and than compile it.

wsdl /out:myProxyClass.cs http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL

(there are other switches available for wsdl. For Example to generate VB class, you need to add switch /language:VB)

Once your proxy class is generated you can easily consume in code.

MyProxyClass objService = new MyProxyClass();
DateTime time = objService.GetServerTime(); //Suppose service has  method getServerTime

Upvotes: 4

Dexter
Dexter

Reputation: 18452

You can change the URL of a web-reference at runtime (provided that the new address is hosting a service with the same schema that you originally used to create the reference):

MyWebService ms = new MyWebService();
ms.Url = "http://example.com/webservice.asmx";
ms.MyWebMethod();

Web References are definitely the way to go - whilst the classes that are created by the web reference are usually pretty heavy, all that strong typing makes it well worth your while.

Upvotes: 18

Philip Fourie
Philip Fourie

Reputation: 116827

You can specify the end-point URL as part of the constructor of your client-side proxy class.

If you don't need to specify it during runtime then it can also be set in your web.config file.

Upvotes: 2

jaysonragasa
jaysonragasa

Reputation: 1076

Where are you trying to call the service and where the service file is located?

If the service is located on the same site. Why not just instantiate the class name from the service. Or just create a separate class and use interface

Upvotes: 0

Related Questions