1

We're creating an ASP.NET MVC app that talks to CRM 2011. We are using the Xrm.Client.Services.OrganizationService. We have a singleton pattern in place for this.

Under load we are seeing an ObjectDisposedException (cannot access a disposed object). I'm thinking that a singleton is not the way to go here.

I'm considering a [ThreadStatic] singleton, or a pool of services, or anything else that might help. Is anyone aware of any limits on the number of connections to CRM that can be made? Anyone experienced the disposed service before? I've been told that having too many org services connecting to CRM at once can be problematic as well, so that's why I'm not creating a new one for each hit to CRM.

Thanks for any help you can provide! If any more clarification is required, please ask.

  • 1
    `Xrm.Client.Services.OrganizationService` is not thread-safe so the idea of wrapping a single instance of it in a Singleton is sure to start causing problems. I would have a look at https://xrmlibrary.codeplex.com/ (whether you choose to use it or roll your own) you can see how they handle pooling instances of `IOrganizationService`, which is what you need here. Also see this related answer: http://stackoverflow.com/questions/12764225/thread-safety-for-organizationserviceproxy-and-generated-xrm-service-context – Nicknow Jun 02 '15 at 02:05
  • @Nicknow you should write this comment as answer – Guido Preite Jun 02 '15 at 03:37

1 Answers1

2

The OrganizationService is not multi-thread safe. It is also user aware, so depending on how you've implemented the creation of the service, you may or may not be able to share it among users. (If you aren't impersonating it, you should be fine, but then you lose out on all of the integrated CRM security)

At one point in time I did write a CRM OrganizationService Pool, but it really wasn't any different then creating a service when needed. Ideally you'll probably only want to create a service once per request of a user.

Daryl
  • 18,592
  • 9
  • 78
  • 145