CRM Dynamcis PLugin错误

时间:2015-09-01 10:24:43

标签: c# entity-framework-5 dynamics-crm-2015

我在尝试配置我的CRM插件时遇到错误。我有一个外部库,我在我的Pluign汇编中引用,这个库使用Entitt Framewiork 5来询问来自外部SQL表的数据并将该数据写回CRM。我使用ILMerger合并了我的Pluin和DAL assesmbblies,虽然现在我有以下错误。

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

Plug-in Profiler初始化期间发生异常。 未处理的异常:System.TypeLoadException:覆盖成员时违反了继承安全规则:'System.Data.Entity.Migrations.Design.ToolingException.GetObjectData(System.Runtime.Serialization.SerializationInfo,System.Runtime.Serialization.StreamingContext)'。覆盖方法的安全性可访问性必须与要覆盖的方法的安全性可访问性相匹配。

这是我的插件代码

 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");
                        }
                    }
                }

            }
        }
    }
}

知道为什么会出现这个错误,我使用CRM On-Premises与在数据库中的Sandbox下注册的插件,2015版和My Entity Framework版本是5.0.0。我还为我的程序集提供了相同的默认命名空间,在所有CRm API程序集上将Copy设置为local = false,我尝试了各种各样的东西,但我没有想法,请帮忙。

1 个答案:

答案 0 :(得分:0)

显然,无法在部分受信任的应用程序域中执行覆盖实体框架方法System.Data.Entity.Migrations.Design.ToolingException.GetObjectData的自定义方法。

但是,即使可以,您仍然无法在沙盒插件中打开数据库连接。

由于您正在为OnPremise环境进行开发,因此最好选择在完全信任下运行代码(即不在沙箱中)。