Replies: 3 comments 3 replies
-
Yes and yes, though depending on your use case you may want or o consider using clones of the connection per thread and using the async versions of the calls. |
Beta Was this translation helpful? Give feedback.
-
@MattB-msft Thanks for your reply, let me describe more about my scenario, we now have a asp.net core service, and we are now using this constructor I am now trying to create a factory like this to create ServiceClient for difference instanceuri public class ServiceClientFactory
{
public ServiceClient Create(Uri instanceUrl, Func<string, Task<string>> tokenProviderFunction)
{
return new ServiceClient(instanceUrl,tokenProviderFunction);
}
} I have some questions here: public class ServiceClientFactory
{
private static ConcurrentDictionary<Uri,ServiceClient> cache = new ConcurrentDictionary<Uri,ServiceClient>();
public ServiceClient Create(Uri instanceUrl, Func<string, Task<string>> tokenProviderFunction)
{
if(cache.TryGetValue(instanceUrl, out ServiceClient root))
{
return root.Clone();
}
var tempRoot = new ServiceClient(instanceUrl,tokenProviderFunction);
var newRoot = cache.GetOrAdd(instanceUrl,tempRoot);
if(newRoot!=tempRoot)
{
tempRoot.Dispose();
}
return newRoot.Clone();
}
}
I am really appreciate if you could give me some best practices in our scenario. |
Beta Was this translation helpful? Give feedback.
-
Moving this to discussions => Q/A as that's more appropriate for this. Clone is effective in creating a new independent connection to the API endpoints that the host client (the parent of the clone) is already connected to. It useful in WebApplications and SDK Services where you are dealing with variable load, but not high or extreme load, where you want to provide quick and efficient calls to the server. If you expect high load on your service, you should in turn use connection pooling to allow for a controlled growth of connections to Dataverse to provide you a means to control API utilization to manage costs. What high / extreme load means to you has a lot to do with your instance and scaling. for the 95% population Clone is just fine and any throttling is handled internally. Clone is not appropriate for Multi environment scenarios, as it's bound to the host connections environment. If you have to work with multiple environments, where there are repeated calls to the web service or api endpoint over a short period of time, you will want to use a variation of connection caching, where you keep a connection for each environment in local cache for a short period of time ( 5 min or so with a sliding refresh ). In answer to your questions:
|
Beta Was this translation helpful? Give feedback.
-
Create a ServiceClient is expensive, is it thread-safe? so we can reuse the same client for multi thread
Beta Was this translation helpful? Give feedback.
All reactions