Thursday, 18 April 2013

CI build, deployment Items and Copy to output directory


As I started to write this blog post, I must admit, I struggled with find an appropriate heading for it. We can put this blog post in the "How I got burnt category" category.

So, we are running our CI builds using TFS 2012 and with very little customisation to the out-of-the-box build template and it all works a treat. However, occasionally our builds are failing with the following error

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets (3686): Unable to copy file "C:\CI\77\src\...." to "C:\CI\77\bin\....". Access to the path 'C:\CI\77\bin\....' is denied.


Looking closely, there were two traits of the files for which we were getting this error.

  1. The files had the "Copy to Output directory" property set to "Copy Always".

  1. The files were present as an "Deployment Item" in a Test class.

We had a few XAML files, which had to be loaded dynamically in our tests, so the DeploymentItem was needed.

The error happened in the  "_CopyOutOfDateSourceItemsToOutputDirectoryAlways" target in the Microsoft.Common.targets file, which is executed when MSBUILD is attempting to copy all files with the setting "Always Copy to output directory" to the output directory.

The error seems to have happened because the CI Build tried to copy a file after it has been deployed as a deployment item. We did have BuildInParallel attribute set to true. In our case, we realised that the "Copy to output directory" was not needed, so we changed the setting to "Do not copy". In other scenarios where both the conditions are needed, the solution would be to set the BuildInParallel parameter to false. 

Tuesday, 16 April 2013

Clearing Windows Server AppFabric databases


If you have been using Windows Service AppFabric, you would be aware of the two AppFabric databases. The AppFabric Monitoring database and AppFabric Persistence database. The Monitoring database stores events information while the persistence database store information about instance data and metadata.

It is often are requirement to clean up the database, especially if you deploying to a clean environment. The following MSBuild file will help do that.


    <UsingTask TaskFactory="PowershellTaskFactory" TaskName="ResetAppFabricDatabases" AssemblyFile="$(PowerShellAssemblyFile)">
        <ParameterGroup>
            <AppFabAdmins Required="true" ParameterType="System.String"/>
            <AppFabReaders Required="true" ParameterType="System.String"/>
            <AppFabUsers Required="true" ParameterType="System.String"/>
            <MonitoringDbName Required="true" ParameterType="System.String" />
            <MonitoringDbServer Required="true" ParameterType="System.String" />
            <PersistenceDbName Required="true" ParameterType="System.String" />
            <PersistenceDbServer Required="true" ParameterType="System.String" />
            <ConfirmPreference Required="true" ParameterType="System.String" />
        </ParameterGroup>
        <Task>
            <![CDATA[         
                            $executionPolicy = Get-ExecutionPolicy
                            Set-ExecutionPolicy Unrestricted                        
                Import-Module ApplicationServer
                $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"Normal", "Removing persistance database {0} on sql instance {1}.", $PersistenceDbName, $PersistenceDbServer)
                Remove-ASPersistenceSqlDatabase -Force -Server $PersistenceDbServer -Database $PersistenceDbName            
            
                $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"Normal", "Creating persistance database.")
                Initialize-ASPersistenceSqlDatabase -Admins $AppFabAdmins -Readers $AppFabReaders -Users $AppFabUsers -Database $PersistenceDbName -Server $PersistenceDbServer
            
                $log.LogMessage([Microsoft.Build.Framework.MessageImportance]"Normal", "Clearing Monitoring Database {0} on sql instance {1}.", $MonitoringDbName, $MonitoringDbServer)
                Clear-ASMonitoringSqlDatabase -Database $MonitoringDbName -Server $MonitoringDbServer                        
                        
                            Set-ExecutionPolicy $executionPolicy                        
                ]]>
        </Task>
    </UsingTask>


Just call the Target  "ResetAppFabricDatabases" from your project file as shown below.

Please note that you would need to have MSBuildExtensions pack installed on your machine to run this script.

    <Target Name="ClearDownAppFabric">
        <ResetAppFabricDatabases AppFabAdmins="$(Domain)\$(AppFabricAdministratorsGroup)"
                                 AppFabReaders="$(Domain)\$(AppFabricObserversGroup)"
                                 AppFabUsers="$(Domain)\$(AppFabricUsersGroup)"
                                 MonitoringDbName="$(APFMonitoringDatabaseName)"
                                 MonitoringDbServer="$(APFDatabaseServer)"
                                 PersistenceDbName="$(APFPersistenceDatabaseName)"
                                 PersistenceDbServer="$(APFDatabaseServer)"
                                 ConfirmPreference="None"/>
    </Target>

You can download the sample from here

Thursday, 11 April 2013

Work Items loosing formatting when edited in Microsoft Excel


Recently, we upgraded to Team Foundation Server 2012 for our project. The upgrade was generally a smooth affair and apart from a few niggles went out trouble free. Here is one of the niggles we faced, so I thought to blog about it here.

A sizeable number of people in our team use Microsoft Excel to edit work item especially where there is a lot of text to write such as Test Cases. It only takes a couple of clicks to open the work item in Microsoft Excel and user can use the rich editing functionality provided by Office.


However, after we moved to TFS 2012, editing work item description in Excel would show the following warning and the description would lose all its formatting.


As, it turns out the change in behaviour is caused because the upgrade process had changed the type of our Description field from PlainText to HTML. 


However, Team Foundation Server doesn't allow editing the actual HTML of the fields. This means that the text typed in Excel would be published without any of its tags and that is the reason that it lost its formatting. Here is the reason why Microsoft wouldn't allow HTML editing.

For us, it was important to keep the ability of editing work item in Excel, so the solution was to change the type of System.Description field to PlainText. This can be done by using the Witadmin utility. Following is the command that I executed. 

witadmin changefield /collection:<ProjectCollectionUrl> /n:System.Description /type:PlainText
Please note that the change will happen at Project Collection level and you will need to have appropriate permissions on all team projects within the collection. 



Some Updates


As I resumed blogging this time around, I swore to myself that I would post more frequently this time. It wasn't to be. My last post was done about month and half ago. So, now that I am done with my work for the day and I have got some good bedtime reading with me, I thought its' about time to write a quick post.

Life has not been dull in the last month or no… far from it. In the midst of some very busy office work and a short holiday trip, there has been some good learning and quite a few things to share. I will post about them soon. But first, I have got two exciting news to share

The first one is that I am a father for the first time. Our little boy was born early last month. He is slowly coming to terms with this world and is continuously keeping us enthralled.

The second great news is that I have been inducted into the Visual Studio ALM rangers program. Special thanks to Mike Fourie for recommending me. Here is the post from Willy-Peter Schaub introducing me to the program. I am thrilled to bits to be part of the program. The rangers program has released some great software in the past and I am looking forward to contributing positively.