C # dynamically modify the webservice address and port

Source: Internet
Author: User

Static modification method---------------------------------- This problem is actually not as complicated as I think. We all know how to directly modify it, that is, to modify the configuration section of the WebConfig file. See the following picture.

I believe many people know this. Just modify it.Dynamic Modification Method------------------------------------------------------------ How to modify it dynamically? I think many people may say this in this way, modifying the WebConfig file has a dedicated help class, or you can write it yourself because WebConfig can be edited in Xml format, the correct method is indeed feasible, so I tell you, you are wrong, there are simpler and more suitable methods: the first step for using Webservice should be to create an object, right? See the following code.

// Web service object WebReference. Service1SoapClient objService = new WebReference. Service1SoapClient ();

If you are more careful, right-click the constructor to view the definition and try again.

This is the top five methods after definition. Let's take a look.

There are five automatic generation methods.

public Service1SoapClient();        public Service1SoapClient(string endpointConfigurationName);        public Service1SoapClient(Binding binding, EndpointAddress remoteAddress);        public Service1SoapClient(string endpointConfigurationName, EndpointAddress remoteAddress);        public Service1SoapClient(string endpointConfigurationName, string remoteAddress);

Let's take a look at the fourth and fifth methods.

// EndpointConfigurationName: the name of the configuration section. The remoteAddress configuration section object is public Service1SoapClient (string endpointConfigurationName, EndpointAddress remoteAddress). // The Name Of The endpointConfigurationName configuration section, string remoteAddress); // set other parameters

For the two methods, we only need to modify the fifth method. The method is as follows:

 

// Web service object webreference. service1soapclient objservice = new webreference. service1soapclient ("service1soap", "the URL you want to modify, such as: http://www.cckan.net/webservices.asmx ");

 

After the problem is solved, we only need to dynamically configure this parameter. The specific method will be fine for everyone. It can be stored in the database or somewhere else. Do not be stupid enough to get the configuration file. The WebService itself has a way to modify it.

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.