简体   繁体   English

Web Api项目中的CrmServiceClient重用或连接池

[英]CrmServiceClient reuse or connection pooling in Web Api project

I am trying to understand how to use CrmServiceClient of the CRM SDK in a Web Api project and want to confirm if my understanding is correct. 我试图了解如何在Web Api项目中使用CRM SDK的CrmServiceClient,并想确认我的理解是否正确。

Is it true that CrmServiceClient implements connection pooling? CrmServiceClient是否实现了连接池? If so, is it valid to simply create a new instance per web-request as the overhead of creating a new connection will be mitigated by CrmServiceClient? 如果是这样,为每个Web请求创建一个新实例是否有效,因为CrmServiceClient将减轻创建新连接的开销?

Or should I use a single instance of CrmServiceClient throughout the entire application? 或者我应该在整个应用程序中使用单个CrmServiceClient实例吗? I don't think this works because my understanding is it is not thread safe and eventually the connection will close (I think). 我不认为这是有效的,因为我的理解是它不是线程安全的,最终连接将关闭(我认为)。

It says in the documentation for CrmServiceClient : 它在CrmServiceClient文档中说:

Thread Safety 线程安全
Any public static (Shared in Visual Basic) members of this type are thread safe . 此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的 Any instance members are not guaranteed to be thread safe. 任何实例成员都不保证是线程安全的。

(Emphasis my own) (强调我自己)

I certainly use one static instance of CrmServiceClient throughout my applications and have never experienced an issue. 我当然在我的应用程序中使用CrmServiceClient的一个静态实例,并且从未遇到过问题。

I found that CrmServiceClient is thread-safe as stated here https://msdn.microsoft.com/en-us/library/dn688177.aspx : All the calls to Dynamics 365 using the CrmServiceClient class are thread safe. 我发现CrmServiceClient是线程安全的,如此处所述https://msdn.microsoft.com/en-us/library/dn688177.aspx使用CrmServiceClient类对Dynamics 365的所有调用都是线程安全的。

In the application I am working on I have cached a single instance of CrmServiceClient which is used throughout the application. 在我正在处理的应用程序中,我已经缓存了整个应用程序中使用的CrmServiceClient的单个实例。 Before using it I check the IsReady property. 在使用之前,我检查IsReady属性。 This single instance is used to instantiate XrmServiceContext instances (in using blocks) throughout the application. 此单个实例用于在整个应用程序中实例化XrmServiceContext实例(使用块)。

Also, in my tests it appeared connection pooling was not something that was performed by CrmServiceClient. 此外,在我的测试中,似乎连接池不是由CrmServiceClient执行的。 I did a test where I called an API endpoint 100 times and for each call the instantiation time of CrmServiceClient remained mostly the same. 我做了一个测试,我调用了一个API端点100次,每次调用CrmServiceClient的实例化时间基本保持不变。 I could be wrong but I am thinking it does not perform connection pooling. 我可能错了,但我认为它不会执行连接池。

The connection is indeed pooled. 这种联系确实是汇集在一起​​的。 It's common to use a single connection throughout an application, but it depends on what you want to accomplish. 在整个应用程序中使用单个连接是很常见的,但这取决于您想要完成的任务。 Adding "RequireNewInstance=True" to your connection string will create a new connection each time it's used. 将“RequireNewInstance = True”添加到连接字符串将在每次使用时创建新连接。 I typically only do this when I'm creating integrations where I need to push more data simultaneously. 我通常只在我创建需要同时推送更多数据的集成时执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM