0

我希望插件自动触发。FakeXrmEasy 支持这个吗?我尝试了以下 - 插件:

    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()));
    }

但是插件永远不会执行。

4

2 回答 2

2

那是因为框架内部使用EntityTypeCode实体的字段来获取注册的插件。

此字段不是Entity基类的一部分,但存在于 crmsvcutil 生成的早期绑定类中。

此外,实例的UsePipelineSimulation属性XrmFakedContext默认为 false,因此不会运行任何插件,您必须将其设置为true.

于 2020-03-13T13:15:53.730 回答
0

如果有人感兴趣,最后的片段如下所示:

var context = new XrmFakedContext { UsePipelineSimulation = true };
context.RegisterPluginStep<Foo, Account>("Create", ProcessingStepStage.Postoperation);
var organizationService = context.GetOrganizationService();
organizationService.Create(new Account { LogicalName = "account" });

实体元数据(全部)必须考虑如下:

[EntityLogicalNameAttribute("account")]
public class Account : Entity
{
    [AttributeLogicalName("name")]
    public string Name { get; set; }

    public int EntityTypeCode;

    public Account()
    {
        this.EntityTypeCode = 1;
    }
}

包括仅限系统的元数据:

[EntityLogicalName("plugintype")]
public class PluginType : Entity
{
    [AttributeLogicalName("plugintypeid")]
    public Guid PluginTypeId { get; set; }

    [AttributeLogicalName("name")]
    public string Name { get; set; }

    [AttributeLogicalName("typename")]
    public string TypeName { get; set; }

    [AttributeLogicalName("assemblyname")]
    public string AssemblyName { get; set; }

    [AttributeLogicalName("major")]
    public int Major { get; set; }

    [AttributeLogicalName("minor")]
    public int Minor { get; set; }

    [AttributeLogicalName("version")]
    public string Version { get; set; }
}


[EntityLogicalName("sdkmessage")]
public class SdkMessage : Entity
{
    [AttributeLogicalName("sdkmessageid")]
    public Guid SdkMessageId { get; set; }

    [AttributeLogicalName("name")]
    public string Name { get; set; }
}


[EntityLogicalName("sdkmessagefilter")]
public class SdkMessageFilter : Entity
{
    [AttributeLogicalName("sdkmessagefilterid")]
    public Guid SdkMessageFilterId { get; set; }

    [AttributeLogicalName("primaryobjecttypecode")]
    public int PrimaryObjectTypeCode { get; set; }
}


[EntityLogicalName("sdkmessageprocessingstep")]
public class SdkMessageProcessingStep : Entity
{
    [AttributeLogicalName("configuration")]
    public string Configuration { get; set; }

    [AttributeLogicalName("eventhandler")]
    public EntityReference EventHandler { get; set; }

    [AttributeLogicalName("sdkmessageid")]
    public EntityReference SdkMessageId { get; set; }

    [AttributeLogicalName("sdkmessagefilterid")]
    public EntityReference SdkMessageFilterId { get; set; }

    [AttributeLogicalName("filteringattributes")]
    public string FilteringAttributes { get; set; }

    [AttributeLogicalName("mode")]
    public OptionSetValue Mode { get; set; }

    [AttributeLogicalName("stage")]
    public OptionSetValue Stage { get; set; }

    [AttributeLogicalName("rank")]
    public int Rank { get; set; }
}
于 2020-03-16T22:27:01.990 回答