Anil D
Anil D

Reputation: 2009

Update web.config applicationSettings programmatically with C# (MVC)

I want to update web reference Url in web.config applicationSettings at runtime

Here is web config application settings

<applicationSettings>
<ItineraryBuilder.Properties.Settings>
  <setting name="ItineraryBuilder_SchedulesConnectionsService_SchedulesConnectionsService"
    serializeAs="String">
    <value>http://www.pathfinder-xml.com/soap/*/services/SchedulesConnections</value>
  </setting>
  <setting name="ItineraryBuilder_SalesForceService_SforceService"
    serializeAs="String">
    <value>https://login.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6</value>
  </setting>
  <setting name="ItineraryBuilder_OAGService_CBWSPublicService"
    serializeAs="String">
    <value>http://ondemand.oag.com:80/CBWebServicePublic/CBWSPubliclPort</value>
  </setting>
</ItineraryBuilder.Properties.Settings>
</applicationSettings>

here is my code using to update

var configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
var section = (System.Configuration.ClientSettingsSection)configuration.GetSection("applicationSettings/ItineraryBuilder.Properties.Settings");

System.Configuration.SettingValueElement sv = new System.Configuration.SettingValueElement();
sv.Equals("https://test.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6");
section.Settings.Get("ItineraryBuilder_SalesForceService_SforceService").Value = sv;
configuration.Save();

But it does not update Value, instead remove Value tag from settings

Earlier i was trying to update Web service url, but did not work so i follow this approach, will you please guide me on how to update web service url?

this is what i was doing

SforceService sf = new SforceService();
sf.Url = "test.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6";;

Please help.

Upvotes: 1

Views: 1880

Answers (2)

Anil D
Anil D

Reputation: 2009

Thanks Amiram Korach for all you help

With his help i got what i was doing wrong.

As he suggested there is no need to update web config as it is not a good approach and so the solution for the above problem is updating Web reference at runtime and so the following code works fine

SforceService sf = new SforceService();
sf.Url = "test.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6"

what i was doing wrong, the "sf" was not passed to API class which is used in creating connection. Amiram Korach helped me finding this issue.

Thanks again.

Upvotes: 1

tomfanning
tomfanning

Reputation: 9670

Don't do this.

Specifically, one would hope that your web application root folder isn't writeable by the application pool identity.

Upvotes: 2

Related Questions