简体   繁体   中英

accessing and updating tfs 2010 build parameters programmatically from .net 3.5

I am probably missing something basic here as this just doesn't seem to be working the way I expect. I am not really ac# guy by day.

I have a tfs 2010 plugin that is watching ticket change events and filtering for the WIT changes to tickets that I want. This is all based on http://geekswithblogs.net/jakob/archive/2010/10/27/devleoping-and-debugging-server-side-event-handlers-in-tfs-2010.aspx

I pull out all the necessary variables in the plugin and I need to pass this to the build engine, which will be actually pushing the build. The thing that is giving me the most grief here is that the parameters are pushed as an xaml string or a "dictionary and serialized it into a string". Now, there is a library Microsoft.TeamFoundation.Build.Workflow that does some handling of this but it seems to be for .net 4 and the tfs server is running in .net 2 and cannot bind it. That method is discussed in the widely linked http://blogs.msdn.com/b/jpricket/archive/2010/03/25/tfs2010-queuing-a-build-from-code-with-custom-process-parameter-values.aspx but he just uses a method DeserialzeProcessParameters that I don't have access to.

I am just trying to update a few values and I can do this in a couple lines of powershell so I thought I would be able to address it myself but I am running into trouble.

The default Buildrequest.parameters for the build request looks like the below (w/ the /r/n converted to new lines. It can be parsed as the innerXml of an XmlDocument).

If I have an xml doc like below, how, in c#, can I address and update the values for, say, RestoreDatabase?

<Dictionary x:TypeArguments="x:String, x:Object" xmlns="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:mtbw="clr-namespace:Microsoft.TeamFoundation.Build.Workflow;assembly=Microsoft.TeamFoundation.Build.Workflow" xmlns:mtbwa="clr-namespace:Microsoft.TeamFoundation.Build.Workflow.Activities;assembly=Microsoft.TeamFoundation.Build.Workflow" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
            <mtbwa:BuildSettings x:Key="BuildSettings" ProjectsToBuild="$/EJTest/TFSServerEventHandler/TFSServerEventHandler.sln">
                            <mtbwa:BuildSettings.PlatformConfigurations>
                                            <mtbwa:PlatformConfigurationList Capacity="0" />
                            </mtbwa:BuildSettings.PlatformConfigurations>
            </mtbwa:BuildSettings>
            <mtbwa:TestSpecList x:Key="TestSpecs" Capacity="0" />
            <mtbwa:CodeAnalysisOption x:Key="RunCodeAnalysis">Never</mtbwa:CodeAnalysisOption>
            <x:Boolean x:Key="AssociateChangesetsAndWorkItems">False</x:Boolean>
            <x:Boolean x:Key="CreateWorkItem">False</x:Boolean>
            <x:Boolean x:Key="DropBuild">False</x:Boolean>
            <x:Boolean x:Key="PerformTestImpactAnalysis">False</x:Boolean>
            <x:Boolean x:Key="CreateLabel">False</x:Boolean>
            <x:Boolean x:Key="DisableTests">True</x:Boolean>
            <mtbw:BuildVerbosity x:Key="Verbosity">Detailed</mtbw:BuildVerbosity>
            <x:String x:Key="BuildNumber">4.4.2.29</x:String>
            <x:String x:Key="BackupDatabase">yes</x:String>
            <x:String x:Key="RestoreDatabase">Yes</x:String>
            <x:String x:Key="OverwriteBackup">Yes</x:String>
            <x:String x:Key="UpgradeSoftware">No</x:String>
            <x:String x:Key="DeploymentTicket">654</x:String>
</Dictionary>

The x:string values are the ones I want to update and change.

For what it's worth, the PS version

[xml]$a = Get-Content .\test.xml
$b = $a.Dictionary.string | where {$_.key -eq "CustomerData"}
$b."#text" = 'No'

Thanks for your time.

ok, i banged my way through it. not sure it is the most efficient way but it seems to work:

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load("test.xml");
        XmlNodeList elemList = xDoc.GetElementsByTagName("x:String");

        foreach (XmlNode xNode in elemList)
        {
            switch (xNode.Attributes[0].Value)
            {
                case "BuildNumber":
                    Console.WriteLine(xNode.Attributes[0].Value + " = " + xNode.InnerText);
                    xNode.InnerText = "4.3w2432.2";
                    break;

                case "BackupDatabase":
                    Console.WriteLine(xNode.Attributes[0].Value + " = " + xNode.InnerText);
                    xNode.InnerText = "4.3w2432.2";
                    break;

                case "RestoreDatabase":
                    Console.WriteLine(xNode.Attributes[0].Value + " = " + xNode.InnerText);
                    xNode.InnerText = "4.3w2432.2";
                    break;

                case "OverwriteBackup":
                    Console.WriteLine(xNode.Attributes[0].Value + " = " + xNode.InnerText);
                    xNode.InnerText = "4.3w2432.2";
                    break;

                case "UpgradeSoftware":
                    Console.WriteLine(xNode.Attributes[0].Value + " = " + xNode.InnerText);
                    xNode.InnerText = "4.3w2432.2";
                    break;

                case "DeploymentTicket":
                    Console.WriteLine(xNode.Attributes[0].Value + " = " + xNode.InnerText);
                    xNode.InnerText = "4.3w2432.2";
                    break;


            }

Since the Build environment dll is a .net 4.0 dll and your current dll you either need to use a adapter (http://code.msdn.microsoft.com/windowsdesktop/Using-a-NET-4-Based-DLL-bb141db3, this way you can start the build normaly using the object model) or kick of a build using tfsbuild.exe from code. This is because your values aren't serialized otherwise.

Since you listed the same functionality in powershell, I thought you where already in powershell. If that was the case you could have just start the tfsbuild.exe command line to kick of a build. this is however not the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM