How to Programmatically Modify Wcf App.Config Endpoint Address Setting

How to programmatically modify WCF app.config endpoint address setting?

I think what you want is to swap out at runtime a version of your config file, if so create a copy of your config file (also give it the relevant extension like .Debug or .Release) that has the correct addresses (which gives you a debug version and a runtime version ) and create a postbuild step that copies the correct file depending on the build type.

Here is an example of a postbuild event that I've used in the past which overrides the output file with the correct version (debug/runtime)

copy "$(ProjectDir)ServiceReferences.ClientConfig.$(ConfigurationName)" "$(ProjectDir)ServiceReferences.ClientConfig" /Y

where :
$(ProjectDir) is the project directory where the config files are located
$(ConfigurationName) is the active configuration build type

EDIT:
Please see Marc's answer for a detailed explanation on how to do this programmatically.

Can I programmatically override client app.config WCF endpoint addresses?

The only way I have done this is to replace the EndpointAddress on each constructed instance of a client.

using (var client = new JournalProxy())
{
var serverUri = new Uri("http://wherever/");
client.Endpoint.Address = new EndpointAddress(serverUri,
client.Endpoint.Address.Identity,
client.Endpoint.Address.Headers);

// ... use client as usual ...
}

WCF - how programmatically set bindingconfiguration?

done work code so:

InstanceContext context = new InstanceContext(new ChatClient(numberclient));
NetPeerTcpBinding binding = new NetPeerTcpBinding();

EndpointAddress endpoint = new EndpointAddress("net.p2p://ChangingName123/FileServer");
binding.Resolver.Custom.Address = new EndpointAddress("net.tcp://191.14.3.11/FileServer");
binding.Security.Mode = SecurityMode.None;
NetTcpBinding ntcp = new NetTcpBinding();
ntcp.Security.Mode = SecurityMode.None;
binding.Resolver.Custom.Binding = ntcp;
factory = new DuplexChannelFactory<IChatChannel>(context, binding, endpoint);
channel = factory.CreateChannel();

WCF change endpoint address at runtime

So your endpoint address defined in your first example is incomplete. You must also define endpoint identity as shown in client configuration. In code you can try this:

EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
var address = new EndpointAddress("http://id.web/Services/EchoService.svc", spn);
var client = new EchoServiceClient(address);
litResponse.Text = client.SendEcho("Hello World");
client.Close();

Actual working final version by valamas

EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
Uri uri = new Uri("http://id.web/Services/EchoService.svc");
var address = new EndpointAddress(uri, spn);
var client = new EchoServiceClient("WSHttpBinding_IEchoService", address);
client.SendEcho("Hello World");
client.Close();

Replace configuration in app.config with code configuration for WCF service

It should work

 var id = new EntityInstanceId
{
Id = new Guid("682f3258-48ff-e211-857a-2c27d745b005")
};

var endpoint = new EndpointAddress(new Uri("http://server/XRMDeployment/2011/Deployment.svc"),
EndpointIdentity.CreateUpnIdentity(@"DOMAIN\DYNAMICS_CRM"));

var login = new ClientCredentials();
login.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

var binding = new CustomBinding();

//Here you may config your binding (example in the link at the bottom of the post)

var client = new DeploymentServiceClient(binding, endpoint);

foreach (var credential in client.Endpoint.Behaviors.Where(b => b is ClientCredentials).ToArray())
{
client.Endpoint.Behaviors.Remove(credential);
}

client.Endpoint.Behaviors.Add(login);
var organization = (Organization)client.Retrieve(DeploymentEntityType.Organization, id);

Here you may find the example of using code instead of config file

How to programmatically change an endpoint's identity configuration?

I don't think there is a way to do this built into the framework, at least I haven't seen anything easy but could be wrong.

I did see an answer to a different question, https://stackoverflow.com/a/2068075/81251, that uses standard XML manipulation to change the endpoint address. It is a hack, but it would probably do what you want.

Below is the code from the linked answer, for the sake of completeness:

using System;
using System.Xml;
using System.Configuration;
using System.Reflection;

namespace Glenlough.Generations.SupervisorII
{
public class ConfigSettings
{

private static string NodePath = "//system.serviceModel//client//endpoint";
private ConfigSettings() { }

public static string GetEndpointAddress()
{
return ConfigSettings.loadConfigDocument().SelectSingleNode(NodePath).Attributes["address"].Value;
}

public static void SaveEndpointAddress(string endpointAddress)
{
// load config document for current assembly
XmlDocument doc = loadConfigDocument();

// retrieve appSettings node
XmlNode node = doc.SelectSingleNode(NodePath);

if (node == null)
throw new InvalidOperationException("Error. Could not find endpoint node in config file.");

try
{
// select the 'add' element that contains the key
//XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
node.Attributes["address"].Value = endpointAddress;

doc.Save(getConfigFilePath());
}
catch( Exception e )
{
throw e;
}
}

public static XmlDocument loadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(getConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
}
}

private static string getConfigFilePath()
{
return Assembly.GetExecutingAssembly().Location + ".config";
}
}
}


Related Topics



Leave a reply



Submit