Friday, April 24, 2015

ConfigurationTransformations Nuget

As in my recent work I had to deal with some configuration transformations from C# code I've created a nuget that can be used if you want to apply a transformation programmatically. To do that you can install the ConfigurationTransformation package from the official nuget repository.
In order to use it you can call the following from the package manager console:
PM> Install-Package ConfigurationTransformations

and then just create an instance of the ConfigTransformer class to apply the transformation.
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string destinationFile = @"C:\temp\resultweb.config";
TransformationResult transformationResult = configTransformer.ApplyTransformation(sourceFile, transformationFile, destinationFile);
If you don't specify the destination file the source file content will be changed with the result transformated content.

The other option is to get the result content without saving it to a file:
ConfigTransformer configTransformer = new ConfigTransformer();
string sourceFile = @"C:\temp\web.config";
string transformationFile = @"C:\temp\web.Release.config";
string transformedContent = configTransformer.GetTransformedFileContent(sourceFile, transformationFile);

1 comment:

  1. I think i've found a bug in the function ApplyTransformation()
    If i call the function with different sourceFile and destinationFile, the sourceFile is locked after the Function-Call. So i can't access the sourceFile after the transforming.

    If i use the sourceFile as the destinationFile, i have no problems to access the sourceFile after the function call.

    Don't work:
    ct.ApplyTransformation(sourceFile, transformfile, destinationFile);

    work:
    ct.ApplyTransformation(sourceFile, transformfile, sourceFile);

    ReplyDelete