Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I programmatically override client app.config WCF endpoint addresses?
    text
    copied!<p>I want to override client WCF endpoint addresses stored in app.config so that I can change them from pointing to "localhost" to pointing to production URLs [depending on configuration that can be set from within the App (contained in an object 'appConfig' in the code shown below) - which is a WinForms project.]</p> <p>By reading other questions in this area I have reached the following pieces of code (InitAllEndpoints which calls into InitEndpoint) which I call from the Form_Load event. I tried these in my application and they appear to change the EndPoint addresses if I hover over the value in the "ep" variable. Yet if I loop again through serviceModelSectionGroup.Client.Endpoints after my code I find they are infact unchanged. (I now read that EndPoint addresses are immutable - so my code looks wrong anyway as I'd expect to overwrite the address with a new EndPoint address object - rather than a Uri?)</p> <p>How do I can I programmatically override client app.config WCF endpoint addresses? </p> <pre><code>private void InitAllEndpoints() { ServiceModelSectionGroup serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup( ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None)); if (serviceModelSectionGroup != null) { foreach (ChannelEndpointElement ep in serviceModelSectionGroup.Client.Endpoints) { InitEndpoint(ep, appConfig.ExternalComms_scheme, appConfig.ExternalComms_host, appConfig.ExternalComms_port); } } } private void InitEndpoint(ChannelEndpointElement endPoint, string scheme, String host, String port) { string portPartOfUri = String.Empty; if (!String.IsNullOrWhiteSpace(port)) { portPartOfUri = ":" + port; } string wcfBaseUri = string.Format("{0}://{1}{2}", scheme, host, portPartOfUri); endPoint.Address = new Uri(wcfBaseUri + endPoint.Address.LocalPath); } </code></pre> <p>Note: My proxies lives in a separate project/DLL.</p> <p>e.g.</p> <pre><code>public class JournalProxy : ClientBase&lt;IJournal&gt;, IJournal { public string StoreJournal(JournalInformation journalToStore) { return Channel.StoreJournal(journalToStore); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload