Hello sir i am also facing the same issue i have less then 10k records in 5 of my tables in dataverse. Its around 5-8 secs or even more sometimes more or less in order to establish connection for the first operation after that subsequest operations are faster i.e i get response in less then 2-3 sec. Can u please help me to reduce the connection time with dataverse with my dotnet webapi app as I am using dataverse for first time and mostly worked with sql and mongodb which are way faster in terms of speed. Below I have share the code for my dataverse operations which i have used acrossed my webapi app.
using Microsoft.Extensions.Configuration;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Rest;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
namespace Tm.Services.Comman
{
public static class ConfigurationHelper
{
public static IConfiguration config;
public static void Initialize(IConfiguration Configuration)
{
config = Configuration;
}
}
public class DataverseOperations
{
private static ServiceClient serviceClient;
private static readonly Uri uri;
private static readonly string clientId;
private static readonly string sec_pass;
private static readonly bool useUniqueInstance;
static DataverseOperations()
{
serviceClient = null;
uri = new Uri(ConfigurationHelper.config.GetSection("CdsClient:Uri").Value);
clientId = ConfigurationHelper.config.GetSection("CdsClient:ClientId").Value;
sec_pass = ConfigurationHelper.config.GetSection("CdsClient:ClientSecret").Value;
useUniqueInstance = Convert.ToBoolean(ConfigurationHelper.config.GetSection("CdsClient:UseUniqueInstance").Value);
}
public static void InitializeServiceClient()
{
if (serviceClient == null)
{
SecureString clientSecret = new SecureString();
Array.ForEach(sec_pass.ToArray(), clientSecret.AppendChar);
clientSecret.MakeReadOnly();
string connectionString = $@"AuthType=ClientSecret;SkipDiscovery=true;url={uri};Secret={sec_pass};ClientId={clientId};RequireNewInstance={useUniqueInstance}";
serviceClient = new ServiceClient(connectionString);
}
}
public async static Task<string> UpsertRequest(Entity entityModel, bool isAdd)
{
InitializeServiceClient();
if (serviceClient.IsReady)
{
if (isAdd)
{
var id = await serviceClient.CreateAsync(entityModel);
return id.ToString();
}
else
{
serviceClient.Update(entityModel);
}
}
return string.Empty;
}
public async static Task<EntityCollection> RetrieveMultipleAsync(QueryExpression query)
{
InitializeServiceClient();
EntityCollection entityCollection = new EntityCollection();
if (serviceClient.IsReady)
{
entityCollection = await serviceClient.RetrieveMultipleAsync(query);
}
return entityCollection;
}
public async static Task<Entity> RetrieveAsync(string entityName, Guid id, ColumnSet columnSet)
{
InitializeServiceClient();
Entity entity = new Entity();
if (serviceClient.IsReady)
{
entity = await serviceClient.RetrieveAsync(entityName, id, columnSet);
}
return entity;
}
public static Guid[] CreateRecordsInParallel(List<Entity> entityList)
{
InitializeServiceClient();
ConcurrentBag<Guid> ids = new ConcurrentBag<Guid>();
if (serviceClient.IsReady)
{
Parallel.ForEach(entityList, new ParallelOptions { MaxDegreeOfParallelism = serviceClient.RecommendedDegreesOfParallelism }, entity =>
{
var id = serviceClient.Create(entity);
ids.Add(id);
});
}
return ids.ToArray();
}
public static T GetRecordsInParallel<T>(List<QueryExpression> queryList)
{
Stack results = new Stack();
InitializeServiceClient();
if (serviceClient.IsReady)
{
Parallel.ForEach(queryList,
new ParallelOptions { MaxDegreeOfParallelism = serviceClient.RecommendedDegreesOfParallelism },
query =>
{
var result = serviceClient.RetrieveMultiple(query);
lock (results) // Ensure thread safety when pushing to the stack
{
results.Push(result);
}
});
}
return (T)(object)results;
}
}
}
your inputs and suggestion are really appreciated. Thank you