Dynamically Change URL of web service

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

I have a the same web service that sits on multiple servers. All I want to
do is change the URL that the client app points to. I found this code in
the Reference.vb file under the web reference:



Public Sub New()

MyBase.New

Me.Url = "http://abccompany.tam.net/tam/tam.asmx"

End Sub



How can I dynamically change this when the client app loads. What VB.NET
code would I use?



Derek
 
If you select this web reference, you can change the properties to make this
dynamic. What this does is put an entry in the config file, so you can
update it by simply changing the config file. (Very handy for moving through
dev, staging, and production.) If you need to be able to dynamically set it,
you can either rewrite this code and grab it from a variable (in which case
it will be overwritten if you ever refresh this reference) or you can open
up the config file as an XML file and edit the XML.
 
I need to be able to read the URL from a database. Different windows forms
client apps will use the exact same web service, but on different servers.
So when my app loads, I need to read the URL before I make the first method
call on the web service. When I use the following code:

Dim myService As New myService.webService

myService.Url = http://myserver.mywebservice.net/

myservice.MethodCall1


I get the error "the request failed with http status 405" when I make my
first method call.

I have seen a ton of responses such as yours, but I have not a lot of people
successfully pull this one off. And I have set the URL Behavior to dynamic.
It seems like more things need to be set here. If I have to look into the
app.config file, that is fine, but I really want to set this dynamically
from a database.

Derek
 
Well, I just pulled this one off, and I can guarantee you it will work, as
it does here. Try putting in the full URL to the asmx file itself, and not
just the path. Also, make sure that you have it in quotes - you don't have
quotes in what you typed below, and I don't know if that was just quick
typing or not. (URL takes a string argument.)

myService.URL = "http://myserver/myWebService/Service1.asmx";
myService.invokeMethod();

I had completely forgotten that the URL property was public, which makes my
rather difficult (though functional) alternatives seem rather ... erm ...
silly. Sorry about that...
 
Good catch. When I add the web reference, I connect to server A (for
simplicity). When I updated the URL to server A, yes, the code did not
crash. But when I update the URL dynamically in code to server B, I get the
following error:

"Server was unable to process request. Object reference not set to an
instance of an object."

I know for a fact these web services are identical. So I wonder if
something else has to change here. The app.config file? But remember, I
want to do this dynamically. Any ideas?

Derek
 
I got this working. What is interesting is that the URL Behavior can be set
to Static, and it still can be changed in code. I guess this property
involves other things . . .

Derek
 
What the URL Behavior property does is twofold:

1. It injects code into the wrapper class in Reference.cs that, in the
constructor of this class, checks the AppSettings portion of Web.Config for
an URL. If it doesn't find one there, it uses the one you have already
specified.
2. It inserts a line into the appSettings portion of the Web.Config with the
current URL.

Once you have constructed the object, you can change the URL all that you
want - this initialization only happens in the constructor. Each time you
construct a new object, you'll get what is in your web.config (or the
default) again.
 
Back
Top