Adding a reference to webservice in a project is relatively fixed. After the webservice address is changed, you need to re-Add the webservice reference for the entire project, dynamic configuration allows you to change the referenced address of the entire project's webservice only by modifying the configuration file when the webservice address changes. Especially in the program development and testing phase, you can first test the webservice on the local machine, and then release it to the server after the test is successful. The specific implementation steps are as follows:
1. Add a local webservice reference
The added image:
After webservice is referenced, double-click to view the automatically added code, for example:
[System. Diagnostics. DebuggerStepThroughAttribute ()]
[System. ComponentModel. DesignerCategoryAttribute ("code")]
[System. Web. Services. WebServiceBindingAttribute (Name = "GlobalWeatherSoap", Namespace = "http://www.webserviceX.NET")]
Public partial class GlobalWeather: System. Web. Services. Protocols. SoapHttpClientProtocol {
/// <Remarks/>
Public GlobalWeather (){
This. Url = "http://www.webservicex.net/globalweather.asmx ";
}
}
[System. Diagnostics. DebuggerStepThroughAttribute ()]
[System. ComponentModel. DesignerCategoryAttribute ("code")]
[System. Web. Services. WebServiceBindingAttribute (Name = "GlobalWeatherSoap", Namespace = "http://www.webserviceX.NET")]
Public partial class GlobalWeather: System. Web. Services. Protocols. SoapHttpClientProtocol {
/// <Remarks/>
Public GlobalWeather (){
This. Url = "http://www.webservicex.net/globalweather.asmx ";
}
}
Dynamic reference webservice inherits the reference automatically added by the system and transmits the url transposition parameter. By default, no parameter is the code used to call the reference, after changing the webservice address, you must reference the corresponding function with parameters. This allows you to debug our system and convert the function when the webservice address is changed.
2. generate an inheritance class for dynamic reference
Units to be referenced:
[Csharp]
Using System. Diagnostics;
Using System. ComponentModel;
Using System. Web. Services;
Using System. Diagnostics;
Using System. ComponentModel;
Using System. Web. Services;
Inheritance class to be generated:
[Csharp]
/// <Summary>
/// Dynamically reference webservice
/// </Summary>
[DebuggerStepThrough (), DesignerCategory ("code"), WebServiceBinding (Name = "", Namespace = "")]
Class Dyn_Weather: WeatherService. GlobalWeather
{
Internal Dyn_Weather ()
: Base ()
{
This. Url = "http://www.webservicex.net/globalweather.asmx ";
}
Internal Dyn_Weather (string ip) // The constructor with parameters. The ip address is specified at www.2cto.com.
: Base ()
{
This. Url = string. Format ("http: // {0}/globalweather. asmx", ip );
}
}
/// <Summary>
/// Dynamically reference webservice
/// </Summary>
[DebuggerStepThrough (), DesignerCategory ("code"), WebServiceBinding (Name = "", Namespace = "")]
Class Dyn_Weather: WeatherService. GlobalWeather
{
Internal Dyn_Weather ()
: Base ()
{
This. Url = "http://www.webservicex.net/globalweather.asmx ";
}
Internal Dyn_Weather (string ip) // a constructor with parameters. Specify an ip address when calling the constructor.
: Base ()
{
This. Url = string. Format ("http: // {0}/globalweather. asmx", ip );
}
}
3. Call dynamic webservice
[Csharp]
Dyn_Weather dy = new Dyn_Weather ("192.168.1.10"); // specify the IP address of the webservice.
LblDeviceid. Text = dy. GetWeather ("beijing", "china"). ToString ();
Dyn_Weather dy = new Dyn_Weather ("192.168.1.10"); // specify the IP address of the webservice.
LblDeviceid. Text = dy. GetWeather ("beijing", "china"). ToString ();
In this way, the dynamic configuration of webservice can be realized by setting the IP address of webservice during the construction function, which brings convenience to project development.
From # Define