Click here to Skip to main content
12,698,373 members (22,271 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

Dynamically accessing XML Webservices bypassing the Configuration file

, , 3 Nov 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on how to dynamically access Webservices bypassing the App.Config file entries.


There can be the following plausible scenarios in terms of accessing XML web services :-

  1. You are both the provider and consumer of an XML Web service.
  2. You may be accessing an XML Web service provided by someone else but you know that you may not even know if an XML Web service that suits your purposes in fact exists.

In the second case, there might be a scenario

  1. Where the administrator updates the app.config file entries to access the new Web Service.
  2. Again, there might be a scenario where the URL to access the new Web Service is generated dynamically (provided as an input).

This solution attempts to address the case where the URL of the Web Service is generated only at runtime, therefore it is not possible to manually alter the app.config file to reflect the changed URL. The following code snippet shows how to modify the proxy class created from the Web reference to reach the correct Web Service location.


The reader must have some basic understanding of XML Web Services and .NET platform. Besides, before running this application, the sample Web service from MS must be installed in two different Web locations on your network. The sample Web service can be found at this link

These web services need to be tested as working from both the locations by using the IIS.

Creating the code

We will write a C# Windows client to the above mentioned Web Services.

  1. Create a Windows Application project. Design the form like the one shown above.
  2. There will be two labels, one Textbox and three buttons.
    public class Form1 : System.Windows.Forms.Form
    private System.Windows.Forms.Label lblHeader;
    private System.Windows.Forms.TextBox txtFTemp;    
    private System.Windows.Forms.Label lblDegreeTemp;
    private System.Windows.Forms.Button btnResult;
    private System.Windows.Forms.Button btnClear;
    private System.Windows.Forms.Button btnEnd;
  3. Add Web Reference to the one of the Web services using the Add Web References Dialog Box.
  4. This process will instruct Visual Studio to download the service description to the local machine and then generate a proxy class for the chosen XML Web service. The proxy class will contain methods for calling each exposed XML Web service method both synchronously and asynchronously. This class is contained in the local .wsdl file's code-behind file.
  5. Modify the Web Reference URL property as Dynamic (from default static). The Add Web Reference sets this property by default to the URL of the XML Web service you select, which is a static URL. This creates a app.config file with following entry.
    //This is the xml entry in the application config file. 
    //This file is read only and cannot be modified programmatically
    //DynClienttoWS is the namespace of the project
    //wsHost is the Web reference namespace 
    <ADD value="http://localhost/DegreeTestSetup/Service1.asmx" 
  6. If you set the URL Behavior property of the Web reference to dynamic, the application obtains the URL at run time from the <appSettings> Element of your application's configuration file. When you specify a dynamic URL after adding a Web reference, Visual Studio updates the proxy class to obtain the URL from the configuration file. But the problem arises when the URL to the Web service is received at runtime so that the app.config file cannot be altered.
  7. Click "Show all Files" icon of the Solution Explorer. Open the Reference.cs file. This is the proxy file created Visual Studio. We will modify this file to accommodate a dynamic link. Comment the following code block (default constructor).
    // This constructor is commented as it will not be used in the future.
    // We will use a overloaded constructor for our purpose  
     public Service1() {
              string urlSetting = 
              if ((urlSetting != null)) {
                   this.Url = string.Concat(urlSetting, "");
               else {
                   this.Url = "http://localhost/Degreetest/Service1.asmx";
  8. Add the following constructor in the Reference.cs file.
    public Service1(string urlInput) 
      this.Url =urlInput;
  9. Modify the Submit button click event to send the newly generated URL to the Proxy reference
    private void btnResult_Click(object sender, System.EventArgs e)
      double dDegreeTemp=0;
      double dFahTemp = double.Parse(txtFTemp.Text);
      //wsHost.Service1  convTemp =  new wsHost.Service1();
      wsHost.Service1  convTemp =  new wsHost.Service1(
         "http://<IP of WebServer>/DegreeTest/Service1.asmx");
      dDegreeTemp = convTemp.ConvertTemperature(dFahTemp);
      lblDegreeTemp.Text="The Temperature in degrees will be " + dDegreeTemp;
  10. Write the following lines for the other two button click events.
    private void btnClear_Click(object sender, System.EventArgs e)
    private void btnEnd_Click(object sender, System.EventArgs e)

Points of Interest

We can dynamically create the URL to access the Web service or can receive the URl as an XML input; whatever it may be the Web service can be easily referenced by modifying the constructor in the proxy.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Authors

Product Manager
United States United States
No Biography provided

India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralC# Client Consuming PHP Client Pin
simple1811-Dec-05 7:59
membersimple1811-Dec-05 7:59 
Generalhi Pin
anupamkundu16-Aug-04 8:22
memberanupamkundu16-Aug-04 8:22 
GeneralRe: hi Pin
Kapil Singhal16-Aug-05 21:02
memberKapil Singhal16-Aug-05 21:02 
QuestionWhy not inherit from Service1? Pin
stotti_no116-Aug-04 5:54
memberstotti_no116-Aug-04 5:54 
QuestionWhy not use the .Url property instead? Pin
Anonymous26-May-04 15:58
sussAnonymous26-May-04 15:58 
AnswerRe: Why not use the .Url property instead? Pin
simple1811-Dec-05 8:14
membersimple1811-Dec-05 8:14 
GeneralNot good idea Pin
gregoryb4-Nov-03 22:21
membergregoryb4-Nov-03 22:21 
GeneralModifying generated proxies Pin
Sami Vaaraniemi4-Nov-03 21:53
memberSami Vaaraniemi4-Nov-03 21:53 
GeneralRe: Modifying generated proxies Pin
Big Man13-Nov-03 2:12
memberBig Man13-Nov-03 2:12 
GeneralRe: Modifying generated proxies Pin
maithoa8-Apr-04 19:41
membermaithoa8-Apr-04 19:41 
GeneralRe: Modifying generated proxies Pin
manojk_batra2-Oct-05 20:28
membermanojk_batra2-Oct-05 20:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 4 Nov 2003
Article Copyright 2003 by anupamkundu, SomnathDas
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid