1

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.

Anil D
  • 1,989
  • 6
  • 29
  • 60
  • sv is empty. This line `sv.Equals("https://test.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6");` doesn't do anything. – Amiram Korach Aug 30 '12 at 07:54
  • so will you please tell me how to set it? i tired this section.Settings.Get("ItineraryBuilder_SalesForceService_SforceService").Value.ValueXml.InnerText = "https://test.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6"; did not work either – Anil D Aug 30 '12 at 08:13
  • @tomfanning is right. Don't do this, but when you want to invoke the web service change the url of the instance. – Amiram Korach Aug 30 '12 at 08:15
  • i was trying that too , but did not work so is 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 = "https://test.salesforce.com/services/Soap/c/25.0/0DFd00000000Wa6"; – Anil D Aug 30 '12 at 08:32
  • Edit your question with this code also. – Amiram Korach Aug 30 '12 at 08:38
  • Question has been edited, please advice – Anil D Aug 30 '12 at 09:32
  • How did you notice it didn't work? – Amiram Korach Aug 30 '12 at 09:53
  • i have two API Salesforce Production and Sandbox, and diff credentials, then i set Web-reference Production url but credential of Sandbox, then change Url to Sandbox in code but it still gives Invalid Login error. – Anil D Aug 30 '12 at 10:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16019/discussion-between-anil-d-and-amiram-korach) – Anil D Aug 30 '12 at 10:16

2 Answers2

2

Don't do this.

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

tomfanning
  • 9,552
  • 4
  • 50
  • 78
1

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.

Anil D
  • 1,989
  • 6
  • 29
  • 60