Welcome to MSDN Blogs Sign in | Join | Help
Debugging Plug-in

During MSCRM plug-in development, developer would want to test his/her plug-in before putting in production server to make sure it works as designed. One way to do it is to have a development environment to test plug-in. However, the turn around time between deploy-test-debug-build-deploy is very significant even putting plug-in on disk. It would be nice if we can test plug-in locally without deploying it on CRM server. The approach I am going to show is to execute plug-in in a small EXE container. Since IPluginExecutionContext is just interface, we can mock the context in our test container. Effectively, you will be able to rapidly develop/test/debug plug-in without ever leaving Visual Studio until you are ready.

image

I created a sample solution containing a simple test context and a test plug-in on code.msdn (http://code.msdn.microsoft.com/MSCRMPluginDebugger). This sample shows you how to populate relevant input parameters and how to create CrmService behind context. You might need to add more implementation to TestContext to suite your need but this sample should be a foundation you can build on for example pre/post image or MetadataService.

Developer can use this approach for both debugging during development or for writing unit test. It would enable ISV to write unit test and run it from test harness/framework such as one provided by TFS.

Even though mocking IPluginExecutionContext can emulate container that plug-in will execute in, it is only approximation. Other differences can still make running in production different from test context, for example:

- Security context. CRM runs plug-in in service account (usually network service). Test contain runs plug-in in currently logged in user context. Files/registries may be accessible from log in user but service account may not , and vice versa.

- HttpContext. Accessing HttpContext.Current is not recommended. If you need something that is available only HttpContext.Current, please let us know. Ideally, plug-in should be able to get everything from IPluginExecutionContext.

NOTE: Depending on currently logged in user, you may or may not be able to impersonate a particular user. Only AD user in PrivUserGroup can impersonate other user in CRM.

Posted: Wednesday, May 07, 2008 2:38 AM by Akezyt

Comments

PaulR_NZ said:

Hi Akezyt,

I would like to do this with a custom workflow plugin however I think as the class inherits from SequenceActivity rather than implementing IPlugin, the way to invoke my Execute method is a little different to what you show here.

ie you use the following code:-

IPlugin plugin = (IPlugin)Activator.CreateInstance(typeof(MyISVPluginClass));

TestContext context = new TestContext(MessageName.Create, userId);

context.InputParameters[ParameterName.Target] = entity;

plugin.Execute(context);

I believe this needs to change for Workflow plugins however I am a definite workflow newbie.  I am guessing at something like the following but it's not even close so any advice would greatly appreciated.

SequenceActivity activity = (SequenceActivity )Activator.CreateInstance(typeof(MyWorkflowActivityClass));

TestContext context = new TestContext(MessageName.ExecuteWorkflow, userId);

context.InputParameters[ParameterName.Target] = entity;

WorkflowRuntime wfr = new WorkflowRuntime(somethingHere?);

wfr.ExecuteActivity(activity, context);

Any thoughts on how this might be done with a workflow plugin would be greatly appreciated.

kind regards,

Paul.

# August 7, 2008 5:05 PM
Anonymous comments are disabled
Page view tracker