2015年还需要检查parent/child pipeline来决定是创建ICrmService还是CrmService?

Do you still need to check the parent / child pipelines to determine whether to create ICrmService or CrmService in 2015?

我们目前使用的是 MS Dynamics CRM V4,并且正在升级到 2015。我的任务是更新我们的一些插件。

我遇到的 1 件事有点令人困惑,那就是我是否仍需要对管道阶段进行某种检查以确定它是父级还是子级。据我了解,截至 2011 年,父管道和子管道已合并为 1,那么应如何更改以下代码?

public CrmServiceProxy(IPluginExecutionContext context, Guid userId)
{
    if (context.InvocationSource == MessageInvocationSource.Parent)
    {
        iCrmService = context.CreateCrmService(userId);
    }
    else
    {
        try
        {
            RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\MSCRM");
            string crmUrl = regkey.GetValue("ServerUrl").ToString();
            string crmServiceUrl = string.Concat(crmUrl, "/2007/crmservice.asmx");
            crmService = CreateCrmService(crmServiceUrl, context, userId);
        }
        catch (Exception)
        {
            throw new InvalidPluginExecutionException("Unable to create CrmServiceProxy - the service URL cannot be read from the Registry");
        }
    }
}

我是这样开始的:

private readonly IOrganizationService iCrmService;
private IOrganizationServiceFactory serviceFactory;

public CrmServiceProxy(IServiceProvider serviceProvider, Guid userId)
{
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

    if (context.Stage == 10) //10 should indicate it's the parent
    {
        iCrmService = serviceFactory.CreateOrganizationService(context.UserId);
    }
    else
    {
        try
        {
            RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\MSCRM");
            string crmUrl = regkey.GetValue("ServerUrl").ToString();
            string crmServiceUrl = string.Concat(crmUrl, "/2007/crmservice.asmx");
            iCrmService = serviceFactory.CreateOrganizationService(crmServiceUrl, context, userId); //doesn't work, just something I was trying
        }
        catch (Exception)
        {
            throw new InvalidPluginExecutionException("Unable to create CrmServiceProxy - the service URL cannot be read from the Registry");
        }
    }
}

所以,我知道之前在 V4 中,您需要对子管道使用 CrmService,对父管道使用 ICrmService,因此需要使用 if 语句来确定它来自哪个管道。但是,我是否仍需要进行此类检查,或者我是否可以取消整个 if 语句并仅使用 ICrmService 创建服务?

您还需要重写 class 声明,这里有一个您可以使用的示例:

namespace PluginNamespace
{
    public class MyPluginClass : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            // ...
        }
    }
}

实际上父管道和子管道没有合并。它们仍然存在于 Dynamics CRM 2011 中。我猜微软简化了模型以防止混淆。此外,在 CRM 4.0 中,子管道不支持免费访问 CRM 服务;因此创建 ICrmService 实例的方式不同。

例如当发出 AssignRequest 时,将调用以下插件步骤:

  1. 验证分配
  2. 预操作分配
  3. 操作前更新
  4. (平台运营)
  5. 操作后更新
  6. 操作后分配
  7. 异步更新
  8. 异步分配

步骤 3 和 5 实际上是子管道步骤;他们都有属于 Assign 消息的父上下文。