Asp.net (c #) dynamically modify the webservice address and port (dynamically modify the configuration file)

Source: Internet
Author: User

This problem is actually not as complicated as I think. We all know how to directly modify it, that is, modify the configuration section of the WebConfig file.
For more information, see the following figure.

I believe many people know this. Just modify it.
Dynamic Modification Method
----------------------------------------------------------
So how can we 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, this method is indeed feasible.
So I will tell you that you are wrong. There are simpler and more suitable methods.
The method is as follows:
The first step for using Webservice is to create an object.
Check 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.

Copy codeThe Code is as follows: 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.
Let me explainCopy codeThe Code is as follows: // endpointConfigurationName indicates the name of the configuration section and remoteAddress indicates the configuration section object.
Public Service1SoapClient (string endpointConfigurationName, EndpointAddress remoteAddress );
// EndpointConfigurationName indicates the name of the configuration section, and remoteAddress indicates the URL of the configuration section.
Public Service1SoapClient (string endpointConfigurationName, string remoteAddress); // set other parameters

That's the two methods. We only need to modify the fifth method,
The usage is as follows:
// Web service objectCopy codeThe Code is as follows: 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.

This article from: http://www.cckan.net Author: su Fei

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.