Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the compilation error from Team City after adding Newtonsoft.Json?

Tags:

json

c#

teamcity

I have a service that was sending bad DateTime data due to use of the JavascriptSerializer. I used Nuget to add Newtonsoft to the project, and utilized that. Here's the only place it's utilized in the code (old way, then new):

374 
-            var messageString = new JavaScriptSerializer().Serialize(messageDetails);

374 
+            var messageString = Newtonsoft.Json.JsonConvert.SerializeObject(messageDetails);

Below is an error that occurs during the Team City build:

Consumer\MetricTrackingMQServiceConsumer.cs(374, 49): error CS0122: 'Newtonsoft.Json.JsonConvert' is inaccessible due to its protection level 
Consumer\MetricTrackingMQServiceConsumer.cs(374, 61): error CS0117: 'Newtonsoft.Json.JsonConvert' does not contain a definition for 'SerializeObject'

The project compiles fine locally. Why is it failing in Team City?

like image 650
333Matt Avatar asked Oct 23 '25 15:10

333Matt


1 Answers

As you're using NuGet to include the dependency here's the workflow I strongly suggest to avoid these type of issues.

  1. Ensure that your reference to the Newtonsoft DLL is pointing at the NuGet packages folder.
  2. Exclude the NuGet packages from source control.
  3. Add a "Nuget Installer" type build step before your solution build step to restore all the NuGet packages referenced by the solution.

This has a number of advantages but most importantly given your current issue, it ensures that the version of the DLL referenced by your solution is available and in the correct location.

like image 86
Paul Hunt Avatar answered Oct 25 '25 06:10

Paul Hunt