Esempio n. 1
0
        public OutboxTestsConfiguration(Func <Type, string> collectionNamingConvention, TimeSpan?transactionTimeout = null)
        {
            DatabaseName = "Test_" + DateTime.UtcNow.Ticks.ToString(CultureInfo.InvariantCulture);
            CollectionNamingConvention = collectionNamingConvention;

            SynchronizedStorage = new StorageSessionFactory(ClientProvider.Client, true, DatabaseName, collectionNamingConvention, transactionTimeout ?? MongoPersistence.DefaultTransactionTimeout);
            transactionFactory  = new MongoOutboxTransactionFactory(ClientProvider.Client, DatabaseName, CollectionNamingConvention, transactionTimeout ?? MongoPersistence.DefaultTransactionTimeout);
        }
Esempio n. 2
0
        public async Task Configure(CancellationToken cancellationToken = default)
        {
            var containerConnectionString = Environment.GetEnvironmentVariable("NServiceBusStorageMongoDB_ConnectionString");

            client = string.IsNullOrWhiteSpace(containerConnectionString) ? new MongoClient() : new MongoClient(containerConnectionString);

            Storage.MongoDB.SagaStorage.InitializeSagaDataTypes(client, databaseName, MongoPersistence.DefaultCollectionNamingConvention, SagaMetadataCollection);
            SagaStorage         = new SagaPersister(SagaPersister.DefaultVersionElementName);
            SynchronizedStorage = new StorageSessionFactory(client, true, databaseName, MongoPersistence.DefaultCollectionNamingConvention, SessionTimeout ?? MongoPersistence.DefaultTransactionTimeout);

            var databaseSettings = new MongoDatabaseSettings
            {
                ReadConcern    = ReadConcern.Majority,
                ReadPreference = ReadPreference.Primary,
                WriteConcern   = WriteConcern.WMajority
            };
            var database = client.GetDatabase(databaseName, databaseSettings);
            await database.CreateCollectionAsync(MongoPersistence.DefaultCollectionNamingConvention(typeof(OutboxRecord)), cancellationToken : cancellationToken);

            OutboxStorage = new OutboxPersister(client, databaseName, MongoPersistence.DefaultCollectionNamingConvention);
        }
        public Task Configure(CancellationToken cancellationToken = default)
        {
            // with this we have a partition key per run which makes things naturally isolated
            partitionKey = Guid.NewGuid().ToString();

            var serializer = new JsonSerializer
            {
                ContractResolver = new UpperCaseIdIntoLowerCaseIdContractResolver(),
                Converters       = { new ReadOnlyMemoryConverter() }
            };

            var persistenceConfiguration = (PersistenceConfiguration)Variant.Values[0];

            var sagaPersistenceConfiguration = new SagaPersistenceConfiguration();

            if (persistenceConfiguration.UsePessimisticLocking)
            {
                var pessimisticLockingConfiguration = sagaPersistenceConfiguration.UsePessimisticLocking();
                if (SessionTimeout.HasValue)
                {
                    pessimisticLockingConfiguration.SetLeaseLockAcquisitionTimeout(SessionTimeout.Value);
                }
                SupportsPessimisticConcurrency = true;
            }

            var partitionKeyPath = new PartitionKeyPath(SetupFixture.PartitionPathKey);
            var resolver         = new ContainerHolderResolver(this, new ContainerInformation(SetupFixture.ContainerName, partitionKeyPath), SetupFixture.DatabaseName);

            SynchronizedStorage = new StorageSessionFactory(resolver, null);
            SagaStorage         = new SagaPersister(serializer, sagaPersistenceConfiguration);
            OutboxStorage       = new OutboxPersister(resolver, serializer, OutboxTimeToLiveInSeconds);

            GetContextBagForSagaStorage = () =>
            {
                var contextBag = new ContextBag();
                // This populates the partition key required to participate in a shared transaction
                var setAsDispatchedHolder = new SetAsDispatchedHolder
                {
                    PartitionKey    = new PartitionKey(partitionKey),
                    ContainerHolder = resolver.ResolveAndSetIfAvailable(contextBag)
                };
                contextBag.Set(setAsDispatchedHolder);
                contextBag.Set(new PartitionKey(partitionKey));
                return(contextBag);
            };

            GetContextBagForOutbox = () =>
            {
                var contextBag = new ContextBag();
                // This populates the partition key required to participate in a shared transaction
                var setAsDispatchedHolder = new SetAsDispatchedHolder
                {
                    PartitionKey    = new PartitionKey(partitionKey),
                    ContainerHolder = resolver.ResolveAndSetIfAvailable(contextBag)
                };
                contextBag.Set(setAsDispatchedHolder);
                contextBag.Set(new PartitionKey(partitionKey));
                return(contextBag);
            };

            return(Task.CompletedTask);
        }