TFS Connection String Parser

by: Per Bjurström

When you are builing applications that integrates with Team Foundation Server you need to store information on how to connect to the server in you application configuration. The quick and dirty approach would be to store multiple key/value pairs in appSettings but that has some drawbacks, the most obvious is that this section is commonly used to store clear text values and storing the password to your most precious system in clear text sounds like a bad idea.

So, instead we want to store the connection information to TFS just like a connection string to a database, like this:

<connectionStrings>
    <add name="MyTfsServer" connectionString="TfsUrl=SERVER; TfsUsername=USER; TfsDomain=DOMAIN; TfsPassword=PASSWORD"/>
  </connectionStrings>

And to create a connection to the TFS Server you just call a factory class:

TeamFoundationServer tfs = TfsFactory.CreateServer("MyTfsServer");

And here is the code to back it up, feel free to use it in any way you want. And if you need help on encryption this section check out this guide.

TfsFactory.cs

/// Factory class to create connections to Team Foundation Server
/// </summary>
public static class TfsFactory
{
    private static string _url;
    private static string _user;
    private static string _domain;
    private static string _password;
    private static object _lockObject = new object();
 
    /// <summary>
    /// Create a new server instance
    /// </summary>
    /// <param name="serverConfig">The connection string name</param>
    /// <returns></returns>
    public static TeamFoundationServer CreateServer(string serverConfig)
    {
        if (_url == null)
        {
            lock (_lockObject)
            {
                if (_url == null)
                {
                    ConnectionStringSettings setting = WebConfigurationManager.ConnectionStrings[serverConfig];
                    if (setting == null)
                        throw new Exception("Cannot find a connection string with name " + serverConfig);
 
                    TfsConnectionStringBuilder conn = new TfsConnectionStringBuilder(setting.ConnectionString);
                    _url = conn.TfsUrl;
                    _user = conn.TfsUsername;
                    _password = conn.TfsPassword;
                    _domain = conn.TfsDomain;
                }
            }
        }
 
        return new TeamFoundationServer(_url, new NetworkCredential(_user, _password, _domain));
    }
}

TfsConnectionStringBuilder.cs

/// <summary>
/// Parses connection strings used for Team Foundation Server
/// </summary>
/// <remarks>
/// Format: TfsUrl=X;TfsUsername=X;TfsDomain=X;TfsPassword=X
/// </remarks>
public class TfsConnectionStringBuilder : DbConnectionStringBuilder
{
    public static string []supportedKeywords = new string[] { "tfsdomain", "tfspassword", "tfsurl", "tfsusername"  };
 
    public TfsConnectionStringBuilder() : base(false)
    {
    }
    public TfsConnectionStringBuilder(string connectionString): base(false)
    {
        base.ConnectionString = connectionString;
    }
    public override object this[string keyword]
    {
        get{return base[keyword];}
        set
        {
            if (Array.BinarySearch<String>(supportedKeywords, keyword) < 0)
                throw new Exception(String.Format("Property {0} is not supported by TfsConnectionStringBuilder",keyword));
            base[keyword] = value;
        }
    }
 
    public string TfsUrl{get { return this["tfsurl"] as string; }}
    public string TfsUsername{get { return this["tfsusername"] as string; }}
    public string TfsDomain{get { return this["tfsdomain"] as string; }}
    public string TfsPassword{get { return this["tfspassword"] as string; }
    }
}
 

28 November 2007


Comments

  1. Thanks, this helped me to make an FtpConnectionStringBuilder
Post a comment    
User verification Image for user verification  
EPiTrace logger