4

I'm running a MVC project and utilizing Json, I have code that is running properly when running within Visual Studio, then I have a Site on IIS pointing to the same folder, when executed a URL from the IIS site my code doesn't perform the same as being in Visual Studio.

Within my code, I have:

return JsonConvert.SerializeObject(objectToSerialize);

When I send RouteData.Values, it produces this error:

Error getting value from 'CompiledAssembly' on 'System.CodeDom.Compiler.CompilerResults'.

at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value)
at x.Data.Helpers.Data.JsonHelper.SerializeObject(Object objectToSerialize) in D:\Development\x\x.Data\Helpers\Data\JsonHelper.cs:line 18

I am currently using Newtonsoft.Json, Version=11.0.0.0

Any ideas what is happening? Does IIS not have something that Visual Studio does?

Derek
  • 653
  • 7
  • 20
  • Have you checked whether the compiled assemblies are present in the bin folder of the application directory? – Priyan Perera Feb 26 '18 at 02:07
  • I'm seeing `Newtonsoft.Json.dll` and `Newtonsoft.Json.xml` under to proper projects. Looks good to me. – Derek Feb 26 '18 at 02:15
  • Can you see the application related compiled assemblies in the bin folder? i.e. .dll – Priyan Perera Feb 26 '18 at 03:23
  • Yes, I do see them (including `Newtonsoft.Json.dll`). I even deployed to Google Cloud so see if there was an issue with running the same folder as the Solution. Still an issue. – Derek Feb 26 '18 at 19:01

1 Answers1

6

I recently ran into this same exception with a different cause. Your RouteData.Values object is a RouteValueDictionary that can have objects as values, some of those objects may have Properties that throw an exception when the their 'get' is called.

You can handle these issues by passing in a JsonSerializerSettings object as the second parameter and override the Error EventHandler.

If you just want to ignore such properties, setting the ErrorEventArgs.ErrorContext.Handled to true should do the trick.

return JsonConvert.SerializeObject(objectToSerialize, new JsonSerializerSettings() { Error = new EventHandler<Newtonsoft.Json.Serialization.ErrorEventArgs>((obj, args) => {
                args.ErrorContext.Handled = true;
            }) });
Dan Rapsinski
  • 61
  • 1
  • 3