none
[CRM 2011 - Plugin - Sandbox - Database] Cannot execute Associate method - Inheritance security rules violated error

    Question

  • Hi all,

    I have a problem while trying to execute associate method in plugin context (Post Create and Post Update). The dll is deployed in database with isolation mode = sandbox.

    I have Account and Contact entities with primarycontactid and contact_customer_accounts relationships. They're all OOTB features. Now, I write a plugin which check if the primarycontactid field is not full, it will be add to Account's contact_customer_accounts relationship. Very simple isn't it? But I got this error when execute Associate method:

    System.TypeLoadException: 
    Inheritance security rules violated while overriding member: 
    'Microsoft.Crm.Services.Utility.DeviceRegistrationFailedException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. 
    Security accessibility of the overriding method must match the security accessibility of the method being overriden.

    This is my code:

    using System;
    using Microsoft.Xrm.Sdk;

    ...

     Entity targetEntity = context.InputParameters["Target"] as Entity;
                if (targetEntity != null && targetEntity.LogicalName == "account")
                {
                    IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                    if (targetEntity["primarycontactid"] != null)
                    {
                        EntityReference contactRef = targetEntity["primarycontactid"] as EntityReference;
                        if (contactRef != null)
                        {
                            Relationship relationship = new Relationship("contact_customer_accounts");
                            EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
                            relatedEntities.Add(contactRef);
                            service.Associate(targetEntity.LogicalName, targetEntity.Id, relationship, relatedEntities);
                        }
                    }
                }

    NOTE: This code works perfectly fine with dll is deployed in database with isolation mode = None.

    Thanks,

    Please help me with this issue.

    Tuesday, June 18, 2013 11:44 AM

All replies