Using Early-Bound Entities in Custom Plugins

Jan 3, 2014 at 7:50 PM
First of all, this is a fantastic project!

I'm creating a couple of plugins and was wondering if you had any example of using Crm Early-Bound entities with the XrmToolbox. I find that I need to work with the base type Entity for any CRUD operation, otherwise I get error messages like

Error on Retrieve:
Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type '[EarlyBoundEntityTypeNameHere]'
Error on Save:
The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:request. The InnerException message was 'Error in line 1 position 11912. Element 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:value' contains data from a type that maps to the name '[EarlyBoundEntityTypeNameHere]'. 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 '[EntityTypeName]' and namespace '[EarlyBoundNamespace]'.'. Please see InnerException for more details.
I understand that this can be handled when using an OrganizationServiceProxy by enabling the proxy types, but am having difficulty as the XrmToolbox handles all the connections and sends me a OrganizationService, not an OrganizationServiceProxy.

Thanks!
Jan 3, 2014 at 8:31 PM
Edited Jan 3, 2014 at 8:31 PM
Ah, I see now that the Runtime type of the IOrganizationService in the UpdateConnection method is a OrganizationServiceProxy so I just made some slight tweaks to enable this and all is well.
        public void UpdateConnection(IOrganizationService newService, string actionName = "", object parameter = null)
        {
            service = newService;
            EnableProxyTypes();
        }

        private void EnableProxyTypes()
        {
            if (service is OrganizationServiceProxy)
            {
                ((OrganizationServiceProxy)service).EnableProxyTypes();
            }
        }
Marked as answer by HovaBlevin on 1/3/2014 at 1:38 PM