Setting Web Service References Dynamically
page 1 of 1
Published: 05 Aug 2002
Unedited - Community Contributed
Abstract
Techniques for changing the URL of an XML Web Service reference dynamically.
by Steve Sharrock
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 116808/ 188

Setting Web Service References Dynamically

I recently finished a public business site for a client that used a Web Service to access the client's back-end database. In addition to the web site itself, I wrote the ASP.NET Web Service in C#. The Web Service was relatively simple and only needed to make a few references into the clients Managed C++ (.NET) interface modules. The client's Managed C++ further interfaced to some legacy business logic DLLS written in unmanaged C++.

I started by creating the Web Service project on my "localhost" workstation. The code simply returned some test data without actually calling the client's Managed C++ modules. When I created the web application project, I simply did a right-click in the Solution Explorer on the "Web References" item and added the reference to my localhost Web Service. As usual it created a web reference named "localhost".

After I added functionality to the Web Service, I deployed a version of the service to one of the clients test servers. I wanted to keep my "localhost" reference for testing, so I added a new web reference to the client's site (acmeco.com). As expected, I got a new web reference named "com.acmeco".

I accessed the appropriate Web Service with a compile-time "#define / #if" within the logic of my web site's code; but I was unhappy with this source code change to select the appropriate Web Service. By the time the client wanted to another optional server to host the Web Service, and I now had three choices, I decided to try and make the selection of the Web Service location dynamic.

My first solution was way overkill. I went to my old familiar design patterns and used a modified bridge-pattern class to create an object that would access the appropriate web reference at run-time. This seemed like too many concrete classes for this simple problem.

Finally I was looking at the on-line help (RTFM), and I found a method of the class (SoapHttpClientProtocol) from which my web reference object was created that allowed me to set the "Url" that specified the location of the Web Service. At this point, I realized I could delete all but one of my web references, and then set the "Url" at run-time to choose the appropriate Web Service. I simply renamed my one remaining web reference (right click in the Solution Explorer) from "localhost" to a common name, like "AcmecoService". I then set the "AmcecoService" object's "Url" at run-time from a value stored in "appSettings" of the web.config file.

  service = new Acmeco.AcmecoService();
  service.Timeout = 10000;
  service.Url = ConfigurationSettings.AppSettings["AcmecoWebServiceUrl"];
   

This solution may be obvious to many, especially those that read (and retain) all of the .NET Web Service/Reference documentation. For me, it was a real simplification of a solution to an annoying problem.

Update to this Article (9/15/02)

Oopps...I found an even easier method to set the Web Service Url dynamically using Visual Studio properties.

And now I've found that the Visual Studio is way ahead of me. Simply choose the properties of the web service and change the "Url Behavior" from "Static" to "Dynamic". This will automatically create an entry in the <appSettings> section of the Web.Config file that contains the Url string. The constructor of the Web Service proxy class is automatically changed to set the Url property to the value stored in Web.Config.

Send your comments and let me know what you think of this article: steve@sharkcode.com

Steve Sharrock -   www.AspAlliance.com/shark



User Comments

Title: vikky   
Name: vikky
Date: 4/16/2012 4:09:25 AM
Comment:
thank you
Title: A better way for this   
Name: Jack
Date: 6/30/2011 12:22:34 PM
Comment:
Why don't we do it as in the following link. That's more efficient and fast.

http://techmentis.blogspot.com/2011/05/dynamic-web-service-invoker.html
Title: Thank you   
Name: Bich Tram
Date: 4/17/2011 6:02:49 AM
Comment:
Thank you!!!
Title: Software Engineer   
Name: Moni
Date: 8/9/2009 3:15:44 AM
Comment:
I have developed web consumer in Visual Studio 2008(unmanaged code) using Sproxy.exe tool. It works all fine until i need to set URL dynamically. Here is my code snippet.

HRESULT hr;
CServiceWSDL *myService = new CServiceWSDL();
hr = m_proxy->SetUrl(_T("http://www.xxx.../service?wsdl"));
hr = m_proxy->method();

Code works fine if the line to SetUrl is commented. However, fails if it is included.

Please help me get the solution.

Thanks in advance.
Title: Another question   
Name: Jorge
Date: 5/25/2009 1:25:17 PM
Comment:
Hello. I'll like to use it on a PDA, but I can't find web.config file. I think with compact framework I can't change webservice url, can I?
Title: Thanks two bunches   
Name: Chuck Bradley
Date: 5/7/2009 10:45:06 AM
Comment:
You wrote this article in 2002. Wow. Tried and true, man.
Good for putting that update on it about the config. Likewise, it was exactly what I was looking for. Kudos.
Title: Simple   
Name: Mike D
Date: 3/16/2009 11:47:11 AM
Comment:
Exactly what I was looking for. The best solutions are are always the simplest.

Thanks.
Title: Same Methods using Web.Config   
Name: Ketan
Date: 11/30/2008 1:51:30 AM
Comment:
Yes Right I Am Also Using Same Just Look it.

http://dotnet-magic.blogspot.com/2008/09/dynamically-change-url-of-web-service.html
Title: Thanks   
Name: Earl
Date: 11/20/2008 3:44:07 AM
Comment:
Thanks for this great help document. Second update worth really a thounsand words. :) Thanks
Title: Thanks   
Name: Mike
Date: 9/4/2008 12:00:32 PM
Comment:
Short and sweet. Exactly what I was looking for.
Title: Engineer   
Name: Ojulari Hakeem
Date: 6/13/2008 10:08:05 AM
Comment:
Hi, this is a good article, but I would love if u can specifically tell me how to locate the url behaviour on the property of the web service in VS 2005. Thanks
Title: works. Thanks!   
Name: Julia
Date: 6/12/2008 6:11:47 PM
Comment:
Thank you for providing this article. It is really helpful.
Title: Thanks   
Name: Joe
Date: 3/17/2008 4:09:47 PM
Comment:
I recently realized that I was going to need to be able to manage dynamic service calls, and was looking for a solution. This article provided a step in the right direction, and has allowed me to come up with the basic framework for what I will want to accomplish. Thanks.
Title: "url" property   
Name: shalom
Date: 12/10/2007 2:06:39 AM
Comment:
I can not find the "url" property
Title: techdrmz   
Name: elton kent
Date: 12/5/2007 2:02:47 AM
Comment:
this is for a asp website i suppose.. but what if your referencing a webservice from another webservice.. you approach does not work.. sorry to say that..
Title: Where in 2005?   
Name: Holly
Date: 11/14/2007 1:54:36 PM
Comment:
I cannot find how to do this in VS 2005. There is no "Web Reference" per se. When I click on the App_WebReferences, there is no properties available. I am not the only person searching for this.
Title: CLR Stored Procedure   
Name: Mark
Date: 5/11/2007 11:49:49 AM
Comment:
Where can the dynamic URL be changed if the VS project is a CLR stored procedure?
Title: Dynamic Webservice   
Name: Richard D
Date: 11/9/2006 4:04:44 AM
Comment:
Brilliant. Exactly what i was after. Quick and simple.

I was initally struggiling with other ideas of trying to dynamically instanstiate an appropriate web service but struggiling.
Title: setting web reference dynamically   
Name: rabindra
Date: 9/4/2006 6:12:56 AM
Comment:
First of all thanks,,, for the article.I have just scanned thro' it and will be using in my project.I hadnot hoped i would find such a clear cut solution in no time!!!
Title: Re: c++ project is not showing "Url Behavior" property   
Name: Neel Bhatt
Date: 4/25/2006 5:26:28 AM
Comment:
If we are using ATL then to set url dynamically on a c++ client we can use SetURL() method of the Service class.

This method is inherited from CSoapSocketClientT class defined in atlsoap.h from which the service inherits.

e.g.

Service1::CService1 WS;

SetUrl(_T("http://xyz.com/WebService1/Service1.asmx"));

WS.Method1();
Title: Persisting changes to the   
Name: Kiran
Date: 4/14/2006 1:22:10 PM
Comment:
Hey,

Does anyone know how to persist the changes to the app.config file or settings? I want to save the URL when it is changed by the user in the UI of my application. This way the next time they launch the app, the last URL they entered shows up.

Thanks :-)
Title: Setting web reference Url behavior property to dynamic in a web form client   
Name: Aru
Date: 1/20/2006 8:07:17 AM
Comment:
Does it works if the web service client is a web form?? I am trying to set the Url behavior property to dynamic, but do not know how do I do this if web service client is a web form.
Title: c++ project is not showing "Url Behavior" property   
Name: Enric
Date: 12/12/2005 7:01:56 AM
Comment:
I have created managed and unmanaged c++ projects, and added the web reference but its properties never show the "Url Behavior" property. However, a c# project does. I guess the clients of your sample are c# projects... but does anyone know a workaround to set url dynamically on a c++ client?
Title: Really great   
Name: Rajesh
Date: 11/24/2005 9:44:26 AM
Comment:
Thanks...this topic helped me to solve a major problem.

My webrefernce url was changing dynamically. Now i am able to set the Url from the program itself.
Title: THANK YOU!   
Name: CoDr
Date: 10/31/2005 9:50:33 AM
Comment:
This is exactly what I was looking for - thank you for posting!!!!
Title: doubt   
Name: Aravind
Date: 8/8/2005 5:35:03 AM
Comment:
Hi venkat,

You said dynamic url works fine with same versions of webservice. What does same version means ?
Title: :thumb: its work nicely   
Name: Kenny Koay Teng Kean
Date: 6/8/2005 8:28:26 PM
Comment:
yuhuuu... save by the bell.... thx for ur great article...
Title: works fine   
Name: Venkat
Date: 3/29/2005 9:30:20 AM
Comment:
The webservice with dynamic for Url Behavoir works fine. I did a mistake that trying with two different versions of webservices (But the method and signature are same). If it is same version, and set to Dynamic, once you install at client place, you can change in the App.config file. Not need to change anything in the reference.vb file.
Title: still not dynamic   
Name: Venkat
Date: 3/27/2005 5:55:01 AM
Comment:
I develped a VB.NET appl. In that I used webservices and make the url behavoir as dynamic. And I found the reference,vb file is dynamically accepting the url form app.config file. After deploying the application at the client place It is not working. Again I re-complied the application with new URL and deployed the applcation.
Title: Not complete?   
Name: Steve
Date: 3/10/2005 12:10:04 PM
Comment:
Referring to prasad's comment "[...]chage the referece.cs".

I never change the reference.cs from that which is generated by Visual Studio. Changing the Url property of overrides the generated Url.
Title: still it is not dynamic completely   
Name: prasad
Date: 3/10/2005 5:29:19 AM
Comment:
still it is not dynamic complete. you have to chage the referece.cs file created for really accepting the dynamic urls
Title: Mr.   
Name: Vikas
Date: 7/23/2004 2:52:08 AM
Comment:
In case we have multi assembly solution, we may have multiple app.config file in our solution. Hence its better to have configuration entry in web.congfig rather than having it in individual app.config files for each assembly.
I am not able to figure out that whether it is possible to have entry in web.config file. Can you please give some suggestion






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 10/31/2014 8:23:20 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search