Saturday, June 23, 2018

OData v4 client code generator System.InvalidCastException issue and solution

Unable to cast object of type 'Microsoft.OData.Edm.Library.AmbiguousTypeBinding' to type 'Microsoft.OData.Edm.IEdmCollectionType'." 
                                                                      or 

Unable to cast object of type 'Microsoft.OData.Edm.Csdl.CsdlSemantics.UnresolvedType' to type 'Microsoft.OData.Edm.IEdmCollectionType'

Resolution: 

Once ODataClient class is executed to generate proxy classes from metadata file, AX tries to generate them for all existing entities at the same time, but there are duplicated types in metadata like ItemType enum and ItemType entity; and it fails with AmbiguousTypeBinding exception as you can notice.

To fix the issue, open the metadata.xml file in notepad (make sure you don't change the file type but open the xml file using notepad). Rename ItemType as ItemTypes at three different places as highlighted below:
1. <EntityType Name="ItemTypes">
2. <Property Name="ItemType" Type="Microsoft.Dynamics.DataEntities.ItemTypes">
3. <EntitySet Name="ItemTypes" EntityType="Microsoft.Dynamics.DataEntities.ItemTypes" />

Save the xml file and save the ODataClient.tt file. The custom tool will run without any error.

2 comments: