OrganizationServiceProxy和生成的xrm服务上下文的线程安全 [英] Thread safety for OrganizationServiceProxy and generated xrm service context
问题描述
这是最佳做法问题。
每最佳做法文章,并根据MSDN, OrganizationServiceProxy
不是线程安全的。
This is a best practices question.
Per this best practices article and per MSDN, the OrganizationServiceProxy
is not thread safe.
如果您有一个多线程客户端应用程序,在其中创建
的实例< a href = http://msdn.microsoft.com/zh-cn/library/microsoft.xrm.sdk.client.organizationservicecontext.aspx rel = nofollow> OrganizationServiceContext
(基于每个线程),其构造函数接受
IOrganizationService
实例,您传入 OrganizationServiceProxy
(即在流程级别分配一次的静态实例),是否会导致线程问题和/或 OrganizationServiceProxy
实例错误,它将影响线程尝试在其自己的 OrganizationServiceContext
?
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
?
我的信念就是这样,并且 OrganizationServiceProxy
实例需要在每个线程的基础上创建,并且每个多线程应用程序中的 OrganizationServiceContext
将需要其相应的 OrganizationServiceProxy
实例。
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.
此外,文章指出
服务代理类通过使用以下类方法执行元数据下载和用户身份验证
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)
通过缓存服务管理和经过身份验证的凭据对象,您的应用程序可以更有效地构造服务代理对象每次应用会话的次数超过一次
如果我尝试在Active Directory身份验证模式下手动执行上述API调用, authCredentials.SecurityTokenResponse
为由MSDN指示
If I try to execute the above API calls manually, in Active directory authentication mode, the authCredentials.SecurityTokenResponse
is null as indicated by MSDN
是否有一种方法可以仅对AD模式执行一次身份验证并通过已认证 SecurityTokenResponse
到新创建的 OrganizationServiceProxy
通过以下构造函数?
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)
这样,在构造 OrganizationServiceProxy
每个线程有一个实例,只命中一次吗?
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?
推荐答案
是的,如果您尝试使用多线程,肯定会遇到问题单个IOrganization服务上的操作。
Yes, you will definitely have issue if you attempt multi-threaded operations on a single IOrganization service.
我们有两个基本的多线程CRM应用程序:批处理程序和另一个Web应用程序。对于批处理程序,我发现最好只有10个不同的线程,并在10个不同的线程之间进行批处理。因此,如果您要插入100,000条记录,请将它们分成10批,每批10,000条,每个线程一个组织服务。
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.
我们也有一个网站,它做很多工作CRM交互,因此没有真正的方法来批量处理请求,因此我们创建了一个CRM连接池以重用任何打开的,已通过身份验证的连接。
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.
这篇关于OrganizationServiceProxy和生成的xrm服务上下文的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!