Pieter Vandenheede Pieter Vandenheede - 11 months ago 61
C# Question

Unable to add behaviorExtension in machine.config via c#

My BizTalk application requires me to add a custom behaviorExtension to my machine.config file. I install my application via MSI, via BizTalk Deployment Framework (BTDF), so I would like this to be done programmatically as well.

Now I cannot seem to find a way to either list installed behaviors not edit them.

I have the following code, but after that I'm stuck.

// Get the machine.config file.
Configuration machineConfig = ConfigurationManager.OpenMachineConfiguration();
// Get the machine.config file path.
ConfigurationFileMap configFile = new ConfigurationFileMap(machineConfig.FilePath);

// Map the application configuration file to the machine
// configuration file.
Configuration config = ConfigurationManager.OpenMappedMachineConfiguration(configFile);

ConfigurationSectionGroup svcModel = config.SectionGroups.Get("system.serviceModel");
ConfigurationSection extensions = svcModel.Sections.Get("extensions");

Can anyone give me some pointers on how to approach this?

Evk Evk
Answer Source

You are almost there. Your extensions variable is of type System.ServiceModel.Configuration.ExtensionsSection, which has property BehaviorExtensions containing what you are looking for. So:

var extensions = (System.ServiceModel.Configuration.ExtensionsSection) svcModel.Sections.Get("extensions");
var behaviors = extensions.BehaviorExtensions;