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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Global class variables in plug-ins

(0) ShareShare
ReportReport
Posted on by

As you will likely have noted, the SDK for CRM 2011 (as for CRM 4.0) states:

Note For improved performance, Microsoft Dynamics CRM caches plug-in instances. The plug-in's Execute method should be written to be stateless because the constructor is not called for every invocation of the plug-in. In addition, multiple threads could be running the plug-in at the same time. All per invocation state information is stored in the context. This means that you should not use global class variables in plug-ins.

I understand, of course, the reason for not having volatile variables declared in the plug-in due to not being thread-safe and the constructor not always being called, however what about constants? Surely there can be no risk posed in such a scenario, since, as long as the value is present, it isn't being changed? Alternatively, what about global-scope variables that require a(n expensive) query to populate them and therefore it might be desireable to purposely cache those values between plug-in calls - particularly again assuming that the values are universal for all users.

As an example, suppose I use a custom system settings entity in CRM. I could use a global class variable which stores the value of these settings. At the point in my plug-in when I need to use a setting, I could check if my variable is null. If it is, I run a routine to fetch the values, if not, I use the values already stored. 

Can anyone comment on whether I am missing something here? Microsoft's statement feels (to me at least) too broad-brush. Perhaps this is for simplicity and unambiguity, but despite violating this "rule" I have not experienced any negative side effects when using the aforementioned approach so far. Of course, I may well be missing something...

Example:

  public class MyPlugin : IPlugin
 
{
      private static string _mySystemSetting;
  
      public void Execute(IServiceProvider serviceProvider)
      {
         if(_mySystemSetting == null){
          _mySystemSetting = GetSystemSetting("MySetting);
      }
      ...
  }
  
  private string GetSystemSetting(string settingName)
  {
    ...
  }
 }

*This post is locked for comments

I have the same question (0)
  • Ajay Ubbott Profile Picture
    410 on at
    Re: Global class variables in plug-ins

    I have a query on this question: -

    How to pass value to a global variable (to be used by plugins) from outside like a System-Setting.

    I need to set a configuration setting for my on-premise installation of CRM 2011, which will be read by plugin code to integrate with another legacy application.

    Thanks,

    \Ajay

  • Martin Donnelly Profile Picture
    1,030 on at
    RE: Global class variables in plug-ins

    Almost my question exactly -- like very slow moving data sets like an entities metadata list of all its DateTime attributes...

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans