Click here to Skip to main content
15,891,744 members
Articles / Programming Languages / C#
Article

Dynamically accessing XML Webservices bypassing the Configuration file

,
Rate me:
Please Sign up or sign in to vote.
2.55/5 (17 votes)
3 Nov 2003CPOL3 min read 102.6K   35   11
An article on how to dynamically access Webservices bypassing the App.Config file entries.

Image 1

Introduction

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.

Background

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.
    C#
    //
    //
    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 
    <configuration>
    <appsettings>
    <ADD value="http://localhost/DegreeTestSetup/Service1.asmx" 
        key="DynClienttoWS.wsHost.Service1">
    </appsettings>
    </configuration>
    //
  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).
    C#
    // 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 = 
        System.Configuration.ConfigurationSettings.AppSettings[
           "DynClienttoWS.wsHost.Service1"];
              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.
    C#
    public Service1(string urlInput) 
    {
      this.Url =urlInput;
    }
  9. Modify the Submit button click event to send the newly generated URL to the Proxy reference
    C#
    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.
    C#
    private void btnClear_Click(object sender, System.EventArgs e)
    {
      lblDegreeTemp.Text="";
      txtFTemp.Text="";
    }
    
    private void btnEnd_Click(object sender, System.EventArgs e)
    {
      this.Dispose(true);
    }

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.

License

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


Written By
Product Manager
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralC# Client Consuming PHP Client Pin
simple1811-Dec-05 6:59
simple1811-Dec-05 6:59 
Generalhi Pin
anupamkundu16-Aug-04 7:22
anupamkundu16-Aug-04 7:22 
GeneralRe: hi Pin
Kapil Singhal16-Aug-05 20:02
Kapil Singhal16-Aug-05 20:02 
QuestionWhy not inherit from Service1? Pin
Sven So.16-Aug-04 4:54
Sven So.16-Aug-04 4:54 
QuestionWhy not use the .Url property instead? Pin
Anonymous26-May-04 14:58
Anonymous26-May-04 14:58 
AnswerRe: Why not use the .Url property instead? Pin
simple1811-Dec-05 7:14
simple1811-Dec-05 7:14 
GeneralNot good idea Pin
gregoryb4-Nov-03 21:21
gregoryb4-Nov-03 21:21 
GeneralModifying generated proxies Pin
Sami Vaaraniemi4-Nov-03 20:53
Sami Vaaraniemi4-Nov-03 20:53 
GeneralRe: Modifying generated proxies Pin
Yuval Naveh13-Nov-03 1:12
Yuval Naveh13-Nov-03 1:12 
Sami,
Your idea is great.
I used it very easily. And it is much better indeed than writing to auto generated files.
I wonder why Microsoft did not think of it...
The dynamic URL is not so dynamic since the app.config is a static file!

BigMan Cool | :cool:
GeneralRe: Modifying generated proxies Pin
maithoa8-Apr-04 18:41
maithoa8-Apr-04 18:41 
GeneralRe: Modifying generated proxies Pin
manojk_batra2-Oct-05 19:28
manojk_batra2-Oct-05 19: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.