TFS API – WorkItemStore is null and throws NullReferenceException

In the last weeks I wrote a migration tool to migrate test cases from Rally to Team Foundation Server. This tool is deployed to a server and runs as a Windows Service.

After I made a small update to this tool suddenly my tool stopped working.

My log file gave me the following error:

Error in Querying Work Item System.NullReferenceException: Object reference not set to an instance of an object

 

This amazed me, because it has worked before and the code that I used to create the WorkItemStore has not changed.

   1: TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(TfsServerURI), nc);

   2:  

   3: _wis = tfs.GetService(typeof(WorkItemStore)) as WorkItemStore;

I searched a long time and it drove me nuts. The code did not change but it did not work. Then I found a hint in a forum. There is another way of creating the WorkItemStore.

   1: TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(TfsServerURI), nc);

   2:  

   3: _wis = new WorkItemStore(tfs);

When I deployed this, it still did not work, but at least the error message was helpful.

System.TypeInitializationException: The type initializer for ‘Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore’ threw an exception. —> System.IO.FileLoadException: Mixed mode assembly is built against version ‘v2.0.50727’ of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

 

This helped me a lot. I changed code and referenced a new assembly. An assembly for the Test Manager API. This is a .Net 4.0 assembly.

The other assemblies are still based on the .Net 2.0 framework. Mixed mode is something that is not supported out of the box. You need to change some configuration in the windows service to support this (same goes for Web)

Add this to your app.config or web.config

   1: <?xml version="1.0"?>

   2: <configuration>

   3:     <startup useLegacyV2RuntimeActivationPolicy="true">

   4:         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>

   5:     </startup>

   6: </configuration>

The attribute useLegacyV2RuntimeActivationPolicy="true" is the keyword!

When I deployed my service it worked !!

Advertisements

14 Responses to “TFS API – WorkItemStore is null and throws NullReferenceException”

  1. Great, It’s really help me out a lot! Thanks!

  2. Great! Thank You.

  3. not working in my case, i get an object reference not set error when running _wis = new WorkItemStore(tfs); otehrwise, tfs.GetService returns null,

    really this is driving me nuts , what to do??

    • Strange. Did you try on another work station or other credentials just to make sure? Otherwise I don’t know either.

      Good luck !

  4. Thank you!!!!x1000

    Unfortunately my original code didn’t work with the TFS2008 (I developed it for TFS2010 but it turned out we need it also for TFS2008) assemblies.

    I also found this way (from the code of TFS Integration Tool) but i got stuck at this point.

    I got some further error messages after that but these were just some missing assemblies.

    I regret there is no donate possibility, you saved me a tons of time / money! (I have to deploy the tool strictly tomorrow)

  5. That really helped me to solve my issue when dealing with TFS 2012. Thanks for writing it down.

  6. Hi, Anybody please answer.. I am trying to do the exact thing but, is TFS explorer installation must in the machine I have installed the windows service?

  7. Saved me a ton of trouble – thanks!

  8. Thank you. I know it’s an old post, but lack of any meaningful exception really had me stumped! You got me on track again.

  9. This old post is still saving people time. Once I saw the Exception that was being thrown, I could move beyond the roadblock. Thanks again.

%d bloggers like this: