Hi Folks,
System.Runtime.Serialization.SerializationException: Microsoft Dynamics CRM has experienced an error. <ErrorCode>-2147220970</ErrorCode> with according to Microsoft translates to "An unexpected error occurred".
I am using the latest online version of CRM Dynamics 2016. Trying to create a Task following the creation of an Account by using early bound assembly which has been ILMerged and part of the one Plugin.dll. This all works fine without error in all of the following scenarios:
- Running the Plugin On Premise (isolation mode: None)
- Running the same code but substitute early binding to late binding in sandbox
- Running almost identical code via an external console application
- Removing the reference to Assembly (Common.dll) and instead include the CS files directly within the Plugin
The above projects are part of the same VS CRM solution and are referencing the same identical early bound library. In my case this is called CRM Common.DLL therefore at the moment I have no reason to believe there is something wrong with the plugin assembly itself; unless there are some other steps I should have taken specifically for an assembly which runs in Sandbox. When I debug the plugin, I find that is comes back with an error message "account id <guid xxxxxxxxx> Does Not Exist". This I do not understand as I have an Account GUID returned in code but neither the Account or subsequent task gets created.
I've tried various other permutations when setting the EntityReference for RegardingObjectId none of them work in sandbox, all of them work on premise ( e.g. RegardingObjectId=incomingAccount.ToEntityReference() or using the output parameter shown in code below).
The plugin is registered:
- Isolation: Sandbox
- Pipeline: Post Operation
- Synchronous
- Create message
- Primary Entity: Account
I have also tested this changing assembly isolation mode to "asynchronous" in that Scenario the Account gets created but the Task does not get created and the plugin trace shows it later errors with the same obscure Account Id does not exist.
My code
public void Execute(IServiceProvider serviceProvider)
{
ITracingService _tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
IPluginExecutionContext _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory _factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = (IOrganizationService)_factory.CreateOrganizationService(_context.UserId);
Entity incoming = (Entity)_context.InputParameters["Target"];
Guid _accountId = new Guid(_context.OutputParameters["id"].ToString());
_tracer.Trace("Incoming entity identified as " + incoming.LogicalName);
Task followup = new Task();
followup.Subject = "Testing EARLY binding task creation via DLL in simple plugin";
followup.RegardingObjectId = new EntityReference("account", _accountId);
_tracer.Trace("Attempting to create Task and providing the account GUID " + _accountId.ToString());
_service.Create(followup);
}
catch (Exception ex)
{
_tracer.Trace("Error in Create Task : " + ex.Message);
}
}
Though I have workarounds
It seems to me there are clear gaps in my knowledge when it comes to referencing early bound types from within a plugin assembly and so I would like to understand how to do this where you have a merged DLL running in Sandbox. Often, I have many projects in a CRM solution, so having them all referencing the one common library is advantageous.
Thanks for help