Search code examples
dynamics-crmfakexrmeasy

Is there a way how to automatically force plugin execution


I want the plugins to trigger automatically. Does the FakeXrmEasy support this ? I tried following - plugin:

    public class Foo : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // never breaks here
        }
    }


    public static EntityMetadata SetObjectTypeCode(EntityMetadata metadata, int value)
    {
        var property = typeof(EntityMetadata).GetProperty(nameof(metadata.ObjectTypeCode));
        property.SetValue(metadata, value);
        return metadata;
    }


    static void Main(string[] args)
    {
        var context = new XrmFakedContext();
        context.InitializeMetadata(SetObjectTypeCode(new EntityMetadata
        {
            LogicalName = "account",
        }, 1));

        context.RegisterPluginStep<Foo>("Create", primaryEntityTypeCode: 1);
        context.GetOrganizationService().Create(new Entity("account", Guid.NewGuid()));
    }

But the plugin is never executed.


Solution

  • That's because the framework internally use the EntityTypeCode field of the entity to get the registered plugins.

    This field is not part of the Entity base class but is present on the early bound classes generated by crmsvcutil.

    Also, the UsePipelineSimulation property of the XrmFakedContext instance is false by default so no plugin would run, you have to set it to true.