Thread safety for OrganizationServiceProxy and generated xrm service context

孤街醉人 提交于 2019-12-07 12:08:43

问题


This is a best practices question. Per this best practices article and per MSDN, the OrganizationServiceProxy is not thread safe.

If you have a multi threaded client application in which you are creating an instance of an OrganizationServiceContext (on a per thread basis), the constructor of which accepts an IOrganizationService instance and you pass in a global instance of the OrganizationServiceProxy (i.e a static instance allocated once at the "process level"), will this cause threading issues and/or if the OrganizationServiceProxy instance faults, will it affect operations that the threads try to perform on their own "local" instance of the OrganizationServiceContext?

My belief is that it will, and that an OrganizationServiceProxy instance needs to be created on a "per thread" basis and that each OrganizationServiceContext in a multi threaded application would need its own corresponding OrganizationServiceProxy instance.

I'm posting this to get confirmation of the above.

Also, the article indicates

The service proxy class performs the metadata download and user authentication by using the following class methods

IServiceManagement<IOrganizationService> orgServiceManagement =
                 ServiceConfigurationFactory.CreateManagement<IOrganizationService>(
                 new Uri(organizationUrl))

AuthenticationCredentials authCredentials = orgServiceManagement.Authenticate(credentials)

By caching the service management and authenticated credential objects, your application can more efficiently construct the service proxy objects more than one time per application session

If I try to execute the above API calls manually, in Active directory authentication mode, the authCredentials.SecurityTokenResponse is null as indicated by MSDN

Is there a way to perform the authentication just once for AD mode and pass an authenticated SecurityTokenResponse to a newly created OrganizationServiceProxy via the following constructor?

OrganizationServiceProxy (IServiceConfiguration, SecurityTokenResponse)

so that you don't have to take the authentication and metadata download hit on a "per thread basis" when constructing the OrganizationServiceProxy instance per thread and just take the hit once?


回答1:


Yes, you will definitely have issue if you attempt multi-threaded operations on a single IOrganization service.

We have two basic multi-threaded CRM applications: batch processors, and another web app. For the batch programs I've found it works better to only have 10 different threads, and to batch up the work among the 10 different threads. So if you're inserting 100,000 records, split them into 10 batches of 10,000, a single organization service for each thread.

We also have a website that does a lot of CRM interactions so there is no real way to batch the requests, so we created a CRM connection pool to reuse any open, already authenticated connections.

Of course this won't work at all if you're not using some system service account.



来源:https://stackoverflow.com/questions/12764225/thread-safety-for-organizationserviceproxy-and-generated-xrm-service-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!