
Use the factory to generate the Organization Service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)) Obtain the Organization Service factory service from the service provider Var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)) Obtain the tracing service from the service provider. If (pluginExecutionContext.Stage = 40 & pluginExecutionContext.MessageName = "Create" & pluginExecutionContext.PrimaryEntityName = "contact") Var pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)) Obtain the execution context service from the service provider. Public void Execute(IServiceProvider serviceProvider) Public Plugin(string unsecure, string secure) Private readonly string postImageAlias = "PostCreateImage" / Note: Only synchronous post-event and asynchronous registered plug-ins / The image contains the following attributes: / primary entity's attributes after the core platform operation executes.

/ Alias of the image registered for the snapshot of the Suppose we have a sample Dynamics CRM plugin, which is called after a contact is created within Dynamics CRM, if the parent customer is set to an account, the number of employees attribute of that account will be increased by one.

Through IServiceProvider we can retrieve at least 3 interface and we need to set up 5 interface, which will be IOrganizationService, IPluginExecutionContext, ITracingService, IOrganizationServiceFactory and IServiceProvider. Unit test CRM plugin is straightforward as well, it is just a bit tedious. IPlugin.Execute only accept one parameter which is IServiceProvider.
#Rhyno mocks code
The complete sample code using Rhino Mocks can be downloaded from MSDN sample gallery: Dynamics CRM unit test using Rhino Mocks

Dynamics CRM 2011 Unit Test Part 1: Introduction and Series Contents
