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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to avoid passing a context reference among classes

(0) ShareShare
ReportReport
Posted on by 1,250

Dynamics CRM 2011 on premise. (But this problem exists in many situations away from Dynamics CRM.)

CRM plugins have an entry point:

void IPlugin.Execute (IServiceProvider serviceProvider)

(http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iplugin.execute.aspx)

serviceProvider is a reference to the plugin execution context. Anything useful that a plugin does requires accessing serviceProvider, or a member of it.

Some plugins are large and complex and contain several classes. For example, I'm working on a plugin that has a class which is instantiated multiple times. This class needs to use serviceProvider.

One way to get access to serviceProvider from all the classes that need it would be to add a property to all those classes and then to set that property. Or to add properties for the parts of serviceProvider that each class needs. Either of these approaches would result in lots of duplicate code.

Another approach would be to have a global variable in the scope of the thread. However, according tohttp://msdn.microsoft.com/en-us/library/cc151102.aspx one "should not use global class variables in plug-ins."

So what is the best way to have access to serviceProvider without passing it around everywhere?

(Please answer at http://stackoverflow.com/questions/19119714/how-to-avoid-passing-a-context-reference-among-classes)

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    Hi,

    As you say, you shouldn't put a member variable on the plugin since instances are cached and reused between requests.

    The approach I take is to create a class that perform the task you need and pass the executionContext provided by the Developer Toolkit (msdn.microsoft.com/.../hh372957.aspx) on the constructor. Your class  then can store the instance for the purposes of executing it's work just in the same way you would with any other piece of code. You are essentially de-coupling from the restrictions imposed by the Plugin framework. This approach also makes it easier to unit test since you only need to provide the execution context to your class rather than mocking the entire plugin pipeline.

    It's worth noting that there is a bug in the automatically generated Plugin.cs class in the Developer Toolkit where it doesn't set the ServiceProvider property - At the end of the constructor of the LocalPluginContext add the line:

    this.ServiceProvider = serviceProvider;

    hth,

    Scott

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans