1
votes

Hi I get an error when I try and profile my CRM Plugin. I have an external library that I reference in my Pluign assemble, this library used Entitt Framewiork 5 to quesry data from an external SQL table and write that data back to CRM. I mergered my Pluin and DAL assesmbblies using ILMerger, which woirked fine, although now I have the following error.

[PluginProfiler.Plugins: PluginProfiler.Plugins.ProfilerPlugin] [01646b5d-8250-e511-815f-bab85315b426: WebCall.Plugin.WebCallTrigger: Create of phonecall (Profiler)]

An exception occurred during the initialization of the Plug-in Profiler. Unhandled Exception: System.TypeLoadException: Inheritance security rules violated while overriding member: 'System.Data.Entity.Migrations.Design.ToolingException.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.

Here is my Plugin Code

 public class WebCallTrigger : IPlugin
{
    //private CallHistoryData db = new CallHistoryData();

    /// <summary>
    /// Plugin to initiate a web call from CRM using MaruSip API
    /// </summary>
    /// <param name="serviceProvider"></param>
    public void Execute(IServiceProvider serviceProvider)
    {

        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (context == null)
        {
            throw new ArgumentNullException("localContext");
        }

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

        IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

        if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity)

        //if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            //initaialize Entity
            Entity phoneCallEntity = (Entity)context.InputParameters["Target"];

            if (phoneCallEntity.LogicalName != "phonecall")
                return;


            //ensure that the Plugin fires on a create operaton
            if (context.MessageName.ToUpper() == "Create")
            {

                try
                {
                    //Guid userId = context.InitiatingUserId;
                    //PhoneCall phoneCallEB = new PhoneCall();

                    string connection = ConfigurationManager.ConnectionStrings["ZoiperHistoryEntities"].ConnectionString;

                    SqlConnection con = new SqlConnection(connection);
                    SqlCommand command = new SqlCommand("dbo.sp_get_call_history", con);

                    command.CommandType = CommandType.StoredProcedure;
                    con.Open();

                    int myreader;
                    using (SqlDataReader reader = command.ExecuteReader())
                        while (reader.Read())
                        {
                            string DialedNo;// = null;
                            string callID;// = null;
                            DateTime StartCallTime;
                            DateTime EndCallTime;
                            string Duration;
                            int NormalizedCallLogiD;
                            string UserAcount;
                            DialedNo = (string)reader["DialedNo"];
                            callID = (string)reader["CallUniqueID"];
                            StartCallTime = (DateTime)reader["CallStartTime"];
                            EndCallTime = (DateTime)reader["CallEndTime"];
                            NormalizedCallLogiD = (int)reader["NormalizedDataID"];
                            UserAcount = (string)reader["UserAccount"];

                            Duration = reader["Duration"].ToString();

                            var filerParams = new
                            {
                                duration = Duration,
                                startdate = StartCallTime,
                                enddate = EndCallTime,
                                useraccount = UserAcount,
                                dialednumber = DialedNo,
                                callerid = callID,
                                normalizedcallid = NormalizedCallLogiD

                            };


                            PhoneCall phoneCall = new PhoneCall();
                            using (var ctx = new OrganizationServiceContext(service))
                            {
                                var result = ctx.CreateQuery(PhoneCall.EntityLogicalName)
                                   .Where(x => (x.GetAttributeValue<string>(phoneCall.new_DialedNumber).Equals(filerParams.dialednumber)) &&
                                       (x.GetAttributeValue<string>(phoneCall.new_CallDuration).Equals(filerParams.duration.ToLower())) &&
                                       (x.GetAttributeValue<string>(phoneCall.new_callstartdate).Equals(filerParams.startdate)) &&
                                       (x.GetAttributeValue<string>(phoneCall.new_CallEndDate).Equals(filerParams.enddate)) &&
                                       (x.GetAttributeValue<string>(phoneCall.new_UserAccount).Equals(filerParams.useraccount.ToLower()))).Select(x => x).ToList();
                                result = result.Where(x => x.GetAttributeValue<EntityReference>(phoneCall.OwnerId.Name).Name.Equals(filerParams.useraccount)).Select(x => x).ToList();

                            }
                            service.Update(phoneCall);
                        }


                }

                    //exception handing for the plugin
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    Console.WriteLine("The application terminated with an error.");
                    Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
                    Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
                    Console.WriteLine("Message: {0}", ex.Detail.Message);
                    Console.WriteLine("Inner Fault: {0}",
                        null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");

                }

                 //Plugin timeout exception handling
                catch (System.TimeoutException ex)
                {
                    Console.WriteLine("The application terminated with an error.");
                    Console.WriteLine("Message: {0}", ex.Message);
                    Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
                    Console.WriteLine("Inner Fault: {0}",
                        null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("The application terminated with an error.");
                    Console.WriteLine(ex.Message);

                    // Display the details of the inner exception.
                    if (ex.InnerException != null)
                    {
                        Console.WriteLine(ex.InnerException.Message);

                        FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                            as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                        if (fe != null)
                        {
                            Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                            Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
                            Console.WriteLine("Message: {0}", fe.Detail.Message);
                            Console.WriteLine("Trace: {0}", fe.Detail.TraceText);
                            Console.WriteLine("Inner Fault: {0}",
                                null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
                        }
                    }
                }

            }
        }
    }
}

Any idea why this error occurred, I am using CRM On-Premises with plugin registered under Sandbox in Database, version 2015 and My Entity Framework version is 5.0.0. I also gave both my assemblies the same default namespace, set Copy to local = false on all CRm API Assemblies, I have tried various things and I'm out of ideas, please help.

1

1 Answers

0
votes

Apparantly a custom method overriding Entity Framework method System.Data.Entity.Migrations.Design.ToolingException.GetObjectData cannot be executed in a partial trusted app domain.

But, even if it could, you still cannot open database connections in sandboxed plugins.

Since you are developing for an OnPremise environment best choice is to run your code under full trust (i.e. not in the sandbox).