예제 #1
0
        /// <summary>
        /// Connects to the Dataverse API for the given SQL connection
        /// </summary>
        /// <returns></returns>
        protected CrmServiceClient ConnectCDS(SqlConnectionStringBuilder conStr)
        {
            if (conStr == null)
            {
                return(null);
            }

            var server = conStr.DataSource.Split(',')[0];

            if (_clientCache.TryGetValue(server, out var con))
            {
                return(con.Clone());
            }

            var resource = $"https://{server}/";
            var req      = WebRequest.CreateHttp(resource);

            req.AllowAutoRedirect = false;
            var resp      = req.GetResponse();
            var authority = new UriBuilder(resp.Headers[HttpResponseHeader.Location]);

            authority.Query = "";
            authority.Port  = -1;
            var authParams = new AuthParams(conStr.Authentication, server, conStr.InitialCatalog, resource, authority.ToString(), conStr.UserID, "", Guid.Empty);

            AuthOverrideHook.Instance.AddAuth(authParams);

            con = new CrmServiceClient(new Uri(resource), true);
            _clientCache[server] = con;

            return(con.Clone());
        }
예제 #2
0
        /// <summary>
        /// Get CrmServiceClient connection for currently signed in user.
        /// </summary>
        /// <returns></returns>
        public CrmServiceClient GetCdsConnectionClient()
        {
            string           TargetOrgUri = ConfigurationManager.AppSettings["ResourceUri"];
            CrmServiceClient cdsSvcClient = (CrmServiceClient)HttpRuntime.Cache[OnBehalfAuthManager.CrmSvcClientCacheKey];

            if (cdsSvcClient == null)
            {
                // Set up new client
                CrmServiceClient.AuthOverrideHook = new OnBehalfCdsSvcClientAuthHandler();
                _svcClient = new CrmServiceClient(instanceUrl: new Uri(TargetOrgUri), useUniqueInstance: true);
                HttpRuntime.Cache.Add(OnBehalfAuthManager.CrmSvcClientCacheKey, _svcClient, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 20, 0), CacheItemPriority.High, null);

                cdsSvcClient = _svcClient;
            }

            var userMap = (Dictionary <Guid, Guid>)HttpRuntime.Cache["CDSUserMap"];

            if (userMap == null)
            {
                userMap = new Dictionary <Guid, Guid>();
                HttpRuntime.Cache.Add("CDSUserMap", userMap, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 20, 0), CacheItemPriority.High, null);
            }

            CrmServiceClient outClient = cdsSvcClient.Clone();

            return(outClient);
        }
예제 #3
0
        /// <summary>
        /// Deletes a list of entity references
        /// </summary>
        /// <param name="svc">The CrmServiceClient instance to use</param>
        /// <param name="entityReferences">A List of entity references to delete.</param>
        private static void DeleteEntities(CrmServiceClient svc, List <EntityReference> entityReferences)
        {
            Parallel.ForEach(entityReferences,
                             new ParallelOptions()
            {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
            },
                             () =>
            {
                //Clone the CrmServiceClient for each thread
                return(svc.Clone());
            },
                             (er, loopState, index, threadLocalSvc) =>
            {
                // In each thread, delete the entities
                threadLocalSvc.Delete(er.LogicalName, er.Id);

                return(threadLocalSvc);
            },
                             (threadLocalSvc) =>
            {
                //Dispose the cloned CrmServiceClient instance
                if (threadLocalSvc != null)
                {
                    threadLocalSvc.Dispose();
                }
            });
        }
예제 #4
0
        public virtual IOrganizationService CreateService()
        {
            if (connectionParams == null)
            {
                throw new NotSupportedException("Creating a CRM connection is unsupported in this instance.");
            }

            IOrganizationService service = null;

            var timeout = connectionParams.Timeout;

            if (timeout != null)
            {
                CrmServiceClient.MaxConnectionTimeout = timeout.Value;
            }

            if (serviceCloneBase == null)
            {
                service          = customServiceFactory(connectionParams.ConnectionString);
                serviceCloneBase = service as CrmServiceClient;
            }

            if (serviceCloneBase != null)
            {
                service = serviceCloneBase.Clone();
            }

            if (service.EnsureTokenValid((int)(tokenExpiryCheck ?? TimeSpan.Zero).TotalSeconds) == false)
            {
                service = null;
            }

            service ??= customServiceFactory(connectionParams.ConnectionString);

            if (timeout != null)
            {
                if (service is OrganizationServiceProxy proxyService)
                {
                    proxyService.Timeout = timeout.Value;
                }

                if (service is OrganizationWebProxyClient webProxyService)
                {
                    webProxyService.InnerChannel.OperationTimeout = timeout.Value;
                }
            }

            return(service);
        }
예제 #5
0
        public IOrganizationService CreateCrmService()
        {
            IOrganizationService service = null;

            if (serviceCloneBase == null)
            {
                service          = customServiceFactory(Parameters.ConnectionParams.ConnectionString);
                serviceCloneBase = service as CrmServiceClient;
            }

            if (serviceCloneBase != null)
            {
                service = serviceCloneBase.Clone();
            }

            if (service.EnsureTokenValid(Parameters.PoolParams?.TokenExpiryCheckSecs ?? 0) == false)
            {
                service = null;
            }

            return(service ?? customServiceFactory(Parameters.ConnectionParams.ConnectionString));
        }
예제 #6
0
        /// <summary>
        /// Creates entities in parallel
        /// </summary>
        /// <param name="svc">The CrmServiceClient instance to use</param>
        /// <param name="entities">A List of entities to create.</param>
        /// <returns></returns>
        private static ConcurrentBag <EntityReference> CreateEntities(CrmServiceClient svc, List <Entity> entities)
        {
            var createdEntityReferences = new ConcurrentBag <EntityReference>();

            Parallel.ForEach(entities,
                             new ParallelOptions()
            {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
            },
                             () =>
            {
                //Clone the CrmServiceClient for each thread
                return(svc.Clone());
            },
                             (entity, loopState, index, threadLocalSvc) =>
            {
                // In each thread, create entities and add them to the ConcurrentBag
                // as EntityReferences
                createdEntityReferences.Add(
                    new EntityReference(
                        entity.LogicalName,
                        threadLocalSvc.Create(entity)
                        )
                    );

                return(threadLocalSvc);
            },
                             (threadLocalSvc) =>
            {
                //Dispose the cloned CrmServiceClient instance
                if (threadLocalSvc != null)
                {
                    threadLocalSvc.Dispose();
                }
            });

            //Return the ConcurrentBag of EntityReferences
            return(createdEntityReferences);
        }
예제 #7
0
        private static void ImportData(string connectionString, string schemaPath)
        {
            Console.WriteLine("Import Started");

            using (var tokenSource = new CancellationTokenSource())
            {
                using (var serviceClient = new CrmServiceClient(connectionString))
                {
                    var entityRepo  = new EntityRepository(serviceClient, new ServiceRetryExecutor());
                    int threadCount = Settings.Default.ThreadCount;
                    var logger      = new ConsoleLogger();

                    if (threadCount > 1)
                    {
                        List <IEntityRepository> entRepos = new List <IEntityRepository>()
                        {
                            entityRepo
                        };
                        while (threadCount > 1)
                        {
                            threadCount--;
                            var newServiceClient = serviceClient.Clone();
                            if (newServiceClient != null)
                            {
                                entRepos.Add(new EntityRepository(serviceClient.Clone(), new ServiceRetryExecutor()));
                            }
                            else
                            {
                                Console.WriteLine("CrmServiceClient Clone() operation is only supported for OAUTH connections, single thread will be used as clone returned null");
                            }
                        }

                        if (!Settings.Default.UseCsvImport)
                        {
                            // Json Import
                            var fileImporterJson = new CrmFileDataImporter(logger, entRepos, GetImportConfig(), tokenSource.Token);
                            fileImporterJson.MigrateData();
                        }
                        else
                        {
                            // Csv Import
                            var schema          = CrmSchemaConfiguration.ReadFromFile(schemaPath);
                            var fileImporterCsv = new CrmFileDataImporterCsv(logger, entRepos, GetImportConfig(), schema, tokenSource.Token);
                            fileImporterCsv.MigrateData();
                        }
                    }
                    else
                    {
                        if (!Settings.Default.UseCsvImport)
                        {
                            // Json Import
                            var fileImporterJson = new CrmFileDataImporter(logger, entityRepo, GetImportConfig(), tokenSource.Token);
                            fileImporterJson.MigrateData();
                        }
                        else
                        {
                            // Csv Import
                            var schema          = CrmSchemaConfiguration.ReadFromFile(schemaPath);
                            var fileImporterCsv = new CrmFileDataImporterCsv(logger, entityRepo, GetImportConfig(), schema, tokenSource.Token);
                            fileImporterCsv.MigrateData();
                        }
                    }
                }
            }

            Console.WriteLine("Import Finished");
        }