TeamCity Service Messages Library for .NET

Do you know what service messages are? Service Messages are strings in the following format:


##teamcity[hello message='teamcity']
TeamCity uses Service Messages to provide an easy API for a build scripts integration, i.e. build status reporting, artifacts publishing, tests reporting and more. For precise description of the format and service messages usage in TeamCity, take a loot at Build Script Integration with TeamCity article. Service Messages introduces a communication protocol for map-like data exchange between processes using stream, socket, HTTP or pipe.

In the blog post I describe a .NET library for manipulating with Service Messages. The library contains two main classes. One for reading and one for writing service messages.

Reading service messages

Reading service messages could be done from TextReader or string with minimal memory requirements:


using JetBrains.TeamCity.ServiceMessages.Read;
...
using(TextReader reader = ... ) {
foreach(var message in new ServiceMessageParser().ParseServiceMessages(reader)) {
/// process IServiceMessage object
}
}
...

Parse method gives you an IEnumerable or IServiceMessage:


public interface IServiceMessage
{
[NotNull]
string Name { get; }
[CanBeNull]
string DefaultValue { get; }
IEnumerable Keys { get; }
[CanBeNull]
string GetValue([NotNull] string key);
}

There you see Name, DefaultValue and key-values access for attributes as Keys and GetValue.

Writing service messages

To create a service message you may use anonymous type for service message attributes:


using JetBrains.TeamCity.ServiceMessages.Write;
....
string serializedMessage
= new ServiceMessageFormatter().FormatMessage(
"rulez",
new {
Version = "3.2.2",
Mode = "zoom"
}));
/// returns: ##teamcity[rulez Version='3.2.2' Mode='zoom']
...

I believe this is most handy method for creating service messages. Still, you may use strongly typed approach:


using JetBrains.TeamCity.ServiceMessages.Write;
....
string serializedMessage
= new ServiceMessageFormatter().FormatMessage(
"rulez",
new ServiceMessageProperty("Apple", "239"),
new ServiceMessageProperty("Pie", "42")
));
...

Usage

The library is compiled for .NET 3.5. It is available as NuGet package:

PM> Install-Package TeamCity.ServiceMessages

Sources are available at GitHub. I setup a build configuration at TeamCity.CodeBetter.Com.

Thanks to TeamCity support for NuGet, it automatically publishes the library as NuGet package to NuGet.org.

comments powered by Disqus