CodeActivity GetExtension null

时间:2015-10-14 09:41:26

标签: c# dynamics-crm

说我从未使用过Workflow并且我从一个示例开始编码,我遇到了CodeActivityContext.GetExtension<>的问题功能

这是构造函数

public SendReportMethod(Guid var1, string var2)
{
    this.var1= var1;
    this.var2= var2;
}

这是给我错误的代码:

protected override void Execute(CodeActivityContext  executionContext)
{
    IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
    IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
}

变量都是null,最后一个是obiouvsly给我nullPointer。

这就是我调用CodeActivity的方式:

WorkflowInvoker.Invoke(new SendReportMethod(interventionID, codiceFornitore));

我做错了什么?任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:2)

您似乎正在使用Dynamics CRM,并且希望在工作流程中使用自定义工作流活动。这是一个如何做到这一点的例子(见https://msdn.microsoft.com/en-us/library/gg334455.aspx

此外,这是一个更全面的例子:https://www.credera.com/blog/technology-insights/microsoft-solutions/three-essential-custom-workflow-activities-crm-2013/

public sealed class SimpleSdkActivity : CodeActivity
{
    [Input("Account Name")]
    [Default("Test Account")]
    public InArgument<string> AccountName { get; set; }
    protected override void Execute(CodeActivityContext executionContext)
    {
        //Create the tracing service
        ITracingService tracingService = executionContext.GetExtension<ITracingService>();

        //Create the context
        IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
        IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        var entityName = AccountName.Get<string>(executionContext);
    }
}