Vigna Hari Karthik
Vigna Hari Karthik

Reputation: 109

Dynamically update the web config for webservice

I have few a web services which I have configured in the web application. In that I have set all the webservices as dynamic. So I can see all the webservices in the web.config file.

For example:

<applicationSettings>
    <WebInterface.Properties.Settings>
      <setting name="WebInterface_CustomerCard_CustomerCard_Service"
       serializeAs="String">
        <value>http://localhost:7047/DynamicsNAV/WS/Supratechnic%20(M)%20Sdn%20Bhd/Page/CustomerCard</value>
      </setting>
</WebInterface.Properties.Settings>
  </applicationSettings>

In the login page I need to change these webservices dynamically at runtime and save it in web.config at the time of login. How can I change it in the code behind?

Upvotes: 0

Views: 846

Answers (1)

Markus
Markus

Reputation: 22491

Web.config is not the right place for a change like this because it is shared among all users. Your application can be used by several users at the same time so this change would affect many of them instead of just one. In a secure environment, your application is not able to write to web.config anyway and - as also @PanagiotisKanavos points out - writing to it would recycle the app pool right away.

A better way to achieve this is to provide the URL when you instantiate the web service client as a constructor argument. You'd add a second URL to your web.config, maybe in the AppSettings section. In the login page, you decide which URL is the right one for the user and save it in a place that is specific for the user e.g. in Session state. Whenever you create an instance of the client, you retrieve the URL and provide it to the constructor.

Upvotes: 1

Related Questions