web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Deserializing message sent by CRM to Azure Service Bus

(0) ShareShare
ReportReport
Posted on by 2,541

Hello everyone,

First of all, i don't know if this post should belong to this forum or the Service Bus one, so forgive me if it's the wrong one ...

My issue is the following :

* I configure the CRM via the Plugin registration tool to send ".NetBinary" message to our Service Bus on Azure (it's configured like this since 6 months).

* I have an application which receive these messages and use the following code to "decrypt" it into a IPluginExecutionContext object :

var stream = message.GetBody<Stream>();
DataContractSerializer serializer = new DataContractSerializer(typeof(RemoteExecutionContext));
XmlDictionaryReader reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max);
var deserializedBody = serializer.ReadObject(reader);
IPluginExecutionContext plugin = (IPluginExecutionContext)deserializedBody;


My problem is :
* Sometimes a regular opportunity update is handled properly (most of the times)
* Sometimes i get an exception raised : "There was an error deserializing the object of type Microsoft.Xrm.Sdk.RemoteExecutionContext. The input source is not correctly formatted."

I don't really know what is happening, is it an issue of the CRM side when it sends the message to SB ? Is it an error of my console app.

Thanks a lot in advance,

Clément

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Bas van de Sande Profile Picture
    4,383 on at

    Hi Clement,

    my first guess is that the error might be caused by the data being sent. Have you been able to set up a reproduction scenario? Do you know what data is being sent?

    You need to sort that out, before you can come with a solution

    Bas

  • Verified answer
    Clem Profile Picture
    2,541 on at

    Hello Bas,

    Actually, i have no idea why some message are (let's say) corrupt.

    For testing purpose i just update everytime a free text field so there is no direct reason that some are passing fine and some others not.

    In the meantime i updated the configuration in my PRT to send direct JSON and deserialize it within one line of code.

    So far it seems to be fine ...

    Clément

  • Community Member Profile Picture
    on at

    Hi Clément,

    What did you use to deserialize JSON within one line of code?

    Also, are you deserializing it as a RemoteExecutionContext by any chance?

    Thank you,

    Sean

  • Clem Profile Picture
    2,541 on at

    Hello Sean,

    On the plugin registration tool, i specified that i wanted to receive a JSON object.
    And here are the lines to deserialize it :

    RemoteExecutionContext deserializedBody = message.GetBody<RemoteExecutionContext>(
                                              new DataContractJsonSerializer(typeof(RemoteExecutionContext)));
    
    IPluginExecutionContext plugin = (IPluginExecutionContext)deserializedBody;

    Then you have a classic plugin object you can play with.

    Rgards,

    Clément

  • Leigh Milbourne Profile Picture
    on at

    Hi Clement.

    Can you post the full set of code for this? I'm trying to parse the context in a FunctionApp and am having trouble.

    Thanks.

  • Verified answer
    Clem Profile Picture
    2,541 on at

    Here we go : 

    QueueClient client = QueueClient.CreateFromConnectionString(connectionString, sourcequeue);
    
    // Configure the callback options.
    OnMessageOptions options = new OnMessageOptions();
    options.AutoComplete = false;
    options.AutoRenewTimeout = TimeSpan.FromMinutes(1);
    			
    client.OnMessage((message) =>
    {
    	try
    	{
    		RemoteExecutionContext deserializedBody = message.GetBody<RemoteExecutionContext>(
    															new DataContractJsonSerializer(typeof(RemoteExecutionContext)));
    
    		IPluginExecutionContext plugin = (IPluginExecutionContext)deserializedBody;
    		
    		// do your stuff
    	}
    	catch(Exception)
    	{
    	
    	}
    });


    Regards,

    Clément

  • Mahesh Wankhade Profile Picture
    90 on at

    Hi Experts,

    I am also getting similar error. we are sending message to azure service bus queue using azure aware plugins and then processing it using azure function.

    Howvere on deserialisig the recieving below error

    "error deserializing the object of type Microsoft.Xrm.Sdk.RemoteExecutionContext. Encountered invalid character ' '."

    below is my json. can you point out what could be wrong with this json, i have tried validating jason with various  and json is validating green with no error.

    {

       "BusinessUnitId": "18b282e2-166e-e711-8122-70106fa12a51",

       "CorrelationId": "c03aa288-99cd-4bfa-9532-18f07ec77004",

       "Depth": 1,

       "InitiatingUserAzureActiveDirectoryObjectId": "00000000-0000-0000-0000-000000000000",

       "InitiatingUserId": "2699416d-c5d1-e811-a9cb-000d3ad1c507",

       "InputParameters": [

           {

               "key": "Target",

               "value": {

                   "__type": "Entity:http://schemas.microsoft.com/xrm/2011/Contracts",

                   "Attributes": [

                       {

                           "key": "address1_line1",

                           "value": "87A"

                       },

                       {

                           "key": "address1_city",

                           "value": "Glenholm st"

                       },

                       {

                           "key": "address2_city",

                           "value": "Glenholm st"

                       },

                       {

                           "key": "jjr_abn_not_applicable",

                           "value": true

                       },

                       {

                           "key": "address2_stateorprovince",

                           "value": "qld"

                       },

                       {

                           "key": "address2_postalcode",

                           "value": "4053"

                       },

                       {

                           "key": "jjr_departmentid",

                           "value": {

                               "__type": "EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts",

                               "Id": "131224da-2ae2-e811-a9d4-000d3ad1c8a7",

                               "KeyAttributes": [],

                               "LogicalName": "jjr_department",

                               "Name": null,

                               "RowVersion": null

                           }

                       },

                       {

                           "key": "address1_postalcode",

                           "value": "4053"

                       },

                       {

                           "key": "address2_line1",

                           "value": "87A"

                       },

                       {

                           "key": "address1_stateorprovince",

                           "value": "qld"

                       },

                       {

                           "key": "modifiedonbehalfby",

                           "value": null

                       },

                       {

                           "key": "jjr_businessclassification",

                           "value": {

                               "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                               "Id": "219a1b08-09cd-e811-a9d4-000d3ad1ce4e",

                               "KeyAttributes": [],

                               "LogicalName": "jjr_businessclassification",

                               "Name": null,

                               "RowVersion": null

                           }

                       },

                       {

                           "key": "accountid",

                           "value": "1ed0399f-bb69-eb11-a812-000d3acb6edf"

                       },

                       {

                           "key": "jjr_organisationalunitid",

                           "value": {

                               "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                               "Id": "6d3ac5b3-5321-ea11-a812-000d3ad0a866",

                               "KeyAttributes": [],

                               "LogicalName": "msdyn_organizationalunit",

                               "Name": null,

                               "RowVersion": null

                           }

                       },

                       {

                           "key": "address2_composite",

                           "value": "87A Glenholm st qld 4053 Australia"

                       },

                       {

                           "key": "jjr_company_name",

                           "value": "Taco"

                       },

                       {

                           "key": "modifiedon",

                           "value": "/Date(1612754075000)/"

                       },

                       {

                           "key": "jjr_customerpartytype",

                           "value": {

                               "__type": "OptionSetValue:schemas.microsoft.com/.../Contracts",

                               "Value": 965680001

                           }

                       },

                       {

                           "key": "modifiedby",

                           "value": {

                               "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                               "Id": "2699416d-c5d1-e811-a9cb-000d3ad1c507",

                               "KeyAttributes": [],

                               "LogicalName": "systemuser",

                               "Name": null,

                               "RowVersion": null

                           }

                       },

                       {

                           "key": "address1_composite",

                           "value": "87A Glenholm st qld 4053 Australia"

                       },

                       {

                           "key": "address2_country",

                           "value": "Australia"

                       },

                       {

                           "key": "address1_country",

                           "value": "Australia"

                       },

                       {

                           "key": "jjr_termsofpayment",

                           "value": {

                               "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                               "Id": "2f27cc65-b761-eb11-a812-000d3a6a449d",

                               "KeyAttributes": [],

                               "LogicalName": "msdyn_paymentterm",

                               "Name": null,

                               "RowVersion": null

                           }

                       },

                       {

                           "key": "jjr_integrationstate",

                           "value": {

                               "__type": "OptionSetValue:schemas.microsoft.com/.../Contracts",

                               "Value": 965680001

                           }

                       },

                       {

                           "key": "address1_latitude",

                           "value": -27.407623

                       },

                       {

                           "key": "address1_longitude",

                           "value": 152.9728049

                       }

                   ],

                   "EntityState": null,

                   "FormattedValues": [],

                   "Id": "1ed0399f-bb69-eb11-a812-000d3acb6edf",

                   "KeyAttributes": [],

                   "LogicalName": "account",

                   "RelatedEntities": [],

                   "RowVersion": "62982747"

               }

           }

       ],

       "IsExecutingOffline": false,

       "IsInTransaction": false,

       "IsOfflinePlayback": false,

       "IsolationMode": 1,

       "MessageName": "Update",

       "Mode": 1,

       "OperationCreatedOn": "/Date(1612754076000+0000)/",

       "OperationId": "dbe7f1c4-bb69-eb11-a812-000d3acb6edf",

       "OrganizationId": "8cbb6fcd-fc9a-4654-b215-200c0dc56bbc",

       "OrganizationName": "jjdev",

       "OutputParameters": [],

       "OwningExtension": {

           "Id": "861fd4e0-9906-ea11-a811-000d3ad1c24a",

           "KeyAttributes": [],

           "LogicalName": "sdkmessageprocessingstep",

           "Name": null,

           "RowVersion": null

       },

       "ParentContext": {

           "BusinessUnitId": "18b282e2-166e-e711-8122-70106fa12a51",

           "CorrelationId": "c03aa288-99cd-4bfa-9532-18f07ec77004",

           "Depth": 1,

           "InitiatingUserAzureActiveDirectoryObjectId": "00000000-0000-0000-0000-000000000000",

           "InitiatingUserId": "2699416d-c5d1-e811-a9cb-000d3ad1c507",

           "InputParameters": [

               {

                   "key": "Target",

                   "value": {

                       "__type": "Entity:http://schemas.microsoft.com/xrm/2011/Contracts",

                       "Attributes": [

                           {

                               "key": "address1_stateorprovince",

                               "value": "qld"

                           },

                           {

                               "key": "address1_country",

                               "value": "Australia"

                           },

                           {

                               "key": "address1_postalcode",

                               "value": "4053"

                           },

                           {

                               "key": "address1_city",

                               "value": "Glenholm st"

                           },

                           {

                               "key": "address1_line1",

                               "value": "87A"

                           },

                           {

                               "key": "address2_stateorprovince",

                               "value": "qld"

                           },

                           {

                               "key": "address2_country",

                               "value": "Australia"

                           },

                           {

                               "key": "address2_postalcode",

                               "value": "4053"

                           },

                           {

                               "key": "address2_city",

                               "value": "Glenholm st"

                           },

                           {

                               "key": "jjr_company_name",

                               "value": "Taco"

                           },

                           {

                               "key": "address2_line1",

                               "value": "87A"

                           },

                           {

                               "key": "jjr_customerpartytype",

                               "value": {

                                   "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts",

                                   "Value": 965680001

                               }

                           },

                           {

                               "key": "jjr_abn_not_applicable",

                               "value": true

                           },

                           {

                               "key": "jjr_businessclassification",

                               "value": {

                                   "__type": "EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts",

                                   "Id": "219a1b08-09cd-e811-a9d4-000d3ad1ce4e",

                                   "KeyAttributes": [],

                                   "LogicalName": "jjr_businessclassification",

                                   "Name": null,

                                   "RowVersion": null

                               }

                           },

                           {

                               "key": "jjr_departmentid",

                               "value": {

                                   "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                                   "Id": "131224da-2ae2-e811-a9d4-000d3ad1c8a7",

                                   "KeyAttributes": [],

                                   "LogicalName": "jjr_department",

                                   "Name": null,

                                   "RowVersion": null

                               }

                           },

                           {

                               "key": "jjr_organisationalunitid",

                               "value": {

                                   "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                                   "Id": "6d3ac5b3-5321-ea11-a812-000d3ad0a866",

                                   "KeyAttributes": [],

                                   "LogicalName": "msdyn_organizationalunit",

                                   "Name": null,

                                   "RowVersion": null

                               }

                           },

                           {

                               "key": "jjr_termsofpayment",

                               "value": {

                                   "__type": "EntityReference:schemas.microsoft.com/.../Contracts",

                                   "Id": "2f27cc65-b761-eb11-a812-000d3a6a449d",

                                   "KeyAttributes": [],

                                   "LogicalName": "msdyn_paymentterm",

                                   "Name": null,

                                   "RowVersion": null

                               }

                           },

                           {

                               "key": "accountid",

                               "value": "1ed0399f-bb69-eb11-a812-000d3acb6edf"

                           },

                           {

                               "key": "address1_composite",

                               "value": "87A Glenholm st qld 4053 Australia"

                           },

                           {

                               "key": "address2_composite",

                               "value": "87A Glenholm st qld 4053 Australia"

                           }

                       ],

                       "EntityState": null,

                       "FormattedValues": [],

                       "Id": "1ed0399f-bb69-eb11-a812-000d3acb6edf",

                       "KeyAttributes": [],

                       "LogicalName": "account",

                       "RelatedEntities": [],

                       "RowVersion": null

                   }

               },

               {

                   "key": "SuppressDuplicateDetection",

                   "value": false

               }

           ],

           "IsExecutingOffline": false,

           "IsInTransaction": false,

           "IsOfflinePlayback": false,

           "IsolationMode": 1,

           "MessageName": "Update",

           "Mode": 1,

           "OperationCreatedOn": "/Date(1612754076000+0000)/",

           "OperationId": "dbe7f1c4-bb69-eb11-a812-000d3acb6edf",

           "OrganizationId": "8cbb6fcd-fc9a-4654-b215-200c0dc56bbc",

           "OrganizationName": "jjdev",

           "OutputParameters": [],

           "OwningExtension": {

               "Id": "861fd4e0-9906-ea11-a811-000d3ad1c24a",

               "KeyAttributes": [],

               "LogicalName": "sdkmessageprocessingstep",

               "Name": null,

               "RowVersion": null

           },

           "ParentContext": null,

           "PostEntityImages": [],

           "PreEntityImages": [],

           "PrimaryEntityId": "1ed0399f-bb69-eb11-a812-000d3acb6edf",

           "PrimaryEntityName": "account",

           "RequestId": "b1003cfa-a7c5-43d0-aa99-b6aa32d808ea",

           "SecondaryEntityName": "none",

           "SharedVariables": [

               {

                   "key": "IsAutoTransact",

                   "value": true

               },

               {

                   "key": "ChangedEntityTypes",

                   "value": [

                       {

                           "__type": "KeyValuePairOfstringstring:#System.Collections.Generic",

                           "key": "account",

                           "value": "Update"

                       }

                   ]

               }

           ],

           "Stage": 30,

           "UserAzureActiveDirectoryObjectId": "00000000-0000-0000-0000-000000000000",

           "UserId": "2699416d-c5d1-e811-a9cb-000d3ad1c507"

       },

       "PostEntityImages": [

           {

               "key": "PostImage",

               "value": {

                   "Attributes": [

                       {

                           "key": "jjr_integrationstate",

                           "value": {

                               "__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts",

                               "Value": 965680001

                           }

                       }

                   ],

                   "EntityState": null,

                   "FormattedValues": [

                       {

                           "key": "jjr_integrationstate",

                           "value": "Integrated"

                       }

                   ],

                   "Id": "1ed0399f-bb69-eb11-a812-000d3acb6edf",

                   "KeyAttributes": [],

                   "LogicalName": "account",

                   "RelatedEntities": [],

                   "RowVersion": null

               }

           },

           {

               "key": "AsynchronousStepPrimaryName",

               "value": {

                   "Attributes": [

                       {

                           "key": "name",

                           "value": "tr1 - 05054365"

                       },

                       {

                           "key": "accountid",

                           "value": "1ed0399f-bb69-eb11-a812-000d3acb6edf"

                       }

                   ],

                   "EntityState": null,

                   "FormattedValues": [],

                   "Id": "1ed0399f-bb69-eb11-a812-000d3acb6edf",

                   "KeyAttributes": [],

                   "LogicalName": "account",

                   "RelatedEntities": [],

                   "RowVersion": null

               }

           }

       ],

       "PreEntityImages": [],

       "PrimaryEntityId": "1ed0399f-bb69-eb11-a812-000d3acb6edf",

       "PrimaryEntityName": "account",

       "RequestId": "b1003cfa-a7c5-43d0-aa99-b6aa32d808ea",

       "SecondaryEntityName": "none",

       "SharedVariables": [

           {

               "key": "IsAutoTransact",

               "value": true

           }

       ],

       "Stage": 40,

       "UserAzureActiveDirectoryObjectId": "00000000-0000-0000-0000-000000000000",

       "UserId": "2699416d-c5d1-e811-a9cb-000d3ad1c507"

    }

    After formatting the json , i am using below code to deserialize it.

     //create an instance of generic type object

               RemoteContextType obj = Activator.CreateInstance<RemoteContextType>();

               MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString));

               System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());

               obj = (RemoteContextType)serializer.ReadObject(ms);

               ms.Close();

               return obj;

  • thenerdynerd Profile Picture
    on at

    What about dates being in json format? How can we convert into a valid DateTime?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans