web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Do custom objects violate the thread safety of an IPlugin?

(0) ShareShare
ReportReport
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?

I have the same question (0)

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans