locked
What exactly does the IServiceFactory do? RRS feed

  • Question

  • I have seen some plugin include the service factory while others have not - what exactly does it do?
    Wednesday, October 14, 2015 2:46 PM

All replies

  • I think you are referring to IOrganizationServiceFactory, and in this case it's required to get an IOrganizationService instance inside the plugin.

    If inside your plugin you don't need an IOrganizationService instance (for example you don't need to do a service.Retrieve or a service.Update) you don't need to instantiate it.


    My blog: www.crmanswers.net - CRM Theme Generator

    • Proposed as answer by Guido PreiteMVP Wednesday, October 14, 2015 3:20 PM
    Wednesday, October 14, 2015 3:20 PM
  • You are right, there are two lines:

    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

    I have seen code that instantiates the service but not the service factory - what is that about?

    Wednesday, October 14, 2015 3:28 PM
  • Re 'I have seen code that instantiates the service but not the service factory' - if you use the serviceFactory to get the IOrganizationService instance, then any operations an that instance will run in the same context (SQL transaction and Depth) as the operation that caused the plugin. If you instantiate an IOrganizationService without using the serviceFactory, then any operations will be in a different context

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Monday, October 19, 2015 7:13 PM
    Moderator