ForkPork
ForkPork

Reputation: 47

Cosmos Db call throws SerializationException

I am writing my own cosmos db accessor which is a wrapper to diff cosmos db operations like create, update, read documents.

e.g.,

try 
{
    ItemResponse<T> response = await client.GetContainer(this.DatabaseId, this.ContainerId)
                .UpsertItemAsync(document, new PartitionKey(document.PartitionKey)).ConfigureAwait(false);
}
catch (Exception ex)
{
   logError(ex, ...);
   throw ex;
}

what I notice is sometimes I get SerializationException in the catch block, stack trace:

   System.Runtime.Serialization.SerializationException: Member 'HelpURL' was not found.
   at System.Runtime.Serialization.SerializationInfo.GetElement(String name, Type& foundType)
   at System.Runtime.Serialization.SerializationInfo.GetString(String name)
   at System.Exception..ctor(SerializationInfo info, StreamingContext context)
   at lambda_method(Closure , Object[] )
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateISerializable(JsonReader reader, JsonISerializableContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateISerializableItem(JToken token, Type type, JsonISerializableContract contract, JsonProperty member)
   at Newtonsoft.Json.Serialization.JsonFormatterConverter.Convert(Object value, Type type)
   at System.Runtime.Serialization.SerializationInfo.GetValue(String name, Type type)
   at System.Exception..ctor(SerializationInfo info, StreamingContext context)
   at lambda_method(Closure , Object[] )
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateISerializable(JsonReader reader, JsonISerializableContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)

so I am a bit confused as why I got such error from cosmos db call and where should I investigate, as mentioned I have many wrapper methods and so far I notice this error usually comes from read operation, either ReadItemAsync or update operation which requires read then modify.

Thanks for any inputs

Upvotes: 0

Views: 562

Answers (2)

Matias Quaranta
Matias Quaranta

Reputation: 15603

Serialization happens when the raw JSON from the service on a response is converted using Newtonsoft.Json to the <T> model or type you are calling the method with.

If I have a class name MyClass with certain properties and serialization decorators, if I do:

ItemResponse<MyClass> = await client.ReadItemAsync<MyClass>(....);

The SDK will take the raw service response and try to deserialize it to an instance of MyClass. Newtonsoft errors mean that there is some issue during this process, potentially the raw JSON stored in the service might have an issue with the type you are trying to map it to.

Sharing what your <T> is might hint at where the issue can be, potentially in some property called HelpURL? Is that part of your <T>?

Also, which SDK are you using? Is that exception the full stack trace? If so, why does it not include the SDK call on it? Are you sure it is coming from there and not some upper layer that is also catching the Exception that you throw and is attempting to, for example, serialize it to be returned in a Web API response?

Upvotes: 1

Sourabh
Sourabh

Reputation: 59

You can check Read/Update/Insert operation separately if these calls are working fine. It looks like some call is failing in backend and giving "HelpURL" in response, to which it is trying to deserialize and failing with this error.

Upvotes: 1

Related Questions