How to queue a new build using TFS API

Another post on TFS API, my last post was related to how to get build Definitions and Build Details from TFS 2010, and this post is about how to queue a new build in TFS 2010 Server.

As earlier you need to add reference of following assemblies

  1. Microsoft.TeamFoundation.dll
  2. Microsoft.TeamFoundation.Client.dll
  3. Microsoft.TeamFoundation.Build.Client.dll

which will be available under – C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\ location.

And here is the code snippet

var tfsName = "http://dotnetthoughts:8080/tfs/defaultcollection";
var tfs = new TeamFoundationServer(tfsName, new UICredentialsProvider());
tfs.EnsureAuthenticated();
if (tfs.HasAuthenticated)
{
	var buildServer = tfs.GetService(typeof(IBuildServer)) as IBuildServer;
	var buildDefinition = buildServer.GetBuildDefinition("", "");
	buildServer.QueueBuild(buildDefinition);
}
catch (Exception exception)
{
    MessageBox.Show(exception.Message);
    throw;
}

Here is how it works, we are connecting and getting the TFS Server, then getting the Build Server using GetService method. And to get the build definition instead of the earlier post(to get all the Build definitions, we are passing “*”), we need to specify the Team project and Build name(Build definitions are unique per team project in TFS). Then we can call QueueBuild() method on Build server object, with build definition as the parameter, which will queue the build definition default values.

Happy coding :)

Posted in .Net, .Net 4.0, Version Control | Tagged , , , , , | 1 Comment

How to use .Net assembly in VBScript

Few days before I got a requirement to use a .Net assembly in VBScript, but it was not working. But today I got the solution. It was related to my 64bit OS. :) In this post I am discussing how to create and use a .net assembly in VBScript or Javascript.

First we need to create a class library. Set the ComVisible attribute to true in the AssemblyInfo.cs manually.

[assembly: ComVisible(true)]

Or you can do it using Properties > Application Tab and select Assembly Information, from the Assembly Information dialog, Check the Make assembly COM-Visible check box.

Make Assembly COM Visible option from Assembly Information dialog

Make Assembly COM Visible option from Assembly Information dialog

Build the assembly. Now we need register the assembly. We can do it using RegAsm.exe. If your OS in 64 bit, then you need to use Regasm command from Framework64 folder(Normally it will be C:\Windows\Microsoft.NET\Framework64\ folder). For running RegAsm command it is recommended to sign the assembly. You can Sign the assembly Properties > Signing Tab.

Sign Assembly option

Sign Assembly option

As RegAsm command add / update information to Windows Registry, you need to run the RegAsm command as Administrator, otherwise it will not work.

You can register the assembly using following command, like this

regasm /codebase assemblyname.dll

You can unregister the assembly using RegAsm command, like this

regasm /u assemblyname.dll

That’s it. Now you can create the object of the .net assembly in VBScript. Like this, it will add 10 and 20 using C# Add method and returns the value.

Dim calcLib
Set calcLib = CreateObject("CalcLib.Math")
Dim result
result = calcLib.Add (10 , 20)
msgbox result

And here is the C# Class, which we are invoked from VBScript.

namespace CalcLib
{
    public class Math
    {
        public int Add(int number1, int number2)
        {
            return number1 + number2;
        }
    }
}

Happy Coding :)

Posted in .Net, .Net 3.0 / 3.5, .Net 4.0, Visual Studio, Windows 7 | Tagged , , , , , | 1 Comment

Cloak option missing from source control explorer context menu

Ever noticed some time cloak menu item missing from source control explorer context menu?

Cloak option is missing from source control explorer context menu
Cloak option is missing from source control explorer context menu

Cloaking used to prevent users from viewing specified workspace folders or for folders you do not currently need. Cloaking is useful when you are working with files from two or more branches under a common parent to prevent you from copying files unnecessarily. Finally, cloaking increases performance bandwidth and conserves local disk space by preventing folders and files not used currently from being copied to the local working folder. Today I noticed that I am missing the cloak option for a specific folder. I couldn’t find the reason, because it available for another Folder in same workspace. Later I found that it was because of the mapping of local and source folder are different. Like my TFS path was $Projects/System/Data/Binaries, it should be mapped to C:\Projects\System\Data\Binaries, but I was mapped to some other folder like C:\Projects\System\Data\DataBinaries. And then I changed it via File > Source Control > Workspaces option. After this I got the cloak option. :)

Cloak option of Source control explorer conext menu

Cloak option of Source control explorer conext menu

Posted in Miscellaneous, Version Control, Visual Studio | Tagged , , , | 1 Comment

How to compress JPEG image using C#

Here is a C# snippet which helps to compress JPEG image. Also displays how much size got reduced.

public static void CompressJpeg(string path, int quality)
{
    if (quality < 0 || quality > 100)
    {
        throw new
            ArgumentOutOfRangeException("Quality must be between 0 and 100.");
    }
    //Creating temp. file and
    string tempFile = Path.GetTempFileName();
    File.Copy(path, tempFile, true);
    using (var image = Image.FromFile(tempFile))
    {
        // Encoder parameter for image quality
        var qualityParam =
            new EncoderParameter(Encoder.Quality, quality);
        // Jpeg image codec
        var jpegCodec = ImageCodecInfo.GetImageEncoders()
            .Where(imageCodecInfo => imageCodecInfo.MimeType == "image/jpeg")
            .FirstOrDefault();
        var encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;
        //Save the compressed image.
        image.Save(path, jpegCodec, encoderParams);
        //Getting the file image sizes.
        var prevImageSize = new FileInfo(tempFile).Length;
        var nextImageSize = new FileInfo(path).Length;
        Console.WriteLine("Image compressed. Size saved :{0} bytes",
            prevImageSize - nextImageSize);
    }
    //Removing the temp. file.
    File.Delete(tempFile);
}
Posted in .Net, .Net 3.0 / 3.5, .Net 4.0 | Tagged , , , | 1 Comment

How to get Build Definitions and Build Details from TFS 2010

This snippet which will connect to TFS Server and get all running builds for all build definitions, using TFS Client API.

try
{
    var tfsName = "http://dotnetthoughts:8080/tfs/defaultcollection";
    var tfs = new TeamFoundationServer(tfsName, new UICredentialsProvider());
    tfs.EnsureAuthenticated();
    if (tfs.HasAuthenticated)
    {
        var bs = tfs.GetService(typeof(IBuildServer)) as IBuildServer;
        //Select all Build definitions
        var spec = bs.CreateBuildQueueSpec("*", "*");
        var buildResults = bs.QueryQueuedBuilds(spec);
        foreach (IQueuedBuild queuedBuild in buildResults.QueuedBuilds)
        {
            Console.WriteLine("Status :{0} - {1} - Requested by : {2} Controller :{3}",
                queuedBuild.Status, queuedBuild.BuildDefinition.Name,
                queuedBuild.RequestedBy, queuedBuild.BuildController.Name);
        }
    }
}
catch (Exception exception)
{
    MessageBox.Show(exception.Message);
    throw;
}

Please make sure you are adding of the following assemblies.

  1. Microsoft.TeamFoundation.dll
  2. Microsoft.TeamFoundation.Build.Client.dll
  3. Microsoft.TeamFoundation.Build.Common.dll
  4. Microsoft.TeamFoundation.dll

which will be available under – C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\ location.

Happy GREEN builds :)

Posted in .Net, .Net 4.0 | Tagged , , , , , | 2 Comments