I have code that references a web service, and I\'d like the address of that web service to be dynamic (read from a database, config file, etc.) so that it is easily changed
If you are fetching the URL from a database you can manually assign it to the web service proxy class URL property. This should be done before calling the web method.
If you would like to use the config file, you can set the proxy classes URL behavior to dynamic.
If you are truly dynamically setting this, you should set the .Url field of instance of the proxy class you are calling.
Setting the value in the .config file from within your program:
Is a mess;
Might not be read until the next application start.
If it is only something that needs to be done once per installation, I'd agree with the other posters and use the .config file and the dynamic setting.
I've struggled with this issue for a few days and finally the light bulb clicked. The KEY to being able to change the URL of a webservice at runtime is overriding the constructor, which I did with a partial class declaration. The above, setting the URL behavior to Dynamic must also be done.
This basically creates a web-service wrapper where if you have to reload web service at some point, via add service reference, you don't loose your work. The Microsoft help for Partial classes specially states that part of the reason for this construct is to create web service wrappers. http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.100).aspx
// Web Service Wrapper to override constructor to use custom ConfigSection
// app.config values for URL/User/Pass
namespace myprogram.webservice
{
public partial class MyWebService
{
public MyWebService(string szURL)
{
this.Url = szURL;
if ((this.IsLocalFileSystemWebService(this.Url) == true))
{
this.UseDefaultCredentials = true;
this.useDefaultCredentialsSetExplicitly = false;
}
else
{
this.useDefaultCredentialsSetExplicitly = true;
}
}
}
}
I know this is an old question, but our solution is much simpler than what I see here. We use it for WCF calls with VS2010 and up. The string url can come from app settings or another source. In my case it is a drop down list where the user picks the server. TheService was configured through VS add service reference.
private void CallTheService( string url )
{
TheService.TheServiceClient client = new TheService.TheServiceClient();
client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url);
var results = client.AMethodFromTheService();
}
For me a Reference to a WebService is a
SERVICE REFERENCE
.
Anyway it's very easy. As someone said, you just have to change the URL in the web.config file.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="YourServiceSoap" />
</basicHttpBinding>
</bindings>
<client>
**** CHANGE THE LINE BELOW TO CHANGE THE URL ****
<endpoint address="http://10.10.10.100:8080/services/YourService.asmx"
binding="basicHttpBinding" bindingConfiguration="YourServiceSoap"
contract="YourServiceRef.YourServiceSoap" name="YourServiceSoap" />
</client>