![]() |
Languages »
C / C++ Language »
General
Intermediate
Dynamically accessing XML Webservices bypassing the Configuration fileBy anupamkundu, SomnathDasAn article on how to dynamically access Webservices bypassing the App.Config file entries. |
C#, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
There can be the following plausible scenarios in terms of accessing XML web services :-
In the second case, there might be a scenario
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.
We will write a C# Windows client to the above mentioned Web Services.
//
//
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;
....
....
//
//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> //
<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.
// 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";
}
}
*/
public Service1(string urlInput)
{
this.Url =urlInput;
}
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;
}
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);
}
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.
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 3 Nov 2003 Editor: Nishant Sivakumar |
Copyright 2003 by anupamkundu, SomnathDas Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |