您是否仍需要检查父

您是否仍需要检查父

本文介绍了您是否仍需要检查父/子管道,以确定是在2015年创建ICrmService还是CrmService?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We currently use MS Dynamics CRM V4 and are in the process of upgrading to 2015. I've got the task of updating some of our plugins.

我碰到的一件令人困惑的事情是,我是否仍然需要在管道的阶段进行某种检查,以确定它是父母还是孩子.据我了解,自2011年起,父管道和子管道已合并为1,那么应如何更改以下代码?

1 of the things I've come across that is a little confusing is whether I still need to do some sort of check on the stage of the pipeline to determine if it's a parent or child. As I understand it the parent and child pipelines have been merged into 1 as of 2011, so how should the following code be altered?

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创建服务?

So, I understand that previously in V4 you needed to use CrmService for child pipelines and ICrmService for parent ones, hence the if statement to determine which pipeline it came from. However, do I still need to do this kind of check or can I just do away with the whole if statement and just create the service using ICrmService?

推荐答案

您还需要重写类声明,在这里可以使用以下示例:

You need to rewrite also the class declaration, here an example you can use:

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);
            // ...
        }
    }
}

这篇关于您是否仍需要检查父/子管道,以确定是在2015年创建ICrmService还是CrmService?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:41