有没有办法自动强制执行插件
Is there a way how to automatically force plugin execution
我希望插件自动触发。 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()));
}
但是插件永远不会执行。
那是因为框架 internally 使用实体的 EntityTypeCode
字段来获取注册的插件。
此字段不是 Entity
基础 class 的一部分,但存在于 crmsvcutil 生成的早期绑定 classes 中。
此外,XrmFakedContext
实例的UsePipelineSimulation
属性默认为false,所以没有插件会运行,你必须将其设置为true
.
如果有人感兴趣,最后的代码片段如下所示:
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; }
}
我希望插件自动触发。 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()));
}
但是插件永远不会执行。
那是因为框架 internally 使用实体的 EntityTypeCode
字段来获取注册的插件。
此字段不是 Entity
基础 class 的一部分,但存在于 crmsvcutil 生成的早期绑定 classes 中。
此外,XrmFakedContext
实例的UsePipelineSimulation
属性默认为false,所以没有插件会运行,你必须将其设置为true
.
如果有人感兴趣,最后的代码片段如下所示:
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; }
}