Welcome to MSDN Blogs Sign in | Join | Help

Redirect web services at run time

Well, this is not an advanced topic or anything like it. Actually it's a very simple thing to do. However, every so often I run into someone who is developing and consuming web services and don't know about this.

So here comes a typical scenario: you are developing a multi-tier application and some interactions between your tiers are done through web services. You develop the web service on your dev machine, you then consume it from an application also on your local machine. To do this you add a web reference to your local web service and everything just works. However, you are faced with the following problem (the common question that I'm getting a lot lately): when in production my tiers won't be in the same machine. I don't know the ip of the web services machine so, how can I prepare my application so it can work in that scenario? And, if the machine changes after some time, how can I update my application's web reference without recompiling?

Other typical scenario would be using the same web service but having to make a decision at runtime about the specific machine that will be used.

Ok....this is very simple and actually you should use this practice even in a situation that you perfectly control all your application deployment and maintenance. So… how to do it?

Let's get one thing straight right from the start: when you add a web reference you're not binding your client application to that specific URL. This operation is only intended to get the wsdl and creating the proxy class so you can get all the method and data type information you need to use the web service. After this, what you actually do is a soap request that can be directed anywhere.

Let's start by creating an entrance in web.config that configures your web service endpoint (meaning the URL). To do this, add the following section to your web.config file:

 

<configuration>

  <appSettings>

    <add key="WebServicesUrl" value="http://x.x.x.x/Services/books.asmx" />

  </appSettings>

</configuration>

 

You should use the appSettings section to set some values that are used within your application and subject to change overtime. This way, you can change them at runtime because they are read using the ConfigurationSettings.AppSettings property as we shall see.

So, if you look at the code of your proxy class, you see that there is a property called 'Url'. Yes, that's just it. To find this you have to set the option in Solution Explorer to 'Show All Files', expand your web reference, expand Reference.map, and inside Reference.cs you'll find something like this:

 

public Books() {

this.Url = "http://x.x.x.x/Services/books.asmx";

}

We see that in the constructor of the proxy class a reference is set to specify where the web service is located.

So, you see that an easy way to do this is just to replace this code with the following lines:

public Books() {

this.Url = ConfigurationSettings.AppSettings["WebServicesUrl"]

}

 

This way, if the web service changes location you can simply change the web.config file to update your application on-the-fly. Because this property is also publicly available, you can also change it before invoking the web service methods.

 

So, like I said...it's pretty simple...no rocket science...but hopefully it will help some of you.

 

Published Wednesday, July 28, 2004 12:24 PM by nunos
Filed under:

Comments

Wednesday, July 28, 2004 12:58 PM by Peter Cook

# re: Redirect web services at run time

You could just set the web reference to dynamic rather than static and then VS.NET takes care of all that for you - even creates an entry in the app.config.
Wednesday, July 28, 2004 1:08 PM by nunos

# re: Redirect web services at run time

Oh right....I forgot to add that...thanks.
I usually like to explain how to do things step-by-step so everyone can understand what is going on and what are the consequences of their actions. Of course, in a day-to-day basis I fully recommend using the automated features of Visual Studio...
Wednesday, July 28, 2004 1:48 PM by Alex Kamenev

# re: Redirect web services at run time

Besides dynamic reference there is another issue to mention: I would not recommend changing the proxy file because it is auto-generated so if then you will update your Web reference your changes will be lost.
Wednesday, July 28, 2004 2:43 PM by denny

# re: Redirect web services at run time

UDDI Server???


make the service dynamic.

but also have 2 or more UDDI servers running and make a call to them frst...

get a list of servers that have a matching service, then pick one from the list.

another idea:

have a "status" service that tests a list of UDDI services and tells you which ones are down / up

use that and UDDI and you can build a very strong network of web services....

kinda like using DNS to find a web server or a mail server and using ping to find the best connection to it.

so if you have say:

123.123.45.12 @ 100 ms
and
233.123.12.23 @ 10 ms then
use #2 as it's faster.

Wednesday, July 28, 2004 6:04 PM by AndrewSeven

# re: Redirect web services at run time

Dynamic: Thats nice, I will be using it very soon.
Thursday, July 29, 2004 8:41 PM by JMcNealy

# re: Redirect web services at run time

We can set the URL like above, but how do you set the name, name space, Request name space, Response name space dynamically.
below is the code example generated using wsdl utility using wsdl file

[System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="http://33.83.76.254/MyValidation"">http://33.83.76.254/MyValidation", ResponseNamespace="http://33.83.76.254/MyValidation"">http://33.83.76.254/MyValidation")]
[return: System.Xml.Serialization.SoapElementAttribute("responseXml")]
public string myvalidate(string s1, string s2)
{


How do you read those values dynamically from web.config file?

Thanks,
JMcN

# nunos s Blog Redirect web services at run time | Cast Iron Cookware

Anonymous comments are disabled
 
Page view tracker