问题
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.
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");
}
}
}
I've made a start like this:
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");
}
}
}
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?
回答1:
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);
// ...
}
}
}
回答2:
Actually Parent and Child pipelines were not merged. They still exist in Dynamics CRM 2011. I guess Microsoft simplified the model to prevent confusion. Also, in CRM 4.0 the child pipeline did not support free access to the CRM service; hence the different ways of creating ICrmService instances.
E.g. when an AssignRequest
is issued, the following plugin steps are invoked:
- Validate Assign
- PreOperate Assign
- PreOperate Update
- (Platform operations)
- PostOperate Update
- PostOperate Assign
- Async Update
- Async Assign
Steps 3 and 5 are actually child pipeline steps; they both have a parent context belonging to the Assign
message.
来源:https://stackoverflow.com/questions/31134751/do-you-still-need-to-check-the-parent-child-pipelines-to-determine-whether-to