MSBuild Zip File, SVN Revision after Web Publish

As part of an automation part for deploying an application I’ve spent some time creating MSBuild scripts for simple tasks. The following are the tasks I’ll be providing

  • Create a Zip file from the output
  • Read the SVN Revision from the repository

The code will be using inline tasks, since they can be created almost at any time. The inline tasks, lets the user create tasks using C# (and compile against .NET 4.0), at least by the time I am creating this blog post.

Reading the Revision Number using MSBuild

For this particular MSBuild task, I am depending on a third party assembly, SharpSvn. It is a SVN client implement in C#. The tricky part in this script is about loading external assemblies and calling its functions, all the work is doing thru reflection.

<UsingTask TaskName="SVNRevisionReader"               TaskFactory="CodeTaskFactory"               AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
      <SvnPath ParameterType="System.String" Required="true" />
	  <SharpSvnUIDllFilePath ParameterType="System.String" Required="false" />
	  <SvnOutputDirectory ParameterType="System.String" Required="false" />
	  <LastChangeRevision ParameterType="System.Int32" Output="true" />
      <Revision ParameterType="System.Int32" Output="true" />
		<Reference Include="Microsoft.CSharp" />
		<Using Namespace="System"/>
		<Using Namespace="System.Diagnostics"/>
		<Using Namespace="System.IO"/>
		<Using Namespace="System.Reflection"/>
		<Code Type="Fragment" Language="cs">
            //========================= BEGIN: In-line Task
            Func<string, Assembly> loadAssembly = (string assemblyFilePath) =>
                Assembly assembly = null;
				Log.LogMessage("Loading assembly from :" + assemblyFilePath, MessageImportance.High);
                if (!string.IsNullOrEmpty(assemblyFilePath) && System.IO.File.Exists(assemblyFilePath))
                    assembly = Assembly.UnsafeLoadFrom(assemblyFilePath);
                    Log.LogError("Assembly file path:" + assemblyFilePath + " does not exist.", MessageImportance.High);

                return assembly;

            var sharpSvnAssembly = loadAssembly(@"..\MSBuild\SharpSvn.1.9-x86.1.9004.3913.141\lib\net40\SharpSvn.dll");
            //var sharpSvnUIAssembly = loadAssembly(@"..\MSBuild\SharpSvn.1.9-x86.1.9004.3913.141\lib\net40\SharpSvn.UI.dll");
            var typeOfSharpSvnSvnClient = sharpSvnAssembly.GetType("SharpSvn.SvnClient");
            var typeOfSharpSvnSvnTarget = sharpSvnAssembly.GetType("SharpSvn.SvnTarget");
            var typeOfSharpSvnSvnInfoEventArgs = sharpSvnAssembly.GetType("SharpSvn.SvnInfoEventArgs");

            LastChangeRevision = 0;
            Revision = 0;
            //using (var client = new SharpSvn.SvnClient())
            using (dynamic client = Activator.CreateInstance(typeOfSharpSvnSvnClient))
                //SharpSvn.SvnInfoEventArgs info;
                    //client.GetInfo(SharpSvn.SvnTarget.FromString(SvnPath), out info);
                    dynamic svnTarget = sharpSvnAssembly
                            .GetMethod(@"FromString", BindingFlags.Public | BindingFlags.Static, Type.DefaultBinder, new[] { typeof(string) }, null)
                            .Invoke(null, new object[] { SvnPath });
                    var getInfoArgs = new object[] { svnTarget, null };
                        .GetMethod(@"GetInfo", new[] { typeOfSharpSvnSvnTarget, typeOfSharpSvnSvnInfoEventArgs.MakeByRefType() })
                        .Invoke(client, getInfoArgs);
                    dynamic info = getInfoArgs[1];

                    LastChangeRevision = Convert.ToInt32(info.LastChangeRevision.ToString());
                    Revision = Convert.ToInt32(info.Revision.ToString());
                //catch (SharpSvn.SvnInvalidNodeKindException svnInvalidNodeKindException)
                catch (System.Exception exception)

			if (!string.IsNullOrEmpty(SvnOutputDirectory) && System.IO.Directory.Exists(SvnOutputDirectory))
				System.IO.File.WriteAllLines(System.IO.Path.Combine(SvnOutputDirectory, "LastChangeRevision.svn"), new[] { LastChangeRevision.ToString() });
				System.IO.File.WriteAllLines(System.IO.Path.Combine(SvnOutputDirectory, "Revision.svn"), new[] { Revision.ToString() });

            Log.LogMessage(string.Concat("LastChangeRevision: ", LastChangeRevision, " Revision: ", Revision), MessageImportance.High);
            //========================= END: In-line Task

Compressing a directory into a Zip file

The next MSBuild script is about using compressing a directory (normally the output directory after the app has been built).

<UsingTask TaskName="ZipDirectory" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
	<InputDirectory ParameterType="System.String" Required="true" />
    <OutputFilename ParameterType="System.String" Required="true" />
    <Reference Include="System.IO.Compression" />
    <Using Namespace="System.IO.Compression" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[       try       { 		Log.LogMessage(string.Concat("Compressing: ", InputDirectory, " To File: ", OutputFilename), MessageImportance.High); 		 		using (Stream zipStream = new FileStream(Path.GetFullPath(OutputFilename), FileMode.Create, FileAccess.Write)) 		using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) 		{ 			foreach(var filePath in System.IO.Directory.GetFiles(InputDirectory,"*.*",System.IO.SearchOption.AllDirectories)) 			{ 				var relativePath = filePath.Replace(InputDirectory,string.Empty); 				using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))                 using (Stream fileStreamInZip = archive.CreateEntry(relativePath).Open())                     fileStream.CopyTo(fileStreamInZip); 			} 		} 		 		//System.IO.Compression.ZipFile.CreateFromDirectory(InputDirectory, OutputFilename, System.IO.Compression.CompressionLevel.Fastest, true);         return true;       }       catch (Exception ex)       {         Log.LogErrorFromException(ex);         return false;       }     ]]>

The previous code snippets are good enough to get the tasks ready for being used. Depending on the type of project, or the needs the MSBuild tasks can be invoked on different times.

Calling the SVN Reader before starting the build process starts

In order call your Task before the build process starts, it can be done by specifying the BeforeTargets attribute, and setting it to: PrepareForBuild

<Target Name="EnsureSVNRevision" BeforeTargets="PrepareForBuild">
    <SVNRevisionReader SvnPath="$(SolutionDir)" SvnOutputDirectory="$(MSBuildProjectDirectory)">
      <Output PropertyName="SvnLastChangeRevision" TaskParameter="LastChangeRevision" />
      <Output PropertyName="SvnRevision" TaskParameter="Revision" />


Calling the Directory Zip Compressor after the Build is over

In the case trying to execute the zip compression after building, normally it can be called in the Target AfterBuild, e.g.:

  <Target Name="AfterBuild"
		  Condition="'$(Configuration)' == 'Development' Or '$(Configuration)' == 'Stage'"
    <MakeDir Directories="$(SolutionDir).Deployments/$(Configuration)/" Condition="!Exists('$(SolutionDir).Deployments/$(Configuration)/')" />
    <Delete Files="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" Condition="Exists('$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip')" />
    <ZipDirectory OutputFilename="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" InputDirectory="$(TargetDir)\" />


However if you are creating a Web Project, normally you don’t take the output directory because it’s slightly different from the published files (which take few more processing). Web Projects are published, In this case, it’s assumed that it’s published to the local file system (where the MSBuild has access to it). Thus, The AfterTargets can be used to detect when files are ready

<Target Name="AfterWebPublish" AfterTargets="WebPublish"
          Condition="'$(Configuration)' == 'Development' Or '$(Configuration)' == 'Stage'"
    <MakeDir Directories="$(SolutionDir).Deployments/$(Configuration)/" Condition="!Exists('$(SolutionDir).Deployments/$(Configuration)/')" />
    <Delete Files="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" Condition="Exists('$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip')" />
    <ZipDirectory OutputFilename="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" InputDirectory="$(MSBuildProjectDirectory)\$(publishUrl)\" />
    <RemoveDir Directories="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName)/" Condition="Exists('$(SolutionDir).Deployments/$(Configuration)/$(ProjectName)/')" />

With the previous snippets it is possible to customize more in detail when the Tasks should be executed to get the work done.


How embed fonts in Windows Phone 8 using a WebBrowser control


I hope I will be sharing some ideas from my last project. This in particular was difficult.

The problem

I have a font I want to embed into my Windows Phone Application, and I want to use in a WebBrowser Control. I have managed to embed the font and use it with standard textblocks control, however I haven’t get it working with the WebBrowser control.

The Solution

Using Data Url Scheme for embedding the font ( http://en.wikipedia.org/wiki/Data_URI_scheme ). Then let the WebBrowser control to consume the font.

First things I learned it’s that a Data Url can contains basically data (e.g.images, fonts, more files. it’s really handy). Moreover, I learned that CSS can consume this data, which makes a good option to embed the font and use it off line. There may be few bumps, such as using Base64 encoding, but one we get the rhythm it won’t hurt more.

1) In your window phone project select add the font file. Here is a remark, make sure the file format is compatible with Windows Phone (TTF files) or the Windows Phone Web Browser (WOFF). TTF may require some work to get them working , for example enabling the flag for allow the file to be embedded, or making sure that its internal file structure is compatible with WP platform.

2) When embedding the file, make sure that the file properties are set: Build Action to Content and Copy to Output Directory to Copy if newer.

3) Prepare some HTML for the setting up the font. In my project sample, I’ll be using Navigate to String method and to keep HTML code apart from my C# code, I’ll be loading an external file from the XAP package, and applying some text replacements with Regular Expressions.

4) In out sample check MainPage.xaml.cs to see how the FileManager to apply the custom font.

I haven’t tried, but I think this approach could be extended to inject a custom font to an external page. Here is the output:


The code sample is at


I hope you could find this handy,



After moving some files my Windows Phone Project Throws a XamlParseException

I am not sure if this is an known issue for Microsoft, but at least I am aware of this. However this is the first time I am gonna document it. Since it can be very frustrating

The Problem

I want to reorganize the files in my Windows Phone Project in order to follow our implementation of MVVM, moreover we have decided that resx file will live in their own assembly project. However, once I move the Resource file to an external assembly the application just thrown an XamlParseException even though the reference in XAML is totally correct.

XamlParseExceptions may be thrown by other reasons. It is important to realize that I knew I moved the RESX file to a different assembly and before that everything was working. Certainly I updated the reference to the new assembly, and the exception was still being thrown. That is why this is a tricky issue.

The solution

It took some time to me to noticed, but somehow the project that contains the RESX file cannot contain a dot (.) character in its assembly name. If it happens then XAML parse will throw a XamlParserException even though the reference to the assembly is correct. The error message may contain something like this:

System.Windows.Markup.XamlParseException occurred
  Message=Unknown parser error: Scanner 2147500037. [Line: 10 Position: 126]
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at MyApp.App.InitializeComponent()
       at MyApp.App..ctor()

Taking a look at the location where I was loading the Resource it was this:


    <!--Application Resources-->
        <local:LocalizedStrings xmlns:local="clr-namespace:MyApp;assembly=MyApp.WindowsPhone8.Resources" x:Key="LocalizedStrings"/>

        <!--Required object that handles lifetime events for the application-->
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>


The line one of my projects generates an assembly named: ‘MyApp.WindowsPhone8.Resources’, in order to resolve the issue I only have to update the generated assembly name to be ‘MyApp_WindowsPhone8_Resources’ and then update the proper reference in the XAML. For example:

        <local:LocalizedStrings xmlns:local="clr-namespace:MyApp;assembly=MyApp_WindowsPhone8_Resources" x:Key="LocalizedStrings"/>

After performing this change your app should work normally.

Custom Configuration files in .NET


I would like to share some ideas about how to deal with configuration files on .NET. I am sure that there should be many options for implementing configuration files. Some are more likely to better than others and others may depend on the platform (e.g. mobile, desktop, cloud, etc..)  In particular I’ll explain an approach that should be working for your Web Application and for your Desktop application.

The problem:

We want to quickly create a configuration file, since we foresee that our application will have a good amount of settings.


I’ll take advantage of the default .NET mechanism for configuration files. It’s plenty flexible and quite extensible. The major drawback is that you’ll have to write code, and sometimes a not-so-easy code. Alternatively you can relay on the existent section handlers, and try to use them when possible.

A brief talk about configuration files

.NET configuration files supports hierarchies and they are extensible. Almost all the cases with defaults is fairly enough, for creating custom sections there I will be explaining three approaches, but certainly there are more. The decision of which approach to take it will depend of how much time you have and also if the team has the will and chance for installing at least the Configuration Section Designer. 

1) Create Custom Section Handler with Code Snippets

This is the simplest one, it’s basically get code snippets of how to create sections for the SectionHandler class. Then you are more likely to have many property of similar times (probably primitive types) but it can safe time for producing the class and understand it. I won’t be providing samples for this.

2) Using the Custom Section Designer from CodePlex at https://csd.codeplex.com/

This is my favorite, but unfortunately it demands that you will have to install a Visual Studio Extension for opening configuration section projects. The best of this approach is that you can get:

  • XSD validation + intelisense
  • XSD Documentation
  • Ease to modify content.
  • etc…

First get proper installer for the extension https://csd.codeplex.com/. Then create a configuration section project (these projects outputs a dll), by conversion these projects end with “.Configuration” for example “MyApp.Configuration” will be dll project that loads the configuration section. In our sample it is provided a project called: ConfigurationFileSample.Configuration


The previous screenshot exemplifies a Section in which it has been defined a Element called Mappings, which is a collection of Mapping elements. By following the instructions at https://csd.codeplex.com/wikipage?title=Defining%20new%20types&referringTitle=Usage you will easily create this configuration. The magic arrive when you are typing these values into visual studio (or any XML editor that supports XSD validation property).

Once you are done designing your configuration file. You save and compile. If it success then add a configuration file to your main project, and also add a reference to the configuration file project. for referencing our custom section we do it into the configSections element. For example:

<!-- NOTE: In here a custom section is specified, this section has been created by writing code--></pre>

Visual studio will enable help toltips and XSD validation for you. As you can see in the following screenshot


For loading configuration files in code, after adding the reference to the dll, the app can use an static helper of simply use the GetSection method.

For example for loading the section using the default instance:

// NOTE: It is also possible to load the default custom section
Console.WriteLine("\nLoading default Minestrone thru singleton!");
var minestroneSection = Minestrone.MinestroneSection.Instance;

Or also you could load a specific section:

// NOTE: You could ask for a given custom section
Console.WriteLine("\nLoading Minestrone by manually specifying a section: minestroneSection");
var minestroneSection = ConfigurationManager.GetSection("minestroneSection") as Minestrone.MinestroneSection;

3) Using one of the build in Section Handlers
As I mentioned you also could you the built-in Sectionhandlers from .NET framework There is a list of them at the end of this page: http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx (basically all the subclasses of System.Configuration.ConfigurationSection )

For example for referencing it into the configuration file:

    <!-- NOTE:This custom section uses .NET framework sections instead, see http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx for a list of the classes available into the framework --></pre>

and for loading it from code:

// NOTE: Loads a custom section, but it uses a .NET built-in class
            Console.WriteLine("\nLoading a ApplicationSettings using a built-in .NET type. (more at http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx ) ");
            var myConfigSection = ConfigurationManager.GetSection("myConfigSection") as System.Collections.Specialized.NameValueCollection;
            for (int appSettingIndex = 0; appSettingIndex < myConfigSection.Count; appSettingIndex++)
                Console.WriteLine(string.Format("Key:{0} Value:{1}", myConfigSection.AllKeys[appSettingIndex], myConfigSection[appSettingIndex]));
            Console.WriteLine("\n\n PRESS ANY KEY TO FINISH! ");

We are almost there, but WAIT!!!!

I know that at this time I haven’t shown how to specify section. Well basically because you can provide the details of the section inline into the same file or specifcying an external file using the configSource attribute (which is available for any custom section).

For example see how the minestroneSection is defined in an external file, and how myConfigSection  is written inline into the configuration file. There are advantages of each approach (e.g. when you want to apply XSLT transformations you may want to apply transformations to a simple file rather than a long and complex XML) o perhaps you may have a config file per environment (e.g. minestrone.debug.config…,)

<?xml version="1.0" encoding="utf-8" ?>
    <!-- NOTE: In here a custom section is specified, this section has been created by writing code-->
    <section name="minestroneSection" type="Minestrone.MinestroneSection, ConfigurationFileSample.Configuration, Version=, Culture=neutral, PublicKeyToken=null"/>

    <!-- NOTE:This custom section uses .NET framework sections instead, see http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx for a list of the classes available into the framework -->
    <section name="myConfigSection" type="System.Configuration.AppSettingsSection, System.Configuration, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

    <add key ="ALPHA" value="1|1"/>
    <add key ="BETA" value="1|1"/>
    <add key ="GAMA" value="1|1"/>

  <!-- NOTE: Loading section from a external file-->
  <minestroneSection configSource="minestroneSection.config" />

    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

I hope this is good enough for setting up quickly custom configuration files in your .NET projects.
The sample is at https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNet.CustomConfigFile and please remember to install the Custom Section Designer if you want to take the option for more detailed configuration file.


Previewing Microsoft Office Documents and PDF in WPF using a Preview Handler Host


I had recently to implement a mechanism for showing documents into a WPF application. The very first solution was to start a new process using the path of the selected file. The only condition for this to work it’s that the file extension should have been previously registered to be opened with an specific application. A drawback is that the document is shown by an external application that we are not able to handle (at all or at least easily).

Then the client decided showing documents by embedding them into the application. So, I started researching about how to display Portable documents, Word documents, Excel documents into a WPF application. Finally after some research I got to a nice solution which will display many documents formats with the only condition of having the proper software installed into the PC where the application is gonna run.

The Problem
Showing a office document (XLS, DOC, XLSX, DOCX) or portable document (PDF) into a WPF APP.

This is not a bullet proof solution, but it is kind extensible for brave developers. The relays on the installed Preview Handlers. For example for visualizing Microsoft Office documents you will have to have installed Microsoft Office in the computer where the application is going to run. It’s the same case for PDF files, where you need to have a Preview Handler installed.
Moreover, the technology of Preview Handler has been built using native code, in order to embedded into a WPF application the app will be hosting WinForm content, this introduces a different set of restrictions about the interaction of the control once it has been instantiated.

The Solution
Preview Handlers is a technology added into Windows Vista, and it’s meant to readonly quick preview of files. It’s available on Windows Explorer
Enabling Previews in Windows Vista Folders
The Microsoft article [1] about Preview Handlers has an utility application which lists the available preview handlers in your system. Moreover after some research, I found the first implementation in [2] which was using Windows Forms and C# for development. Finally I found a more elaborated solution from Coding4Fun [3]. Since this point the solution for WPF was straightforward because it was only matter of loading a WinForm contro into WPF by using Interop components.

1) Grab the code from http://c4fdevkit.codeplex.com/ project. To use the Preview Handler Host you will need only two projects.
/Preview Handlers/Source/VCS/PreviewHandlerFramework
/Preview Handlers/Source/VCS/PreviewHandlerHost

Compile them and the create a new WPF project. The codeplex project hasn’t been updated in awhile, thus you’re more likely to have to update the solution file to a new version of visual studio.

2) Add to your WPF application (or library) a reference to WindowsFormIntegration library. This will allow the application to host WinForm controls. Also add System.Windows.Forms since it will be required by the Preview Handler libraries.

3) The use the WindowsFormsHost tag to embed the PreviewHandlerHostControl as the follwing XAML shows:


            <C4F_DevKit_PreviewHandler_PreviewHandlerHost:PreviewHandlerHostControl x:Name="previewHandlerHostControlInstance" />

4) Finally our C# code that for our sample is quite simple. We just select a file and let the PreviewHandlerHost control do the work.

using System;
using System.Windows;

namespace PreviewHandlerWpfApplication
    public partial class MainWindow : Window
        public MainWindow()

        private void Button_Click(object sender, RoutedEventArgs e)
            var dlg = new Microsoft.Win32.OpenFileDialog();
            if (dlg.ShowDialog().Value)
                previewHandlerHostControlInstance.FilePath = dlg.FileName;

Here is an screenshot of my mighty document viewer:


Finally the code is available at GitHub https://github.com/hmadrigal/playground-dotnet/tree/master/MsDesktop.PreviewHandlers Have fun!




Automatic retries using the Transient Fault Handling from Enterprise libraries (EntLib)


I was reading about the Transient Fault Handling at http://msdn.microsoft.com/en-us/library/hh680901(v=pandp.50).aspx This is a block of the Enterprise Libraries which is part of the Enterprise Integration Pack for Windows Azure. I haven’t played much with Azure yet, but one part of the documentation took my attention:

When Should You Use the Transient Fault Handling Application Block?
…… (some explanation about when you are using Azure services) ……
You Are Using a Custom Service
If your application uses a custom service, it can still benefit from using the Transient Fault Handling Application Block. You can author a custom detection strategy for your service that encapsulates your knowledge of which transient exceptions may result from a service invocation. The Transient Fault Handling Application Block then provides you with the framework for defining retry policies and for wrapping your method calls so that the application block applies the retry logic.Source: http://msdn.microsoft.com/en-us/library/hh680901(v=pandp.50).aspx#sec10

So, I thought why do I have to write always the retry logic if this is already done ;-).

The problem

We want to write a generic retry mechanism for our application. Moreover we need to support some level of customization thru configuration files.

The solution
Simple let set up the Transient Fault Handling Block (from now on FHB). First of fall you will have to download the libraries, you could do it using NuGet – which by default adds also references to support Windows Azure -. You can remove the Windows Azure DLL if you’re not going to be using Windows Azure in your app.

The FHB lets you specify a error detection strategy as well as retry strategy when you construct the retry policy. Also, a Retry policy support the execution of Action, Funcdelegates or Actionfor async retries.

As part of the configuration process for FHB, it’s required to indicate which errors should be handled by FHB. A mechanism for doing this is by implementing the interface: ITransientErrorDetectionStrategy. In our sample, we only want to retry when a FileNotFoundException is thrown. Thus we have coded the FileSystemTransientErrorDetectionStrategy:

using System;
using System.IO;
using Microsoft.Practices.TransientFaultHandling;

namespace SampleConsoleApplication.TransientErrors.Strategies
    public class FileSystemTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
        #region ITransientErrorDetectionStrategy Members

        public bool IsTransient(Exception ex)
            return ex is FileNotFoundException;


Certainly you could implement it by handling any particular exception.
Now we need to define a retry policy. This is done by specifying a retry strategy. The following code example configures a retry policy by providing a retry strategy incremental. Additionally in our sample service.DoSlowAndImportantTask will always fail, so the FHB will retry automatically based on the retry policy.

 private static void RetryPolityUsingCode(IUnityContainer container, IService service, OutputWriterService writer)
            writer.WriteLine("Begin sample: RetryPolityUsingCode");
            // Define your retry strategy: retry 5 times, starting 1 second apart
            // and adding 2 seconds to the interval each retry.
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            // Define your retry policy using the retry strategy and the Windows Azure storage
            // transient fault detection strategy.
            var retryPolicy = new RetryPolicy(retryStrategy);

                // Do some work that may result in a transient fault.
            catch (Exception exception)
                // All the retries failed.
                writer.WriteLine("An Exception has been thrown:\n{0}", exception);
            writer.WriteLine("End sample: RetryPolityUsingCode");

Additionally it’s possible to specify the retry policy in the configuration file, for example:

<!--?xml version="1.0" encoding="utf-8" ?-->

<section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling" requirepermission="true"></section><section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common"></section>

Please notice that there is a default Retry Strategy indicating that will use “Fixed Interval Retry Strategy”. Our C# code will ask for the RetryManager instance in order to retrieve the Retry Policy and then perform the task.

private static void RetryPolityUsingConfigFile(IUnityContainer container, IService service, OutputWriterService writer)
            writer.WriteLine("Begin sample: RetryPolityUsingConfigFile");
            // Gets the current Retry Manager configuration
            var retryManager = EnterpriseLibraryContainer.Current.GetInstance();

            // Asks for the default Retry Policy. Keep on mind that it's possible to ask for an specific one.
            var retryPolicy = retryManager.GetRetryPolicy();
                // Do some work that may result in a transient fault.
            catch (Exception exception)
                // All the retries failed.
                writer.WriteLine("An Exception has been thrown:\n{0}", exception);
            writer.WriteLine("End sample: RetryPolityUsingConfigFile");

The Transient Fault Handling Block a simple and flexible alternative for a well-known task. A automatic retry system, moreover this supports configuration file, so we should be able to change the retry policy just by modifying our configuration file.

Here is an screen shot of the app, which automatically retries calling our mischievous method.

Transient Fault Handling Tries Output Capture

As usual the code sample is at https://github.com/hmadrigal/playground-dotnet/tree/master/MsEntLib.TransientErrorHandling

Multiple configuration files per environment


Today I had the chance to perform a quick research about a classic problem. If have different settings for development, testing, staging and production. So, we usually merge or lost settings of the configuration files. Visual Studio 2008 have already resolved the problem (at least in web applications) by allowing the user to create different web.config files and applying XSLT to generate the final configuration file. Today I’ll propose the same solution, but for desktop Apps, and hopefully you could apply this solution to any XML file.

Just in case you’re one of those who want to download a sample, you coudl get it at https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNet.MultipleConfigurationFile

The Problem:

We have a configuration file ( .config or any xml document), and we want to have different settings per environment (development, testing, staging and production). Moreover we want to automate this process, so we don’t have to write the settings manually.


  • It’s good to know basics of MSBuild and how to modify project.
  • It’s good to know about XML, XPath and XSLT

The solution

Create one configuration per environment 

Let’s start by creating configuration settings for each environment where you need custom settings. You could do it by click on “Configuration Manager…“. For example I’ve created the environments that we’re gonna use on this example.


Now, add a configuration file (in our case app.config) and also add one config file per environment. Please note that the configuration file app.config contains most of the final content, and the other configuration files will hold only XSLT expressions to modify the original. As a convention usually the file names are: app.$(environment).config. The following image illustrates the configuration files:


More in detail app.config, contains three simple settings:

<?xml version="1.0" encoding="utf-8"?>
    <add key="EnvironmentName" value="Debug" />
    <add key="ApplicationDatabase" value="Data Source=DevelopmentServer\SQLEXPRESS;Initial Catalog=DevelopmentDatabase;Integrated Security=True;" />
    <add key="ServerSharedPath" value="\\" />

Set up your project to use a XslTransform task once the compilation is done

Now you will have to edit the project file using a text editor. VS project files are XML, these normally are read by VS and see the project structure. However there are tasks that only can be customized by editing manually the XML file. You can modify the xml of a project within Visual Studio by using right (secondary) click on the project from the Solution Explorer. Then, click on “Unload project”, and once again right click and now select “Edit project …”. Now, you should be able to see the XML associated to the selected project.

You’ll be adding two targets (or if modifying if any of them is already in place). The following code:

<Target Name="ApplySpecificConfiguration" Condition="Exists('App.$(Configuration).config')">
    <XslTransformation XmlInputPaths="App.config" XslInputPath="App.$(Configuration).config" OutputPaths="$(OutputPath)$(RootNamespace).$(OutputType).config" />
    <Copy SourceFiles="$(OutputPath)$(RootNamespace).$(OutputType).config" DestinationFiles="$(OutputPath)$(RootNamespace).vshost.$(OutputType).config" />
  <Target Name="AfterBuild">
    <CallTarget Targets="ApplySpecificConfiguration" />

The previous XML portion Adds a custom task called and it uses MSBuild variables to determine the name of the configuration file, our example is using a Console App, thus we need to create a copy of the config file but with vshost.config extension, so we added a copy task.  The second thing we’ve added is a call to the task we’ve just created, this call is added into the AfterBuild Target. The AfterBuild is invoked by MSBuild automatically when the build process has finished.

Transform your app.config with XSLT

On this section, we’re gonna write the content for each configuration. This will let us custom XML porting into the final configuration file. As example, here is the XML for the production transformation:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <!-- Nodes to be replaces -->
  <xsl:template match="/configuration/appSettings/add[@key='EnvironmentName']">
    <add key="EnvironmentName" value="Production" />
  <xsl:template match="/configuration/appSettings/add[@key='ApplicationDatabase']">
    <add key="ApplicationDatabase" value="Data Source=PRODUCTION\SQLEXPRESS;Initial Catalog=ProductionDatabase;Integrated Security=True;" />
  <xsl:template match="/configuration/appSettings/add[@key='ServerSharedPath']">
    <add key="ServerSharedPath" value="\\PRODUCTION" />

  <!-- Copy all the remaining nodes -->
  <xsl:template match="node()|@*">
      <xsl:apply-templates select="node()|@*"/>

I know XSLT might sound intimidating at first, but it’s kinda easy.BTW XSLT uses XPath, so if you want to know the basics of them, please check out w3c school for a quick reference. XPath at http://www.w3schools.com/xpath/xpath_examples.asp and XSLT at http://www.w3schools.com/xsl/xsl_transformation.asp

To put it in a nutshell, our XSLT is detecting specifically three attributes (by its name and location into the XML) and replacing them with a custom value. All the remaining nodes are kept into the final XML.

Let run our sample

Just to make it work I’ve coded two classes, one that defines the constant name of the settings and a main class which print values from the configuration file.

using System;
using System.Configuration;

namespace MultipleConfigurationFilesSample
    public static class ApplicationConstants
        public class AppSettings
            public const string ServerSharedPath = @"ServerSharedPath";
            public const string EnvironmentName = @"EnvironmentName";
            public const string ApplicationDatabase = @"ApplicationDatabase";
    class Program
        static void Main(string[] args)
            var environment = ConfigurationManager.AppSettings[ApplicationConstants.AppSettings.EnvironmentName];
            var serverSharedPath = ConfigurationManager.AppSettings[ApplicationConstants.AppSettings.ServerSharedPath];
            Console.WriteLine(@"Environment: {0} ServerSharedPath:{1}", environment, serverSharedPath);

Now I have run the application twice, but using different configurations. The first was using development and I got the following output:


The second was using Production configuration, and it produces the following output:


As you can see, the selected environment is determining the settings into the resultant configuration file.

As usual the source code can be fond at https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNet.MultipleConfigurationFile

References: http://litemedia.info/transforming-an-app-config-file

Best regards,