【问题标题】:CRM Dynamcis PLugin ErrorCRM 动态插件错误
【发布时间】:2015-09-01 10:24:42
【问题描述】:

您好,我在尝试分析我的 CRM 插件时遇到错误。我有一个在我的 Pluign 程序集中引用的外部库,该库使用 Entitt Framewiork 5 从外部 SQL 表中查询数据并将该数据写回 CRM。我使用 ILMerger 合并了我的 Pluin 和 DAL 程序集,效果很好,但现在出现以下错误。

[PluginProfiler.Plugins:PluginProfiler.Plugins.ProfilerPlugin] [01646b5d-8250-e511-815f-bab85315b426:WebCall.Plugin.WebCallTrigger:创建电话呼叫(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 和我的实体框架版本是 5.0.0。我还为我的两个程序集提供了相同的默认命名空间,在所有 CRm API 程序集上将 Copy 设置为 local = false,我尝试了各种方法,但我没有想法,请帮忙。

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 我现在已经尝试过了,它现在给了我一个不同的异常,未处理的异常:System.TypeInitializationException:'Microsoft.CSharp.RuntimeBinder.Semantics.PredefinedTypeFacts' 的类型初始化程序抛出了一个异常,我是使用 CRM 2015,所以我只能使用注册工具进行部署,因为还没有发布开发人员工具包,所以当我尝试在 GAC 中注册时失败,出现以下异常 Unable to load plug-in assembly,有什么建议吗?
    • 您在 Linq 查询中使用匿名类型 filerParams。为什么?可能是这导致了问题。如果没有,请分享更多详细信息(堆栈跟踪,导致异常的行)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    相关资源
    最近更新 更多