How can I dynamically switch web service addresses in .NET without recompiling?

I have a code that references a web service, and I would like the address of this web service to be dynamic (read from the database, configuration file, etc.) so that it changes easily. One of the main uses for this is to deploy in multiple environments where machine names and IP addresses are different. The web service signature will be the same across all deployments, only somewhere else.

Perhaps I just got corrupted by the Visual Studio Add Web Link wizard - it looks like this should be something relatively simple.

+47
c # url visual-studio web-services
Sep 24 '08 at 4:20
source share
11 answers

When you create a web link and click the web link in Solution Explorer. In the properties panel, you will see the following:

Web reference properties

Changing the value in dynamic mode will write to the app.config file.

Here is a CodePlex article that contains more information.

+48
Sep 24 '08 at 4:24
source share

If you really dynamically set this, you must set the .Url field of the proxy class instance that you are calling.

Setting the value in the .config file from your program:

  • It's a mess

  • Unable to read before starting the next application.

If you need to do this only once for installation, I would agree with other posters and use the .config file and dynamic settings.

+24
Sep 24 '08 at 14:56
source share

Change the behavior of the URL to " Dynamic ".

+5
Sep 24 '08 at 4:22
source share

As long as the web service methods and the underlying classes do not change, this is pretty trivial. In Visual Studio 2005 (and later), adding a web link creates the section app.config (or web.config for web applications) that has this URL. All you have to do is edit the app.config file to display the desired URL.

In our project, our simple approach was to simply add app.config entries for each type of environment (development, testing, production). Therefore, we simply uncomment the entry for the desired type of medium. No special coding is required.

+4
Sep 24 '08 at 5:31
source share

I struggled with this problem for several days, and finally the light bulb pressed. The key to being able to change the web service URL at run time is overriding the constructor I made with the partial class declaration. The above, setting the URL behavior for Dynamic should also be done.

This basically creates a web service wrapper, where if you need to restart the web service at some point by adding a link to the service, you won’t lose your job. The Microsoft help system for Partial classes states that part of the reason for this design 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; } } } } 
+4
Jun 27 '13 at 12:51 on
source share

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 higher. The string url may come from application settings or another source. In my case, this is a drop-down list in which the user selects a server. The service was configured using the VS service add link.

 private void CallTheService( string url ) { TheService.TheServiceClient client = new TheService.TheServiceClient(); client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url); var results = client.AMethodFromTheService(); } 
+4
Nov 30 '16 at
source share

If you retrieve the URL from the database, you can manually assign it to the web service proxy URL property. This must be done before calling the web method.

If you want to use the configuration file, you can configure the behavior of proxy class URLs to be dynamic.

+2
Sep 24 '08 at 4:30
source share

Just a note on the difference between static and dynamic.

  • Static : you must set the URL property every time you call the web service. This is because the base URL if the web service is in the constructor of the proxy class.
  • Dynamic : a special configuration key will be created in your web.config file. By default, the proxy class will read the URL from this key.
+2
Sep 24 '08 at 6:13
source share

Definitely use the Url property - the way. Whether you need to install it in app.config, in a database or in a third location depends on your configuration needs. Sometimes you do not want the application to restart when the web service location changes. You may not have a load balancer that scales the backend. Perhaps you are fixing a web service error. Your implementation may have security configuration issues. Regardless of whether they are db data names and passwords or even ws authentication information. Proper segregation of duties can help you with more complex configuration settings.

If you add a wrapper class around the generated proxy classes, you can set the Url property in a unified way every time you create a wrapper class to invoke the web method.

+1
Sep 24 '08 at 15:19
source share

open solition explorer

right click on the URL of changing the web service behavior to dynamic

click the show all files icon in Solution Explorer

in the web link edit the Reference.cs file

change constructor

 public Service1() { this.Url = "URL"; // etc. string variable this.Url = ConfigClass.myURL } 
0
Feb 22 '16 at 21:08
source share

For me, the link to the WebService is

SERVICE LINK

.

In any case, it is very easy. As someone said, you just need 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> 
0
May 11 '16 at
source share



All Articles