Unable to deserialize object: The deserializer threw an exception ,in Scaleout Object Browser

#1
I'm trying to store the data in ScaleOut and I am able to save the String value. And I am able to validate the stored data in the ScaleOut through ScaleOut Object Browser.
But when I am trying to store the class object in ScaleOut, code is successfully executing. But when I'm trying to validate the stored data in the scaleout through ScaleOut Object Browser, I'm getting the object deserialization error.

Error:
ScaleOut Object Browser error in object info.PNG

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.Serialization.SerializationException: There was an error deserializing the object . The data at the root level is invalid. Line 1, position 1. ---> System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.
at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlUTF8TextReader.Read()
at System.Xml.XmlBaseReader.IsStartElement()
at System.Runtime.Serialization.NetDataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
--- End of inner exception stack trace ---
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at DotNetBrowser.CustomDeserializationConfig.Deserialize(Stream stream)
at DotNetBrowser.UserControls.ObjectPane.DeserializeObject(MemoryStream stream)
 
Last edited:

markw

Administrator
Staff member
#2
Hi Anuja,

It looks like you've configured the Object Browser to use an XML deserializer, but the object wasn't serialized as XML--the XML parser didn't get past the first character. If you look at the raw object contents (in the Contents tab there in your screenshot), does it look like XML?

By default, the ScaleOut APIs perform binary serialization. So in your app, you'll want to look at your cache configuration to make sure that you've set it up to perform XML serialization. The exact call will depend on the client library that you're using:
If you didn't intend for the Object Browser to perform XML deserialization, you can configure it in the "Tools > Default Serialization" menu item, or you can specify how the Object Browser does deserialization on a cache-by-cache basis: just right-click on the cache in question and select "Deserialization..." from the context menu.
 
Last edited:
#3
Hi,
I am able to save and retrieve the json data and validate the stored data in the ScaleOut Object Browser.

But still getting below error message, detailed screenshots attached FYI:

Unable to deserialize object: The deserializer threw an exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.Serialization.SerializationException: There was an error deserializing the object . The data at the root level is invalid. Line 1, position 1. ---> System.Xml.XmlException: The data at the root level is invalid. Line 1, position 1.




1738842552931.png
1738842603888.png
 

Attachments

markw

Administrator
Staff member
#4
It looks like you're performing JSON serialization outside the caching library and then saving that string to the cache. The ScaleOut APIs serialize that string using .NET's BinaryFormatter, which is why you see that 23-byte header in the hex viewer.

There are 2 ways to fix this.

1. If you just want to see your JSON as a string, configure the Object Browser to use the default BinaryFormatter instead of XML. This can be selected in "Tools > Default Deserialization...". Also check the deserialization settings on your individual cache to make sure it isn't overriding the Browser's default.

1738882816911.png

2. Another (potentially better!) option would be to change your app to avoid the BinaryFormatter altogether and register your JSON serialization logic directly with the ScaleOut API. So instead of serializing to a string and then saving that string, let ScaleOut cache invoke your serialization logic by registering callbacks. For example, if you're using the NamedCache API:

C#:
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Soss.Client;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

internal class Program
{
    static void Main(string[] args)
    {
        var nc = CacheFactory.GetCache("MyCache");
        // Register  JSON serialization callbacks with the cache:
        nc.SetCustomSerialization(SerializeCachedObj, DeserializeCachedObj<Person>);

        var newPerson = new Person { Name = "MyFirstName", Age = 40 };

        // add to cache:
        nc.Insert("my key", newPerson, nc.DefaultCreatePolicy, true, false);
        // retrieve from cache:
        Person retrievedPerson = nc.Retrieve("my key", acquireLock: false) as Person;

        Console.WriteLine(retrievedPerson);
    }


    private static readonly UTF8Encoding UTF8NoBom = new UTF8Encoding(
        encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

    public static void SerializeCachedObj(Stream stream, object obj)
    {
        var serializer = new JsonSerializer();

        // The NamedCache library will close the supplied
        // stream for us later, so don't let the StreamWriter
        // close it here it during disposal.
        using (var sw = new StreamWriter(stream,
                                            UTF8NoBom,
                                            bufferSize: 1024,
                                            leaveOpen: true))
        using (var writer = new JsonTextWriter(sw))
        {
            serializer.Serialize(writer, obj);
            writer.Flush();
        };
    }

    public static object DeserializeCachedObj<T>(Stream stream)
    {
        var serializer = new JsonSerializer();

        // The Scaleout.Client library will close the supplied
        // stream for us later, so don't let the StreamReader
        // close it here uring disposal.
        using (var sr = new StreamReader(stream,
                                            Encoding.UTF8,
                                            detectEncodingFromByteOrderMarks: false,
                                            bufferSize: 1024,
                                            leaveOpen: true))
        using (var jsonTextReader = new JsonTextReader(sr))
        {
            return serializer.Deserialize<T>(jsonTextReader);
        }
    }
}
Some benefits of this more integrated approach:
  • Your serialization logic will write directly to the stream used by the ScaleOut library.
  • The client library's client cache may improve read performance by avoiding unnecessary deserialization overhead.

Once you change your app to avoid that default BinaryFormatter behavior, you can configure the Object Browser to do JSON serialization:

1738883119052.png
 
Last edited by a moderator:
#5
Thanks for sharing the code snippet.

We are trying to implement the shared code in my application, but we are unable to use Soss.Client (namespace).
Please help us to add the Soss.Client namespace in the application to proceed further.
 

markw

Administrator
Staff member
#6
Hi Satya,

Is there a particular problem or error that's preventing you from using the Soss.Client namespace? Or are you using the newer Scaleout.Client nuget package in your application?

If you send an email to [email protected] and reference this forum thread, we'd be happy to schedule a call with your development team to discuss this further.
 
Top