Debugging a Plug-in

Plugin is a .NET assembly that can be used to intercept events generated from the CRM system to perform a variety of actions. Some common plug-ins will perform a complicated update routine on CRM entities and\or attributes when it might be impractical to use Javascript or Workflow.

Not like in most of the other developments, in Plugins developers have to debug the CRM plugin code at the time of running the application. Developers writing the plugin code inside the plugin and some times the logic can be write in a seperate assembly and from the plugin code it can be invoked. When writing the whole logic inside the plugin is little complex to test the plugin logic by debugging the plugin as it executes from the application itself in a given event such as create an entity, update or delete.

There are two ways of debugging a CRM plugin as follows,

[1] Attaching the debugger to the w3p.exe process
[2] Forcing the add-in to call the debugger


Demonstration

A plugin has written targeting Account entities as follows and a step has registered to be fired on Account entity’s Create step.

namespace CrmDev.Plugins
{
    public class AccountCreatePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context =
                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Plug-in business logic goes below this line.
            // Invoke organization service methods.
            Entity entity = (Entity)context.InputParameters["Target"];

            throw new Exception("Throwing exception from Account Create Plugin");

            // Continue with the pluging business logic
        }
    }
}



[1] Attaching the debugger to the w3p.exe process

Just build the plugin project in Debug mode, register the assembly in CRM Application using Plugin Registration tool and register the step as well.
Place the .pdb file of the plugin assembly in server\bin\assmbly forder
E.g. \Server\bin\assembly

Set break points in the plugin code and attach the debugger to the w3wp.exe process.


If the correct w3wp.exe cannot be identified just attach all the processes.


Open a New Account Form, enter data for required fields and try to save the record.


It points to the first breakpoint placed, and can be debug the plugin from that point.



[2] Forcing the add-in to call the debugger

Following statement can be placed in the plugin code and it can be built and register the plugin using Plugin Registration tool.

System.Diagnostics.Debugger.Launch();


Plugin assembly can be registered with a debug biuild or using a release build.

namespace CrmDev.Plugins
{
    public class AccountCreatePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            System.Diagnostics.Debugger.Launch();

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context =
                (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            // Plug-in business logic goes below this line.
            // Invoke organization service methods.
            Entity entity = (Entity)context.InputParameters["Target"];

            throw new Exception("Throwing exception from Account Create Plugin");

            // Continue with the pluging business logic
        }
    }
}


Open a new Account form, fill required fields and try to save the record. Followinf windows will be popup.


The recommended way is to debug with a new instance.


It gives environment to debug the plugin code with a new Visual Studio instance.


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s