Dynamic reference of Web Service

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.