开发者

Winforms ConnectionString and TeamCity

We are starting a new WinForms project and decided to use TeamCity to create builds and run unit and integration tests. The project deals with database. We have 3 databases (developDB (this is used by developers while developing =) ), testDB (this is used by teamcity to run tests) and productionDB(this is used by client)). TeamCity has 3 buildConfiguration. The first is triggered when commit happens. The second is triggered every night to run integration tests. And the third is triggered by developer when we what to make a release. So I want TeamCity to be able to change connectionString depending on what kind of build happens. Also I don't want to store connectionString in a开发者_运维知识库pp.config (I don't want client to know the user and password). What options are available to perform the task?

Thanks in advance!

Updated

I use NHibernate and FluentNHibernate to connect to databases if it matters.


In this situation, I would use TeamCity to run a nant script to perform the build.

NAnt allows you to modify config file values (such as your connection string) at build time.

An example of using TeamCity/NAnt to deploy to different staging environments can be found at this blog post: http://thecodedecanter.wordpress.com/2010/03/25/one-click-website-deployment-using-teamcity-nant-git-and-powershell/

As @surfen suggests, the connection string values for each environment should be encrypted to prevent credentials from being stored in plain text.


I have not used TeamCity, but I have written multiple applications with dynamically changing ConnectionStrings during logon process (ie. at runtime), and It's quite simple.

You didn't tell how do you connect to your Database. Since you mention app.config, I suppose it is ADO.NET DataSets or simmilar technology, which creates a read-only(getter) ConnectionString in your Settings.Designer.cs / app.config.

What I did, was to create a setter method in Settings.cs (not Settings.Designer.cs) for the ConnectionString property like this:

public void setNorthwindConnectionString(String value) {
    this["NorthwindConnectionString"] = value;
}

My generated DataSet then uses this NorthwindConnectionString for accessing data.

You can use preprocessor directives for conditional setup of your ConnectionString:

#if DEBUG
    Console.WriteLine("Mode=Debug"); 
    Settings.Default.setNorthwindConnectionString("(DebugDBConnectionString)");
#else
    Console.WriteLine("Mode=Release"); 
    Settings.Default.setNorthwindConnectionString("(ReleaseDBConnectionString)");
#endif

You could also encrypt your connection strings, and copy the right app.config during post build event.


I am assuming you would be using msbuild to build your projects in Team city. If that is the case, then you can send the Conditional Compilation Symbol where in you can pass what ever symbols you need.

Once you have the symols, you can do things like:

#if DEVBUILD
//.... Your Connection String Code here
#endif

#if INTBUILD
.... Your Connection String Code here
#endif

That's the answer to your frst question.

Looking at the second part of your question, where in you do not want to store the user name & password in the app.config,

Options:

  • try intergrated security, it will use your domain account
  • if option cannot be used, try keeping your connection string as a Registry Key, so that its not obvious or an Environment variable.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜