Exemplo n.º 1
0
        public async Task ClearDatabaseTest()
        {
            Func <Task <int> > getDbDepCount = async() => (await SqlDependencyEx.GetDependencyDbIdentities(TEST_CONNECTION_STRING, TEST_DATABASE_NAME)).Length;

            var dep1 = new SqlDependencyEx(
                TEST_CONNECTION_STRING,
                TEST_DATABASE_NAME,
                TEST_TABLE_NAME,
                "temp",
                identity: 4);
            var dep2 = new SqlDependencyEx(
                TEST_CONNECTION_STRING,
                TEST_DATABASE_NAME,
                TEST_TABLE_NAME,
                "temp",
                identity: 5);

            dep1.Start();
            // Make sure db has been got 1 dependency object.
            Assert.AreEqual(1, getDbDepCount());
            dep2.Start();
            // Make sure db has been got 2 dependency object.
            Assert.AreEqual(2, getDbDepCount());

            // Forced db cleaning
            await SqlDependencyEx.CleanDatabase(TEST_CONNECTION_STRING, TEST_DATABASE_NAME);

            // Make sure db has no any dependency objects.
            Assert.AreEqual(0, getDbDepCount());

            dep1.Dispose();
            dep2.Dispose();
        }
Exemplo n.º 2
0
        public Task Start()
        {
            IEntityType[] syncEngineEntityTypes;
            string        databaseName;

            using (var serviceScope = _serviceScopeFactory.CreateScope())
            {
                var dbContext = serviceScope.ServiceProvider.GetRequiredService <TDbContext>();

                syncEngineEntityTypes = dbContext.Model.GetEntityTypes().Where(EntityTypeExtensions.IsSyncEngineEnabled).ToArray();

                databaseName = dbContext.Database.GetDbConnection().Database;
            }

            SqlDependencyEx.CleanDatabase(_configuration.GetConnectionString("LegacyConnection"), databaseName);

            _logger.LogInformation("Found {entityTrackingCount} Entities with Sync Engine enabled", syncEngineEntityTypes.Length);

            foreach (var entityType in syncEngineEntityTypes)
            {
                var tableName = entityType.GetTableName();

                Interlocked.Increment(ref _SqlDependencyIdentity);

                var sqlTableDependency = new SqlDependencyEx(_configuration.GetConnectionString("LegacyConnection"), databaseName, tableName, identity: _SqlDependencyIdentity);

                _logger.LogDebug("Create SqlDependency on table {tableName} with identity: {sqlDependencyId}", tableName, _SqlDependencyIdentity);

                sqlTableDependency.TableChanged += async(object sender, SqlDependencyEx.TableChangedEventArgs e) => await ProcessChangeEvent(sender);

                _sqlTableDependencies.Add(sqlTableDependency);

                _logger.LogInformation("Sync Engine configured for Entity: {entityTypeName}", entityType.Name);
            }

            _sqlTableDependencies.ForEach(s => s.Start());

            return(Task.CompletedTask);
        }
Exemplo n.º 3
0
        public void ClearDatabaseTest()
        {
            Func <int> getDbDepCount =
                () =>
                SqlDependencyEx.GetDependencyDbIdentities(
                    TEST_CONNECTION_STRING,
                    TEST_DATABASE_NAME).Length;

            var options = new SqlDependencyExOptions
            {
                ConnectionString = TEST_CONNECTION_STRING,
                DatabaseName     = TEST_DATABASE_NAME,
                TableName        = TEST_TABLE_NAME,
                SchemaName       = "temp"
            };

            var dep1 = new SqlDependencyEx(
                options,
                identity: 4);
            var dep2 = new SqlDependencyEx(
                options,
                identity: 5);

            dep1.Start();
            // Make sure db has been got 1 dependency object.
            Assert.AreEqual(1, getDbDepCount());
            dep2.Start();
            // Make sure db has been got 2 dependency object.
            Assert.AreEqual(2, getDbDepCount());

            // Forced db cleaning
            SqlDependencyEx.CleanDatabase(TEST_CONNECTION_STRING, TEST_DATABASE_NAME);

            // Make sure db has no any dependency objects.
            Assert.AreEqual(0, getDbDepCount());

            dep1.Dispose();
            dep2.Dispose();
        }