Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Do custom objects violate the thread safety of an IPlugin?

Posted on by 15

Hello,

I've been reading this article in Microsoft's documentation on best practices for developing IPlugin. I'm a bit confused regarding the "stateless" requirement. 

If I understand correctly, storing objects like the IPluginExecutionContext and its derivatives (e.g. IOrganizationService) is problematic because their object references might persist between executions, correct? There's also the fact that the server will cache the plugin for a certain amount of time, so it wouldn't call the constructor each time.

However, does this problem still exist if I create a class member from scratch in the Execute function, or functions called by Execute?

Let's use the following example:

public class MyPlugin : IPlugin 
{

    Entity customEntity;
    MyCustomObject customObject;

    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
        ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        
        Guid id = ((Entity)context.InputParameters["Target"]).Id;
        customEntity = service.Retrieve("new_customentity", id, new ColumnSet());
        
        DoStuff();
    }
    
    private void DoStuff() 
    {
        customObject = new MyCustomObject();
    }
}

In the example I posted, would customEntity (and customObject, for that matter) violate the stateless requirement of the plugin? Or would they also somehow get cached between executions?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans