locked
name autogenerator plugin on Create crashes really early on. RRS feed

  • Question

  • This is my C# plugin:

    public sealed class BedrijfsLidmaatschapPreValidator : IPlugin
        {
            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.");
                }
                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 it's registered:

    . What it does is it takes the the name of the Account lookup field in the record, prepends it with "Bedrijfslidmaatschap", then assigns this new value to the name parameter of the record. When I execute it though, I get the following exception:

    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: #DF3E3CFADetail: 
    <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: #DF3E3CFA</Message>
      <Timestamp>2014-05-09T07:56:12.1358117Z</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, the plugin doesn't even reach the first Trace() statement. I don't know why it goes wrong. The create goes through fine if I remove the plugin step, though. I don't know why this plugin fails. I just made a VS 2012 CRM solution using the SDK and adapted the default plugin it makes.

    Friday, May 9, 2014 9:05 AM

Answers

  • I forgot to pass an update.

    The problem was that I was trying to pass something different than an Entity to my Target parameter, which doesn't work.

    Thursday, May 22, 2014 6:58 AM

All replies

  • The parser is having a problem with the structure of your code.  I think it is expecting a plugin class constructor.  Something like:

    public BedrijfsLidmaatschapPreValidator (string unsecureConfiguration = null, string secureConfiguration = null) {}

    as a part of your class.

    Friday, May 9, 2014 2:08 PM
  • I forgot to pass an update.

    The problem was that I was trying to pass something different than an Entity to my Target parameter, which doesn't work.

    Thursday, May 22, 2014 6:58 AM