There can be the following plausible scenarios in terms of accessing XML web services :-
- You are both the provider and consumer of an XML Web service.
- 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
- Where the administrator updates the app.config file entries to access the new Web Service.
- 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.
- Create a Windows Application project. Design the form like the one shown above.
- 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;
- Add Web Reference to the one of the Web services using the Add Web References Dialog Box.
- 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.
- 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.
- 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.
- 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).
- Add the following constructor in the Reference.cs file.
public Service1(string urlInput)
- 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 dFahTemp = double.Parse(txtFTemp.Text);
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;
- 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.