简体   繁体   English

Dynamics CRM 2011的自动命名生成器插件在进入执行之前崩溃

[英]Autoname generator plugin for Dynamics CRM 2011 crashes before it enters execute

I have a custom entity acm_lidmaatschapbedrijf which I have to automatically generate a name for when it's created, so the user doesn't need to do this himself. 我有一个自定义实体acm_lidmaatschapbedrijf,在创建该实体时必须自动为其生成一个名称,因此用户无需自己执行此操作。

I got the following class for this, which : 我为此获得了以下课程:

protected class LocalPluginContext
    {
        internal IOrganizationService OrganizationService
        {
            get;
            private set;
        }

        internal IPluginExecutionContext PluginExecutionContext
        {
            get;

            private set;
        }

        internal ITracingService TracingService
        {
            get;

            private set;
        }

        internal LocalPluginContext(IServiceProvider serviceProvider)
        {
            if (serviceProvider == null)
            {
                throw new ArgumentNullException("serviceProvider");
            }

            // Obtain the execution context service from the service provider.
            PluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Obtain the tracing service from the service provider.
            TracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            Trace("Tracingservice aangemaakt.");
            // Obtain the Organization Service factory service from the service provider
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            Trace("Factory aangemaakt");
            // Use the factory to generate the Organization Service.
            OrganizationService = factory.CreateOrganizationService(PluginExecutionContext.UserId);
            Trace("Service aangemaakt");
        }

        internal void Trace(string message)
        {
            if (string.IsNullOrWhiteSpace(message) || TracingService == null)
            {
                return;
            }
            if (PluginExecutionContext == null)
            {
                TracingService.Trace(message);
            }
            else
            {
                TracingService.Trace(
                    "{0}, Correlation Id: {1}, Initiating User: {2}",
                    message,
                    PluginExecutionContext.CorrelationId,
                    PluginExecutionContext.InitiatingUserId);
            }
        }
    }


    public void Execute(IServiceProvider serviceProvider)
    {
        if (serviceProvider == null)
        {
            throw new ArgumentNullException("serviceProvider");
        }

        // Construct the Local plug-in context.
        LocalPluginContext localcontext = new LocalPluginContext(serviceProvider);
        //Get the entity;
        localcontext.Trace("Localcontext aangemaakt");
        acm_lidmaatschapbedrijf lidmaatschapbedrijf = ((Entity)localcontext.PluginExecutionContext.InputParameters["Target"]).ToEntity<acm_lidmaatschapbedrijf>();
        //Check whether the linked lidmaatschap is the right one;
        localcontext.Trace("lidmaatschapbedrijf opghaald");
        acm_lidmaatschap lidmaatschapcategorie = localcontext.OrganizationService.Retrieve(acm_lidmaatschap.EntityLogicalName, lidmaatschapbedrijf.acm_typelidmaatschap.Id, new ColumnSet(new[] { "acm_typelidmaatschap" })).ToEntity<acm_lidmaatschap>();
        localcontext.Trace("Categorie opgehaald");
        localcontext.Trace(string.Format("Categorie = {0}", lidmaatschapcategorie.acm_typelidmaatschap.Value));
        if (lidmaatschapcategorie.acm_typelidmaatschap.Value != 805210000)
        {
            throw new InvalidPluginExecutionException("Het type lidmaatschap is niet bedrijf.");
        }
        //assign name
        lidmaatschapbedrijf["acm_name"] = string.Format("{0} {1}", "Bedrijfslidmaatschap", lidmaatschapbedrijf.acm_account.Name);
        localcontext.Trace(string.Format("Nieuwe naam = {0}", lidmaatschapbedrijf.acm_name));
        localcontext.PluginExecutionContext.InputParameters["Target"] = lidmaatschapbedrijf;
        localcontext.Trace("Target opnieuw toegekend");


    }
}

This is how the plugin is registered 这是插件注册的方式

在此处输入图片说明

It appears like the plugin is launched, but it crashes with the following error dump: 看起来好像插件已启动,但由于以下错误转储而崩溃:

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.Runtime.Serialization.SerializationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #6C7B81E3Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220970</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
    <KeyValuePairOfstringanyType>
      <d2p1:key>CallStack</d2p1:key>
      <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract&amp; dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
   at ReadKeyValuePairOfstringanyTypeFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )
   at System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract&amp; dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
   at ReadParameterCollectionFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract&amp; dataContract)
   at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
   at System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
   at Microsoft.Crm.Sandbox.SandboxUtility.DeserializeDataContract[T](Byte[] serializedDataContract, Assembly proxyTypesAssembly)
   at Microsoft.Crm.Sandbox.SandboxExecutionContext.get_InputParameters()
   at Microsoft.Crm.Sandbox.SandboxExecutionContext.Merge(IExecutionContext originalContext)
   at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)</d2p1:value>
    </KeyValuePairOfstringanyType>
  </ErrorDetails>
  <Message>System.Runtime.Serialization.SerializationException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #6C7B81E3</Message>
  <Timestamp>2014-05-09T11:55:16.4261105Z</Timestamp>
  <InnerFault i:nil="true" />
  <TraceText>

[acm.mscrm.stm.Plugins: acm.mscrm.stm.Plugins.BedrijfsLidmaatschapPreValidator]
[e0ec46f5-f5d5-e311-b055-005056a03018: acm.mscrm.stm.Plugins.BedrijfsLidmaatschapPreValidator: Create of acm_lidmaatschapbedrijf]


</TraceText>
</OrganizationServiceFault>

As you can see, it doesn't show any traces. 如您所见,它不显示任何痕迹。 it just crashes and I don't know why. 它只是崩溃,我不知道为什么。 Do you need to do any special settings to use InputParameters["Target"]? 您是否需要进行任何特殊设置才能使用InputParameters [“ Target”]?

The serialization error comes from the following line : 序列化错误来自以下行:

localcontext.PluginExecutionContext.InputParameters["Target"] = lidmaatschapbedrijf;

You can't assign an early bound entity to InputParameter["Target"] . 您不能将早期绑定实体分配给InputParameter [“ Target”]。 The assignment must be a late-bound Entity ie Entity() 分配必须是后期绑定的实体,即Entity()

This is Ok 还行吧

Entity lidmaatschapbedrijf = new Entity("new_ lidmaatschapbedrijf")
lidmaatschapbedrijf.acm_name = "constructed name";
localcontext.PluginExecutionContext.InputParameters["Target"] = lidmaatschapbedrijf;

You can also do this... 您也可以这样做...

localcontext.PluginExecutionContext.InputParameters["Target"] = lidmaatschapbedrijf.ToEntity<Entity>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM