Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting error while serializing and deserializing in custom workflow

Posted on by 140

Hi All,

I want to serialize a .NET object and deserialize a JSON object in a custom workflow in Dynamics CRM.
To serialise, I use the following code.

string serializedString = "";
using (var memorystream = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(object));
serializer.WriteObject(memorystream, objectToSerialize);
serializedString = Encoding.UTF8.GetString(memorystream.ToArray());
}
return serializedString;

The code below is used for deserializing.

T deserializedResult;
using (var memorystream = new MemoryStream(Encoding.UTF8.GetBytes(objectToDeserialise)))
{
var deserializer = new DataContractJsonSerializer(typeof(T));
deserializedResult = (T)deserializer.ReadObject(memorystream);
}
return deserializedResult;

When I run the custom workflow, it is showing the following error.

Unexpected exception from plug-in (Execute): CustomWorkflow: System.Runtime.Serialization.SerializationException: Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.

Please guide me to solve this issue.

Any help is appreciated.

*This post is locked for comments

  • Manju Augustine Profile Picture
    Manju Augustine 140 on at
    RE: Getting error while serializing and deserializing in custom workflow

    Any suggestions to solve this issue?

  • Manju Augustine Profile Picture
    Manju Augustine 140 on at
    RE: Getting error while serializing and deserializing in custom workflow

    Hi Alex,

    I tried the code above but still, I am getting the below exception.

    Unexpected exception from plug-in (Execute): customWorkflow: System.Runtime.Serialization.SerializationException: Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.

     

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Getting error while serializing and deserializing in custom workflow

    Did you try the most popular answer there, though?

    "Unfortunately, none of the proposed solutions solve the real source of the problem. This exception means that your deserializer tries to read from the end of a stream.

    The solution is to rewind the stream to the beginning, ie. set the stream.Position = 0; before deserialization."

    I'm guessing it would look like this:

    T deserializedResult;

    using (var memorystream = new MemoryStream(Encoding.UTF8.GetBytes(objectToDeserialise)))

    {

    memorystream.Position = 0;

    var deserializer = new DataContractJsonSerializer(typeof(T));

    deserializedResult = (T)deserializer.ReadObject(memorystream);

    }

    return deserializedResult;

  • Manju Augustine Profile Picture
    Manju Augustine 140 on at
    RE: Getting error while serializing and deserializing in custom workflow

    Hi Alex

    I have tried using JavaScriptSerializer but it shows the following error.


    Unexpected exception from plug-in (Execute): customWorkflow: System.MethodAccessException: Attempt by security transparent method 'SerializationHelper.Serialize(System.Collections.Generic.IEnumerable`1<!!0>)' to access security critical method 'System.Web.Script.Serialization.JavaScriptSerializer..ctor()' failed. Assembly 'customWorkflows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a5cdced9d17070f3' is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself. In order to access security critical code, this assembly must be fully trusted. Assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.Web.Extensions, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain.

    Thanks.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Getting error while serializing and deserializing in custom workflow

    That's not a CRM question, really.. but this might help:

    stackoverflow.com/.../error-converting-json-to-net-object-in-asp-net

    "The solution is to rewind the stream to the beginning, ie. set the stream.Position = 0; before deserialization."

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans