Adding behaviorExtensions to machine.config via C#

I’ve been playing around with BizTalk Deployment Framework lately and for one particular BizTalk application, I need to add 4 custom behaviorExtensions. I’ve had some very specific logic that I needed to put into some WCF Message Inspectors.

When you think about automating the installation of a BizTalk application, you don’t want to be manually adding the behaviorExtensions to the machine.config. So I set out to add these via a C# application. Seems this was not as trivial as I though it would be.

First things first, we need to be able to retrieve the location of the machine.config file:

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

The above code, will give you the path of the machine.config file, depending on what runtime (x86 or x64) you are running the code under.

When running as x86, you will get the following path:

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config"

When running as x64, you will get the following path:

"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\machine.config"

Once the path has been found, we need to open the file and position ourselves at the correct location in the file (system.serviceModel/extensions):

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

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

Now this is the point where I initially got stuck. I had no idea I had to cast the Section as a System.ServiceModel.Configuration.ExtensionsSection. Doing so, allows you to add your behaviorExtension in the config file as such:

ExtensionElement e = new ExtensionElement("MyName", "MyType, MyNamespace.MyType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4275f802b89cdd22");
 extensions.BehaviorExtensions.Add(e);
 extensions.SectionInformation.ForceSave = true;

config.Save(ConfigurationSaveMode.Full);

Don’t forget to set the ForceSave, as – without it – it doesn’t seem to write the update.

All together, this gives you the following code:

// 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");
 ExtensionsSection extensions = (ExtensionsSection) svcModel.Sections.Get("extensions");

ExtensionElement e = new ExtensionElement("MyName", "MyType, MyNamespace.MyType, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4275f802b89cdd22");
 extensions.BehaviorExtensions.Add(e);
 extensions.SectionInformation.ForceSave = true;

config.Save(ConfigurationSaveMode.Full);

If you do like me and you want to adapt the x86 AND the x64 machine.config file, just replace the “Framework” in the x86 machine.config path into “Framework64” before doing the same.

I made myself a simple console application which allows me to call it while running the MSI for my BizTalk application. Only make sure the MSI runs as an administrator!

 

This post has been cross-posted at http://www.codit.eu/blog/2016/05/31/adding-behaviorextensions-to-machineconfig-via-c/

BizTalk Software Factory on VS2010

Did you ever hear of “BizTalk Software Factory“?
Good chance you haven’t, looking at the lack of love it seems to be getting on CodePlex. Until a few days ago, I was in that same spot…

So today I tried to install BizTalk Software Factory on a BizTalk Server 2010 development machine. Trying to find out what it actually does, since it I never heard of it before and to be honest: documentation seems to be scarce! Reading the instructions seemed promising and who knows, it might help in getting things automated and potentially be a time-saver!

Diving in

I had some issues getting it to run. I have a VM with Visual Studio 2010 Premium on it, since that is the one which is included into my MSDN subscription.
Being somebody who generally doesn’t read too much instructions, since I think I will get it working anyway, I found myself in trouble faster than expected…. Getting the BSF extension installed, quickly hit me in the face:

elpmckv

Note: The installer somehow mentions I have Ultimate installed, but I do have VS2010 Premium installed

img

The “View Install Log” baffled me, it said:

23/05/2016 0:45:07 - Searching for applicable products...
23/05/2016 0:45:07 - Found installed product - Microsoft Visual Studio 2010 Ultimate
23/05/2016 0:45:07 - The extension with ID 'BizTalkSoftwareFactory' is not installed to Microsoft Visual Studio 2010 Ultimate.
23/05/2016 0:45:07 - Found installed product - Microsoft Visual Studio 2010 Premium
23/05/2016 0:45:09 - The following target products have been selected...
23/05/2016 0:45:09 - Microsoft Visual Studio 2010 Ultimate
23/05/2016 0:45:09 - 
23/05/2016 0:45:09 - Beginning to install extension to Microsoft Visual Studio 2010 Ultimate...
23/05/2016 0:45:09 - Install Error : Microsoft.VisualStudio.ExtensionManager.MissingReferencesException: This extension cannot be installed because the following references are missing:
- Guidance Automation Extensions
 at Microsoft.VisualStudio.ExtensionManager.ExtensionManagerService.BeginInstall(IInstallableExtension installableExtension, Boolean perMachine, AsyncOperation asyncOp)
 at Microsoft.VisualStudio.ExtensionManager.ExtensionManagerService.InstallWorker(IInstallableExtension extension, Boolean perMachine, AsyncOperation asyncOp)

Perhaps time to actually read the installation instructions…yes, I know they are on the front page, but don’t judge me, it takes some googling.

Apparently there are several dependencies:

I was able to find them all by performing a quick Google search (links above).

Everything installed correctly, except the Visual Studio 2010 SDK:

img

This blog post helped me to realize I have Visual Studio 2010 SP1 and therefore needed to install Visual Studio 2010 SDK SP1 as well. Makes sense, once you realize it…

Once the Visual Studio 2010 SDK SP1 was installed, I was able to install BSF as well:

otwihhb

EUREKA! So, all set to go!
I open up an existing solution and project and expect to be able to add a new project, with all the new fancy stuff from BSF. Unfortunately, that’s not how it works!

BSF only seems to work with new solutions, since it will create a complete range of projects for your new solution in one go!
Therefore, close any existing solutions you have open and just create a new project.

At last, there we have the BSF project-type available:

rg1uehh

Click the Add button once you are satisfied with the settings and behold the magic:

bzufy8v

You can even provide a single .snk for all of your projects, or combine all project types into one single project:

og8pich

Now that’s pretty cool and saves you quite some time!

Here is another screenshot of the solution that was created now:

k61dfpn

Note that I didn’t provide a snk-file during creation.

Conclusion

BizTalk Software Factory is a nifty little thing!

It will create a BizTalk solution with the type of projects you need. It will include references to BizTalk Deployment Framework (BTDF) with build scripts (.bat files) and deployment scripts already included, which is another plus. What is even nicer is that it will already set your BTDF deployment project with the correct ItemGroups and properties set! Now that’s a time-saver!

ngevz2h

znsl4he

Many thanks to Jean-Paul Smit (Twitter) for the creation of the tool. I’m sure I will use it in the future!