Dynamics CRM 2016 Global Variables

I am just incrementing a static global variable in the Dynamics CRM 2016 plugin and it shows a weird random pattern like below. Why does this show strange behavior and pattern?

Magazine image

Below is the code I am using.

 public class MyPlugin : IPlugin
{
    private static int count = 0;

    public void Execute(IServiceProvider serviceProvider)
    {

        try
        {
          if (_objContext.InputParameters.Contains("Target") &&     _objContext.InputParameters["Target"] is Entity)
            {
                WriteLog("Count value before increament: " + count,  service);
                count = count + 1;
                WriteLog("Count value after increament: "+count, service);
             }
         }
     }
  }

      

0


source to share


2 answers


By the looks of it, you have two web application servers currently hosting your CRM instance (or they are asynchronous plugins, in which case you have two asynchronous servers serving your CRM instance). Each server has its own local version MyPlugin.count

, so you see strange behavior.

Application domains in CRM are a little easier for unauthorized plugins, this one is for Crm web server. Plugin sandboxes are a little more complex. Each stage of plugin registration has its own unique domain. In order for these values ​​to be in sync, this requires a CRM database (or something else external CRM).



I created an auto-numbering solution that does exactly that, using the newer version of CRM 2015 that allows optimistic updates. But unfortunately Microsoft has a bug where version # is zero for isolated plugins, so it will only work in the preview environment until the bug is resolved.

Update: The bug has been resolved.

+6


source


As MS says on MSDN:

To improve performance, Microsoft Dynamics CRM caches the instance plug-in. The plugin's Execute method must be written as stateless because the constructor is not called for every plugin call. In addition, multiple threads of the system can execute the plugin simultaneously. All call state information is kept in context, so you shouldn't use global variables or try to store any data in member variables for use during the next plugin ...



https://msdn.microsoft.com/en-us/library/gg328263.aspx

Simply put, don't use local variables in plugins. If you are looking for autostart only to use an approach like this https://www.linkedin.com/pulse/custom-auto-numbering-6-quick-steps-ms-dynamics-crm-eran-fuks

+5


source







All Articles