locked
Can not create instance of a plug-in. RRS feed

  • Question

  • I can register my plugin but when I run it I get the following error:

    "Can not create instance of a plug-in. Verify that plug-in type is not defined as abstract and it has a public constructor supported by CRM SDK."

    Anyone know how to resolve this?

    Tuesday, October 14, 2014 2:52 PM

All replies

  • Hi,

    Is the class and constructor public? Could you show some of the code?


    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Tuesday, October 14, 2014 3:40 PM
  • Hi,

    Please make sure your class and constructor is public.

    public class SamplePlugin: IPlugin
    {
    }

    Please make sure you have constructor signature is in one of the below form.

    public SamplePlugin()
    public SamplePlugin(string unsecure)
    public SamplePlugin(string unsecure, string secure)

    Thanks!

    http://social.microsoft.com/Forums/en-US/home?category=dynamics  

    • Proposed as answer by Mohd Saad Wednesday, October 15, 2014 6:18 AM
    Wednesday, October 15, 2014 5:33 AM
  • It is public.

    Code below:

     public class PreContractsCreate: Plugin
        {
            /// <summary>
            /// Initializes a new instance of the <see cref="PreContractsCreate"/> class.
            /// </summary>
            public PreContractsCreate()
                : base(typeof(PreContractsCreate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Create", "dcc_contracts", new Action<LocalPluginContext>(ExecutePreContractsCreate)));
    
                // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
                // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
            }
    
            /// <summary>
            /// Executes the plug-in.
            /// </summary>
            /// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the
            /// <see cref="IPluginExecutionContext"/>,
            /// <see cref="IOrganizationService"/>
            /// and <see cref="ITracingService"/>
            /// </param>
            /// <remarks>
            /// For improved performance, Microsoft Dynamics CRM caches plug-in instances.
            /// The plug-in's Execute method should be written to be stateless as the constructor
            /// is not called for every invocation of the plug-in. Also, multiple system threads
            /// could execute the plug-in at the same time. All per invocation state information
            /// is stored in the context. This means that you should not use global variables in plug-ins.
            /// </remarks>
            protected void ExecutePreContractsCreate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                if (localContext.PluginExecutionContext.InputParameters.Contains("Target") &&
                    localContext.PluginExecutionContext.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)localContext.PluginExecutionContext.InputParameters["Target"];
                    // Set up everything ready for the CreateQuery
                    IPluginExecutionContext context = localContext.PluginExecutionContext;
                    IOrganizationService service = localContext.OrganizationService;
                    OrganizationServiceContext mycontext = new OrganizationServiceContext(service);
    
                    //Verify that the target entity represents an account.                
    
                    if (entity.LogicalName == "dcc_contracts")
                    {
                        //set the default number
                        //string nextnumber = "6";
                        
                        //find the last used increment number
                       var query = (from n in mycontext.CreateQuery<dcc_contracts>()
                                     orderby n.dcc_ID descending          // Sort elements
                                     select n.dcc_tempno).FirstOrDefault();            // Translate each element (project) 
    
                        if (query == null)
                        {
                            var nextnumber = "6";
    
                            var newnumber = string.Format("{0:d5}", Int32.Parse(nextnumber.ToString()) + 1);
    
                            entity.Attributes.Add("dcc_tempno", newnumber);
                            entity.Attributes.Add(("FWK") + "dcc_no", newnumber);
    
                        }
                        else
                        {
                            var nextnumber = query;
    
                            var newnumber = string.Format("{0:d5}", Int32.Parse(nextnumber.ToString()) + 1);
    
                            entity.Attributes.Add("dcc_tempno", newnumber);
                            entity.Attributes.Add(("FWK") + "dcc_no", newnumber);
    
                        }
                    }
                   }
                }
              }
          }

    Wednesday, October 15, 2014 10:31 AM
  • Hi,

    The Execute method needs to be public as well. I would think it will work if you change that to "public void Execute..."

    EDIT: you don't have an execute when I read more carefully :)

    There are a couple of errors, have a look at the code here which should point you in the right direction. In shourt, your constructor shouldn't call the Execute, which in turn should just be "Execute"

    Good Luck!


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se


    Wednesday, October 15, 2014 10:36 AM
  • Still same error
    Wednesday, October 15, 2014 12:48 PM
  • How does the code look like after the change? Can you remove everything except the class, constructor and execute just to try that?

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Wednesday, October 15, 2014 12:50 PM
  • Hi,

    Would you please check base class Plugin code and paste your code? I think You are using Toolkit to create plugin and while registration you have chosen base Plugin while registering step for assembly.

    public class PreContractsCreate: Plugin

    Thanks!

    Wednesday, October 15, 2014 6:31 PM