Embedd_0913
Embedd_0913

Reputation: 16555

How to write endpoint information in app.config at runtime WCF?

Is there a way to write in a app.config file the information about endpoints? Actually I want to read the <service> tag from one app.config file and want to write it in other app.config file's <Services> tag.

Please anyone tell me how can I do it?

Actually I have a config file called "WCFService.exe.config" which i want to read in my program , so what i am writing is:

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");
Configuration config = ConfigurationManager.OpenExeConfiguration(path)  

ServicesSection serviceSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;

ServiceElementCollection sereleColl = serviceSection.Services;

but i get nothing in sereleColl.

Upvotes: 1

Views: 4502

Answers (2)

marc_s
marc_s

Reputation: 754538

Your calling "OpenExeConfiguration" - this will open the config for the currently executing app.

You need to read up on the .NET 2.0 configuration system: there's an excellent 3-part series on the .NET configuration system on CodeProject:

There's a series of really good articles on you to demystify the .NET 2.0 configuration system on CodeProject:

  1. Unraveling the mysteries of .NET 2.0 configuration

  2. Decoding the mysteries of .NET 2.0 configuration

  3. Cracking the mysteries of .NET 2.0 configuration

Highly recommended! Jon Rista did a great job explaining the configuration system in .NET 2.0.

Upvotes: 3

Ezombort
Ezombort

Reputation: 1922

Take a look at the ConfigurationManager class

I don't remember how to do it though.

Edit:

To access it you have to add a reference to System.Configuration.

Edit 2:

Changing the appsettings can be done like this:

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
AppSettingsSection appSettings = config.AppSettings;
KeyValueConfigurationElement setting = appSettings.Settings["MyAppSettingsKey"];
setting.Value = "newValue";
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

You can access the WCF settings by typing:

ConfigurationSectionGroup sct = config.SectionGroups["system.serviceModel"];

Hope this helps.

Comment:

Your code is working fine here. However, I changed

string path = Path.Combine(Application.StartupPath, "WCFService.exe.config");

to

string path = Application.ExecutablePath;

This will use the config file of the application currently running. I don't know why your path doesn't work. Either it's that, or there must be an error in your config file?

Upvotes: 2

Related Questions