hello B
hello B

Reputation: 963

In CRM 2011, which Class calls dll Plug-in installed?

I need to know which class of CRM 2011 calls the plug-in installed on.

I'm new to plug-ins but I think each call to them, uses the same process. I need to make them to work on a different instance .

Let me introduce an example. I have an implementation of IPlugin class with a member called _log.

public class Plugin : IPlugin { 
    private static String _log;
}

So, if I have asynchronous plug-in, each time plug triggers, I should have new object, right? When I watch the log file, I notice that in the same log there's more than one process of the plug-in. Example: "Log-> obj1.method_1, obj2.method_1" It's like there's only one instance for the class plug-in and each time the plug-in triggers, it just calls the Execute() method with a different serviceProvider parameter. It's that possible? Any idea?

Upvotes: 0

Views: 473

Answers (2)

Daryl
Daryl

Reputation: 18895

I'm not sure what you're asking, but I'm guessing your having some multi-threading problems with plugins. There is no way to change it so a new object gets created every time. Any class level variables are going to create race conditions. Why do you think that you need each plugin call to be in a different process?

Edit 1

Why don't you just create a new object as the first step of your plugin, passing in the execution process? Move all class level variables defined in your plugin to your new class. That way you don't have to worry about multi-threading issues.

Edit 2 - Example ##

// Your actual IPlugin class would only contain this, nothing else.
public class MyPlugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {       
        new MyPluginLogic().ExecutePluginLogic(serviceProvider);    
    }
}

public class MyPluginLogic{
    private static String _log;

    public void ExecutePluginLogic(IServiceProvider serviceProvider){
        // Do what ever logic you were previously doing in your plugin class.
    }
}

If you setup your code this way, you are guaranteed to get a new MyPluginLogic object each time the plugin is called, which will eliminate any possible non-static race conditions.

If you're still having issues, post your code.

Upvotes: 1

Greg Owens
Greg Owens

Reputation: 3878

It sounds like you've encountered a problem with your logic and have decided on a way to fix it that is not compatible with the way CRM is supposed to work. Take a step back and describe why you feel that "each call must work on a different instance".

As @Daryl correctly states, there isn't a way to change the caching of the Plugin object and that's precisely why the CRM SDK states:

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. Also, multiple system threads could execute the plug-in at the same time. All per invocation state information is stored in the context, so you should not use global variables in plug-ins or attempt to store any data in member variables for use during the next plug-in invocation.

Upvotes: 0

Related Questions