Dynamic WebServices Address Setting

Source: Internet
Author: User

Method 1:Sometimes we need to dynamically set the WebService address so that it will be re-generated when published to different servers. in config, the WebService address is dynamically configured and a lot of information is found online. This method is easy to use and implement. Modify the local proxy class (Add a new class to inherit your WebService proxy class)

 
NamespaceWeb_service
 
{
 
[System. Diagnostics. debuggerstepthrough (), system. componentmodel. designercategory ("Code"),
 
 
 
System. Web. Services. webservicebinding (name = "", namespace = "")]
 
 
 
Public ClassDynwebservice: selfwebservice
 
 
 
{
 
 
 
PublicDynwebservice ():Base()
 
 
{
 
 
 
// Set the default WebService address
 
 
 
This. Url ="Http: // localhost/WebService. asmx";
 
 
 
}
 
 
 
PublicDynwebservice (StringWeburl ):Base()
 
 
 
{
 
 
 
This. Url = weburl;
 
 
 
}
 
}
 
 
 
}

Note: selfwebservice the WebService you referenced

Deploy the Web Service URI to the configuration file.

 
<Add Key="Webservicekey" Value="Http: // xxxx/WebService. asmx"/>

Final Implementation (call)

 
Private VoidWebservicetest ()
 
{
 
StringWebserviceurl = configurationmanager. appsettings ["Webservicekey"]. Tostring ();
 
 
 
Web_service.dynwebservice DWS =NewWeb_service.dynwebservice (webserviceurl );
 
 
StringResult = DWS. helloworld ();
 
 
 
}

Note: helloword () is the WebService method you call. Here we assume it returns a value of the string type!

OK. Now we can fix it!

Method 2:

You can choose the Web Serivce file on your client (like the one in the earth-shaped area). My name is the localhost file, then there is a URL in the right-side health check, and then the dynamic modification will be made to the dynamic modification. Web. config appears

 
<Add Key="Webpos. localhost. myservice" Value="Http: // localhost/soapservice/myservice. asmx"/>

What I think is that this kind of child may be affected by your web service. This time
Http: // localhost/accesswebpos/web references/localhost/reference. CS
File appears

 
PublicMyservice ()
 
{
 
StringUrlsetting = system. configuration. configurationsettings. deleettings ["Webpos. localhost. myservice"];
 
If(Urlsetting! =Null))
 
{
 
This. Url =String. Concat (urlsetting ,"");
 
 
 
}
 
 
 
Else
 
{
This. Url ="Http: // localhost/soapservice/myservice. asmx";
 
}
 
}

Another method for dynamically calling webserice is to simulate and generate a client proxy class to realize dynamic calling of WS.

Address: http://topic.csdn.net/u/20090220/10/08c8ca6f-3733-40ff-840e-d6ca6894ab35.html

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.