0

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?

4

2 に答える 2

1

何を求めているのかわかりませんが、プラグインでマルチスレッドの問題が発生していると思います。変更する方法がないため、毎回新しいオブジェクトが作成されます。クラスレベルの変数はすべて、競合状態を引き起こします。各プラグイン呼び出しを異なるプロセスにする必要があると思うのはなぜですか?

編集1

プラグインの最初のステップとして新しいオブジェクトを作成し、実行プロセスを渡してみませんか?プラグインで定義されているすべてのクラスレベル変数を新しいクラスに移動します。そうすれば、マルチスレッドの問題について心配する必要はありません。

編集2-例##

// 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.
    }
}

この方法でコードを設定するMyPluginLogicと、プラグインが呼び出されるたびに新しいオブジェクトを取得することが保証されます。これにより、静的でない競合状態が発生する可能性がなくなります。

それでも問題が解決しない場合は、コードを投稿してください。

于 2012-11-16T13:03:51.897 に答える
0

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.

于 2012-11-16T14:43:40.530 に答える