Web Services

  • Thread starter Maziar Aflatoun
  • Start date
M

Maziar Aflatoun

Hi,

Is there a way to connect to a Web Service dynamically at runtime (Web
Reference URL)? I have always used Visual Studio to create a Web Reference
and then used it in my code. However, that always requires the URL to the
Web Service to be the same on the staging site and the production site. Now
I have two instances of the same application running (Staging site and
Production) and want to store the URL in a file instead. Is there a way to
do this?

ex. Web.Config AppSetting <add key="WebServiceURL"
Value=http://....webserv.asmx>

Thank you
Maz.
 
F

Frisky

First of all, to answer your question, "yes", you sure can set the address
dynamically. And, you get do it a multitude of ways. Basically, when you add
a reference to an existing web service, Visual Studio generates a bunch of
code for you. If you take that code and build it into your object, you have
the makings of your own dynamic call.

However, there is an easier way that simply uses the code Visual Studio has
already generated for you. Suppose you have a web service called test
service, you can set its url with the following code:

TestService service = new TestService();
string url = ConfigurationSettings.AppSettings["ServiceUrl"];
if (url != null && url != string.Empty) {
service.Url = url;
}
string test = service.HelloWorld();
Of course, you need the following in your appConfig section of your
configuration file (and the value would need to be your server url):

<add key="ServiceUrl" value="http://services.myServer.net/TestService.asmx"
/>

Hope this helps...

Frisky
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top