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 :
Customer experience | Sales, Customer Insights,...
Answered

Plugin with early bound throw SerializationException

(0) ShareShare
ReportReport
Posted on by 92

Hi everyone,

I've got an early bound entity plugin registered on retrieve multiple message and throw an exception as follow:

System.Runtime.Serialization.SerializationException: Element 'schemas.microsoft.com/.../Contracts:Entity' contains data from a type that maps to the name 'XXX.AAA.Plugins.ProxyClasses:xxx_prescriberrole'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'xxx_prescriberrole' and namespace 'XXX.AAA.Plugins.ProxyClasses'.

I tried to build an updated early bound entity, but it doesn't help.

At the same time, I have another plugin running the same business logic but without early bound. I don't see any exception. Any clue?

I have the same question (0)
  • Verified answer
    - Matt Bayes - Profile Picture
    890 on at

    Hey Charlie,

    This issue has come up a few times with myself during development. I am unsure as the root cause but have a solution.
    I believe the issue may be because the output you are setting in your plugin (probably an EntityCollection) isn't build around the Early Bound objects, and although your object extends from the base Entity class, it still isn't classified as an "Entity" when being set.

    In order to resolve, all I've had to do is convert the object(s) I'm setting to the base Entity structure.

    var role = new xxx_prescriberrole();
    var entityRole = role.ToEntity();

    If you have an Entity Collection you are trying to set:

    var roles = new List();
    var entityRoles = new EntityCollection();
    
    roles.ForEach(r => entityRoles.Add(r.ToEntity()));

    You may also be able to just cast your Early Bound object instead of calling the ToEntity method:

    var role = new xxx_prescriberrole();
    var entityRole = (Entity)role;

    If you have more questions please feel free to reach out.

    If this solved your issue I appreciate you liking and marking this as answered.

    Thanks!

    Matt Bayes

  • Suggested answer
    Charles Abi Khirs Profile Picture
    3,569 on at

    Hello Charlie,

    Do you have other plugins that are working well with Early Bound ?

    Did you try to use the EnableProxyType in your plugins project? in order to enable it, follow the steps below:

    1. In the plugin project, expand Properties and double-click AssemblyInfo.cs

    2. Add [assembly: ProxyTypesAssembly] at the end of the file

    3. Add the needed reference using Microsoft.Xrm.Sdk.Client; under the using section

  • Charlie Chen Profile Picture
    92 on at

    Thanks Matt for your detailed info.

    Here is how I add the entity to outputparameter:

    EntityCollection results = RetrieveMultiple(castedQuery);
    
                        if (results?.Entities?.Count > 0)
                        {
                            
                            foreach (var entity in results.Entities)
                            {
                                outputParameter.Entities.Add((Entity)entity);
                                //the casting is added as per suggestion.
                            }
                        }

    The result of retrievemultiple is already entity collection. However, I will make the change to test it a bit.

  • Charlie Chen Profile Picture
    92 on at

    Hi Charles,

    I tried your steps, but VS complaint "Duplicate 'ProxyTypesAssembly' attribute".  Maybe there is another attribute "[assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()]

    " in Entities.cs.

    Thanks,

    Charlie

  • Verified answer
    - Matt Bayes - Profile Picture
    890 on at

    Hey Charlie,

    Did you test with the casting?

    If that casting doesn't work, you could tailor your code to be something like this:

    EntityCollection results = RetrieveMultiple(castedQuery);
    
    results?.Entities?.ToList?.ForEach(e => outputParameter.Entities.Add(e.ToEntity()));

    I find using the "ToEntity" extension method works well when casting to a type.

  • Charlie Chen Profile Picture
    92 on at

    Hi Matt,

    It's working! My goodness! Thank you very very very much!

    I still don't understand your previous post why the entity is not being "classified as an "Entity" when being set into outputparameter". Do you mind help me understand this? Anything I can read?

    Charlie

  • - Matt Bayes - Profile Picture
    890 on at

    Hey Charlie,

    Glad to hear it solved your problem!

    Unfortunately there's no documentation around why this error occurs. My best best, based on the error message presented, has to do with the EntityCollection type expecting the type of record to be an "Entity". Although the ProxyClasses inherit from the base Entity, they are not truly the "Entity" type when you build a collection using ProxyClasses. Casting to the type of "Entity" makes the EntityCollection happy, and the object serializer/deserializer happy because it knows how to handle the "Entity" type.

    I just attribute it to Microsoft voodoo and like to think it's a feature, not a bug :)

    Cheers!

    Matt Bayes

  • Charlie Chen Profile Picture
    92 on at

    Thanks Matt. During the process from query building by amend information the original query expression  and executing the retrievemultiple method, I haven't use the early bound in purpose at all. How it can get involved when the method deserializes the JSON before returning the query result ? Below is the entire code block.

    if (outputParameter.Entities.Count == 0)
                {
    
                    
                    var oringinalQuery = (InputParameters["Query"]);
    
                    QueryExpression castedQuery = new QueryExpression();
    
                    QueryByAttribute castedQueryByAttribute = new QueryByAttribute();
    
                    bool queryExpressionUsed = false;
    
                    if (oringinalQuery.GetType() == typeof(FetchExpression))
                    {
                        string fetchXml = ((FetchExpression)oringinalQuery).Query;
    
                        // Convert the FetchXML into a query expression.
                        var conversionRequest = new FetchXmlToQueryExpressionRequest
                        {
                            FetchXml = fetchXml
                        };
    
                        var conversionResponse =
                            (FetchXmlToQueryExpressionResponse)Execute(conversionRequest);
    
                        castedQuery = conversionResponse.Query;
                        queryExpressionUsed = true;
    
                    }
                    else if (oringinalQuery.GetType() == typeof(QueryExpression))
                    {
                        castedQuery = (QueryExpression)oringinalQuery;
                        queryExpressionUsed = true;
                    }
                    else if (oringinalQuery.GetType() == typeof(QueryByAttribute))
                    {
                        castedQueryByAttribute = (QueryByAttribute)oringinalQuery;
                    }
    
                    bool queryRequired = false;
                    if (queryExpressionUsed)
                    {
                        foreach (var condition in castedQuery.Criteria.Conditions)
                        {
                            if (condition.AttributeName == "xxx_name")
                            {
                                condition.AttributeName = "xxx_name_en";
                                queryRequired = true;
                            }
                        }
                    }
                    else
                    {
                        foreach (var att in castedQueryByAttribute.Attributes)
                        {
                            if (att == "xxx_name")
                            {
                                var index = castedQueryByAttribute.Attributes.IndexOf(att);
                                castedQueryByAttribute.Attributes.RemoveAt(index);
                                castedQueryByAttribute.Attributes.Insert(index, "xxx_name_en");
                                queryRequired = true;
                            }
                        }
    
                    }
    
                    if (queryRequired && queryExpressionUsed)
                    {
                        
                       // EntityCollection results = OrgService.RetrieveMultiple(castedQuery);
                        EntityCollection results = _localContext.OrganizationService.RetrieveMultiple(castedQuery);
    
                        results?.Entities?.ToList().ForEach(e => outputParameter.Entities.Add(e.ToEntity()));
    
                        //if (results?.Entities?.Count > 0)
                        //{
                        
                        //    foreach (var entity in results.Entities)
                        //    {
                        //        outputParameter.Entities.Add((Entity)entity);
                        //    }
                        //}
                    }
                    else if (queryRequired && !queryExpressionUsed)
                    {
                     
                       // EntityCollection results = OrgService.RetrieveMultiple(castedQueryByAttribute);
                       
                        EntityCollection results = _localContext.OrganizationService.RetrieveMultiple(castedQueryByAttribute);
                        
                        results?.Entities?.ToList().ForEach(e => outputParameter.Entities.Add(e.ToEntity()));
                        
                        //if (results?.Entities?.Count > 0)
                        //{
                        
                        //    foreach (var entity in results.Entities)
                        //    {
                        //        outputParameter.Entities.Add((Entity)entity);
                        //    }
                        //}
                    }
    
                }

    And when I build the plugin exactly same just without the early bound, everything is fine.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans