/// <summary> /// Scans the dlls and creates a dictionary in which each message in IFunctions is referenced to each function. /// </summary> public HandlersContainer(string serviceName, bool lockSaga = true) { lock (o) { this.serviceName = serviceName; this.lockSaga = SettingsUtil.HasSettings(SETTINGS.LOCKSAGAS)? SettingsUtil.GetSettings <bool>(SETTINGS.LOCKSAGAS):lockSaga; AddDependency <ISerializeMessages, JSONSerializer>(); AddDependency <ISagaLocker, SagaAzureStorageLocker>(); AddDependency <ISendMessages, AzureStorageQueueSendTransport>(SolveDependency <ISerializeMessages>()); AddDependency <IPublishEvents, AzureEventHubPublishTransport>(SolveDependency <ISerializeMessages>()); this.serializer = SolveDependency <ISerializeMessages>(); this.sagaLocker = SolveDependency <ISagaLocker>(); AddDependency <ISagaStoragePersistence, SagaAzureStoragePersistence>(this.sagaLocker as ISagaLocker, this.lockSaga); sagaPersistence = SolveDependency <ISagaStoragePersistence>(); messageSender = SolveDependency <ISendMessages>(); var assemblies = new List <Assembly>(); assemblies.Add(Assembly.GetCallingAssembly()); assemblies.AddRange(Assembly.GetCallingAssembly().GetReferencedAssemblies().Select(a => Assembly.Load(a.FullName))); var types = assemblies.SelectMany(a => a.GetTypes()); LookForSagas(types); LookForHandlers(types); this.sagaLocker.CreateLocksContainer(); sagaPersistence.CreateSagaPersistenceTableAsync().Wait(); } }
public async Task UpdateAsync(SagaData entity) { if (entity.IsDeleted) { return; } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); // Create the table client. CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable table = tableClient.GetTableReference(TABLE_NAME); // Create the TableOperation object that inserts the customer entity. TableOperation replaceOperation = TableOperation.Replace(entity as ITableEntity); // Execute the insert operation. await table.ExecuteAsync(replaceOperation).ConfigureAwait(false); var sagaID = entity.PartitionKey + entity.RowKey; if (this.lockSagas && !entity.IsDeleted) { await sagaLock.ReleaseLock(sagaID, entity.LockID).ConfigureAwait(false); } }
public async Task PublishEventsAsync <T>(T message, string topicName, AFBusMessageContext messageContext) where T : class { var connectionStringBuilder = new EventHubsConnectionStringBuilder(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_EVENTHUB)) { EntityPath = topicName }; var eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); try { var messageAsString = serializer.Serialize(message); var messageWithEnvelope = new AFBusMessageEnvelope() { Context = messageContext, Body = messageAsString }; messageContext.Destination = topicName; var finalMessage = serializer.Serialize(messageWithEnvelope); //if the message is bigger than the limit put the body in the blob storage if ((finalMessage.Length * sizeof(Char)) > MAX_MESSAGE_SIZE) { var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus"; messageWithEnvelope.Context.BodyInFile = true; CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower()); await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); await blockBlob.UploadTextAsync(messageWithEnvelope.Body); messageWithEnvelope.Body = fileName; finalMessage = serializer.Serialize(messageWithEnvelope); } await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(finalMessage))); } finally { await eventHubClient.CloseAsync(); } }
public async Task DeleteFileWithMessageBodyAsync(string fileName) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower()); await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); await blockBlob.DeleteIfExistsAsync(); }
public static async Task <bool> DeleteBlob(string bigPropertyWrapperSerialized) { var jsonSerializer = new JSONSerializer(); var wrapper = jsonSerializer.Deserialize(bigPropertyWrapperSerialized, typeof(BigPropertyWrapper)) as BigPropertyWrapper; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower()); await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(wrapper.FileName); return(await blockBlob.DeleteIfExistsAsync()); }
public static async Task <string> StoreDataInBlob <T>(T property) { var jsonSerializer = new JSONSerializer(); var wrapper = new BigPropertyWrapper(); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus"; // Create a container var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower()); await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); await blockBlob.UploadTextAsync(jsonSerializer.Serialize(property)); wrapper.PropertyType = typeof(T).AssemblyQualifiedName; wrapper.FileName = blockBlob.Name; return(jsonSerializer.Serialize(wrapper)); }
public SagaAzureStorageLocker() { storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); }
public async Task PublishEventsAsync <T>(T message, string topicName, AFBusMessageContext messageContext) where T : class { var sender = new MessageSender(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_SERVICEBUS), topicName.ToLower()); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); var messageAsString = serializer.Serialize(message); var messageWithEnvelope = new AFBusMessageEnvelope() { Context = messageContext, Body = messageAsString }; messageContext.Destination = topicName; TimeSpan?initialVisibilityDelay = null; if (messageContext.MessageDelayedTime != null && messageContext.MessageDelayedTime >= MaxDelay()) { initialVisibilityDelay = MaxDelay(); messageContext.MessageDelayedTime = MaxDelay(); } else if (messageContext.MessageDelayedTime != null) { initialVisibilityDelay = messageContext.MessageDelayedTime; } if (messageContext.MessageDelayedTime != null && initialVisibilityDelay.Value < TimeSpan.Zero) { initialVisibilityDelay = null; messageContext.MessageDelayedTime = null; } var finalMessage = serializer.Serialize(messageWithEnvelope); //if the message is bigger than the limit put the body in the blob storage if ((finalMessage.Length * sizeof(Char)) > MAX_MESSAGE_SIZE) { var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus"; messageWithEnvelope.Context.BodyInFile = true; CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower()); await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); await blockBlob.UploadTextAsync(messageWithEnvelope.Body); messageWithEnvelope.Body = fileName; finalMessage = serializer.Serialize(messageWithEnvelope); } var finalSBMessage = new Message(Encoding.UTF8.GetBytes(finalMessage)) { ContentType = "application/json", Label = topicName, MessageId = messageContext.MessageID.ToString(), TimeToLive = TimeSpan.FromDays(10) }; if (messageContext.MessageDelayedTime.HasValue) { finalSBMessage.ScheduledEnqueueTimeUtc = DateTime.UtcNow + messageContext.MessageDelayedTime.Value; } await sender.SendAsync(finalSBMessage).ConfigureAwait(false); }
public SagaAzureStoragePersistence(ISagaLocker sagaLock, bool lockSagas) { this.sagaLock = sagaLock; this.lockSagas = lockSagas; storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); }
public async Task SendMessageAsync <T>(T message, string serviceName, AFBusMessageContext messageContext) where T : class { serviceName = serviceName.ToLower(); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(SettingsUtil.GetSettings <string>(SETTINGS.AZURE_STORAGE)); CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient(); CloudQueue queue = queueClient.GetQueueReference(serviceName); if (!createdQueues.Contains(serviceName)) { await queue.CreateIfNotExistsAsync(); createdQueues.Add(serviceName); } var messageAsString = serializer.Serialize(message); var messageWithEnvelope = new AFBusMessageEnvelope() { Context = messageContext, Body = messageAsString }; messageContext.Destination = serviceName; TimeSpan?initialVisibilityDelay = null; if (messageContext.MessageDelayedTime != null && messageContext.MessageDelayedTime >= MaxDelay()) { initialVisibilityDelay = MaxDelay(); messageContext.MessageDelayedTime = MaxDelay(); } else if (messageContext.MessageDelayedTime != null) { initialVisibilityDelay = messageContext.MessageDelayedTime; } if (messageContext.MessageDelayedTime != null && initialVisibilityDelay.Value < TimeSpan.Zero) { initialVisibilityDelay = null; messageContext.MessageDelayedTime = null; } var finalMessage = serializer.Serialize(messageWithEnvelope); //if the message is bigger than the limit put the body in the blob storage if ((finalMessage.Length * sizeof(Char)) > MAX_MESSAGE_SIZE) { var fileName = Guid.NewGuid().ToString("N").ToLower() + ".afbus"; messageWithEnvelope.Context.BodyInFile = true; CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Create a container var cloudBlobContainer = cloudBlobClient.GetContainerReference(CONTAINER_NAME.ToLower()); await cloudBlobContainer.CreateIfNotExistsAsync().ConfigureAwait(false); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName); await blockBlob.UploadTextAsync(messageWithEnvelope.Body); messageWithEnvelope.Body = fileName; finalMessage = serializer.Serialize(messageWithEnvelope); } await queue.AddMessageAsync(new CloudQueueMessage(finalMessage), null, initialVisibilityDelay, null, null).ConfigureAwait(false); }