Fixing JSON serialization issues when using a VSTS / TFS Service Hook

Today I was working on an integration project when I discovered some unwanted behavior when utilizing a Service Hook when trying to capture a changed work item. I used the following strategy (see this excellent blog post by René van Osnabrugge) to create JSON classes for a generated request. While debugging my solution I figured out that not all properties had values. What could be the issue?

Setting up the scenario

Using the previously mentioned strategy I have created some C# classes for the JSON from the Service Hook. I use these classes in my method to capture the Service Hook. Specifying the “TFSHook.RootObject” automatically transforms the JSON into the classes.

Method Definition

Finding the issue…

I expected all the classes to be automatically filled with the values from the received JSON stream. Unfortunately, some of the classes where empty.

Empty Properties

First thing I went to validate was whether the JSON actually contained the values. Now once the request has been transformed it is not very easy to get the original JSON. Luckily the Service Hooks page has an excellent history view, in there you can find all you need.

Service Hook Request Details

With the extra set of eyes from Mark Dekker we quickly discovered that the some of the properties HAVE a “.” in their names, while inspecting the generated JSON classes did NOT HAVE a “.” in their names.

The solution

Call it a bug or a feature, the solution is all that matters for now. In the generated classes we can easily decorate the properties with an JSON attribute that will allow us to specify the property name to look for in the JSON stream.

Code Snippets

Voila! Running this will provide you with objects containing the actual values.

Bonus Material: Increasing the debugging experience

When debugging your Service Hook, changing some code and then debugging again may give an unexpected behavior, your previous breakpoint may not be hit. My experience is that when something goes wrong in your code, or a timeout happens, the Service Hook is set to “Enabled (restricted)”.

Enabled Restricted Service Hook

This can be easily fixed by choosing “Enable” from the context menu.

Enable a Restricted Service Hook

Be sure to have the Service Hooks page around when developing a Service Hook. This post proves how useful it can be!

Advertisements