Warm tip: This article is reproduced from serverfault.com, please click

Debugging a Dynamics365 code activity locally

发布于 2020-12-16 08:47:01

I have a Dynamics365 (in the cloud) setup and I am tasked with debugging some of its custom workflow activities that are suspected of not working properly. As a long-time desktop developer, this whole cloud setup makes it very tricky to do any decent debugging.....

So my code activities all look something like this:

public class MyCustomActivity : CodeActivity
{
    public IOrganizationService OrganizationService { get; private set; }
    public IExecutionContext ExecutionContext { get; private set; }
    public CodeActivityContext ActivityContext { get; private set; }
    public ITracingService TracingService { get; private set; }

    // Bunch of input parameters
    [Input("param1")]
    public InArgument<string> Param1 { get; set; }

    [Input("param2")]
    public InArgument<string> Param2 { get; set; }

    // possibly output parameters
    
    // main method with all the logic
    protected override void Execute(CodeActivityContext context)
    {
        var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

        this.ActivityContext = context;
        this.ExecutionContext = context.GetExtension<IExecutionContext>();
        this.OrganizationService = serviceFactory.CreateOrganizationService(ExecutionContext.UserId);
        this.TracingService = context.GetExtension<ITracingService>();

        // actual core of the code activity
    }
}

Very basic, run-of-the-mill code for a Dynamics code activity, I believe.

I was wondering if there's a way (without moving heaven and hell) to create a custom (not really "faked") CodeActivityContext so that I could basically write a little "test app" that would create such a context, instantiate the code activity, and then run its Execute method? This would allow me to actually run and debug the test app, and go through it and see what's going on (and what might be going wrong).

I am aware of the Fake Xrm Easy package by Jordi Montana and have used it in my integration testing (wonderful piece of code!) - but I think that won't really work for me in this scenario, since I need the "real thing" - I need to have a working IOrganisationService primarily, to run the code activity's code and see what it's doing with the Dynamics platform...

Any ideas? Has anyone else attempted to do something like this before??

Questioner
marc_s
Viewed
0
21.6k 2020-12-16 20:44:46

I accomplish this by decoupling the plug-in/activity logic from the plug-in/activity plumbing. The logic lives in its own class and is called from the Execute method. Pass in things you need in your logic like the org service, inputs, etc from the context and inputs. The Execute method usually just has a single line of code that calls these logic methods.

Now in your local console testing app, just reference those logic methods - they only need basic things like the org service and a few parameters, not the whole Context. Architecting your code this way also lets you easily share logic between plugins/activities, since plugins/activities are really just wrappers around your main logic.