public static void PostDocumentExcel( [ServiceBusTrigger("q.planbutlerupdateexcel", Connection = "butlerSend")] Microsoft.Azure.ServiceBus.Message messageHeader, [Blob("{Label}", FileAccess.ReadWrite, Connection = "StorageSend")] out byte[] payload, ILogger log) { payload = messageHeader.Body; }
public async Task SendWithDelayAsync(Message message, int delayMilliseconds = 0) { s_logger.LogDebug("Preparing to send message on topic {Topic}", message.Header.Topic); EnsureTopicExists(message.Header.Topic); ITopicClient topicClient; try { RetryPolicy policy = Policy .Handle <Exception>() .Retry(TopicConnectionRetryCount, (exception, retryNumber) => { s_logger.LogError(exception, "Failed to connect to topic {Topic}, retrying...", message.Header.Topic); Thread.Sleep(TimeSpan.FromMilliseconds(TopicConnectionSleepBetweenRetriesInMilliseconds)); } ); topicClient = policy.Execute(() => _topicClientProvider.Get(message.Header.Topic)); } catch (Exception e) { s_logger.LogError(e, "Failed to connect to topic {Topic}, aborting.", message.Header.Topic); throw; } try { s_logger.LogDebug( "Publishing message to topic {Topic} with a delay of {Delay} and body {Request} and id {Id}.", message.Header.Topic, delayMilliseconds, message.Body.Value, message.Id); var azureServiceBusMessage = new Microsoft.Azure.ServiceBus.Message(message.Body.Bytes); azureServiceBusMessage.UserProperties.Add("MessageType", message.Header.MessageType.ToString()); azureServiceBusMessage.UserProperties.Add("HandledCount", message.Header.HandledCount); if (delayMilliseconds == 0) { await topicClient.SendAsync(azureServiceBusMessage); } else { var dateTimeOffset = new DateTimeOffset(DateTime.UtcNow.AddMilliseconds(delayMilliseconds)); await topicClient.ScheduleMessageAsync(azureServiceBusMessage, dateTimeOffset); } s_logger.LogDebug( "Published message to topic {Topic} with a delay of {Delay} and body {Request} and id {Id}", message.Header.Topic, delayMilliseconds, message.Body.Value, message.Id); ; } catch (Exception e) { s_logger.LogError(e, "Failed to publish message to topic {Topic} with id {Id}, message will not be retried.", message.Header.Topic, message.Id); } finally { await topicClient.CloseAsync(); } }
public static async void PostDocumentSalary( [ServiceBusTrigger("q.planbutlerupdatesalary", Connection = "butlerSend")] Microsoft.Azure.ServiceBus.Message messageHeader, [Blob("{Label}", FileAccess.ReadWrite, Connection = "StorageSend")] CloudBlockBlob blob, ILogger log) { string payload = Encoding.Default.GetString(messageHeader.Body); SalaryDeduction orderBlob = new SalaryDeduction(); orderBlob.Order = new List <Order>(); orderBlob = JsonConvert.DeserializeObject <SalaryDeduction>(payload); string name = string.Empty; DateTime date = DateTime.Now; foreach (var item in orderBlob.Order) { date = item.Date; break; } var stringday = date.Day.ToString(); var stringMonth = date.Month.ToString(); blob.Metadata.Add("month", stringMonth); blob.Metadata.Add("day", stringday); await blob.UploadTextAsync(payload); await blob.SetMetadataAsync(); }
public static void Run([ServiceBusTrigger( "<your_queue_name>", Connection = "QueueConnectionString")] Microsoft.Azure.ServiceBus.Message message, Int32 deliveryCount, DateTime enqueuedTimeUtc, string messageId, ILogger log) { log.LogInformation($"C# ServiceBus queue trigger function processed messageId: {message.MessageId} delivery count: {deliveryCount}"); var settings = new StorageSettingData( "<your_blob_storage_container_name>", "<your_blob_storage_connection_string>", VeryLargeMessageStrategy.Storage); FunctionMessageReader reader = new FunctionMessageReader(settings); reader.SubScribe(message, (arg) => { //PUT HERE YOUR MESSAGE HANDLING LOGIC log.LogInformation($"function processed object: {arg.ReceivedEventMessage?.ObjectName?? arg.ReceivedEventMessage.Body.Substring(0,10) }"); } ); }
protected static Task <TResult> SendAsync <TQueueProcessor, TResult>(TMessageParam messageParams, string subscription, Func <TResult> onSent, Func <string, TResult> onFailure) where TQueueProcessor : QueueProcessor <TMessageParam> { return(Web.Configuration.Settings.GetString( Security.SessionServer.Configuration.AppSettings.ServiceBusConnectionString, async serviceBusConnectionString => { var message = new Microsoft.Azure.ServiceBus.Message(); EncodeMessageParams(message.UserProperties, messageParams); message.UserProperties[MESSAGE_PROPERTY_KEY_MESSAGE_NAME] = subscription; var sendClient = new Microsoft.Azure.ServiceBus.QueueClient(serviceBusConnectionString, subscription); try { await sendClient.SendAsync(message); return onSent(); } catch (Exception ex) { return onFailure(ex.Message); } finally { await sendClient.CloseAsync(); } }, onFailure.AsAsyncFunc())); }
public AzureServiceBusMessageContext(IBus bus, Microsoft.Azure.ServiceBus.Message serviceBusMessage) { this.bus = bus; DeliveryCount = serviceBusMessage.SystemProperties.DeliveryCount; EnqueueTime = new DateTimeOffset(serviceBusMessage.SystemProperties.EnqueuedTimeUtc); MessageHeaders = serviceBusMessage.UserProperties.ToDictionary((arg) => arg.Key, (arg) => arg.Value as string); }
public async Task EnqueueMessage(string messageAsJson) { var queueClient = await GetQueueClient(); var payload = messageAsJson; var message = new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(payload)); await queueClient.SendAsync(message); }
public void SendWithDelay(Message message, int delayMilliseconds = 0) { _logger.Value.Debug($"Preparing to send message on topic {message.Header.Topic}"); EnsureTopicExists(message.Header.Topic); ITopicClient topicClient; try { RetryPolicy policy = Policy .Handle <Exception>() .Retry(TopicConnectionRetryCount, (exception, retryNumber) => { _logger.Value.ErrorException( $"Failed to connect to topic {message.Header.Topic}, retrying...", exception); Thread.Sleep(TimeSpan.FromMilliseconds(TopicConnectionSleepBetweenRetriesInMilliseconds)); } ); topicClient = policy.Execute(() => _topicClientProvider.Get(message.Header.Topic)); } catch (Exception e) { _logger.Value.ErrorException($"Failed to connect to topic {message.Header.Topic}, aborting.", e); throw; } try { _logger.Value.Debug( $"Publishing message to topic {message.Header.Topic} with a delay of {delayMilliseconds} and body {message.Body.Value} and id {message.Id}."); var azureServiceBusMessage = new Microsoft.Azure.ServiceBus.Message(message.Body.Bytes); azureServiceBusMessage.UserProperties.Add("MessageType", message.Header.MessageType.ToString()); if (delayMilliseconds == 0) { topicClient.Send(azureServiceBusMessage); } else { var dateTimeOffset = new DateTimeOffset(DateTime.UtcNow.AddMilliseconds(delayMilliseconds)); topicClient.ScheduleMessage(azureServiceBusMessage, dateTimeOffset); } _logger.Value.Debug( $"Published message to topic {message.Header.Topic} with a delay of {delayMilliseconds} and body {message.Body.Value} and id {message.Id}"); } catch (Exception e) { _logger.Value.ErrorException($"Failed to publish message to topic {message.Header.Topic} with id {message.Id}, message will not be retried.", e); } finally { topicClient.Close(); } }
// Pack a single brokered message into an MSMQ message. public static Message PackServiceBusMessageIntoMsmqMessage(Microsoft.Azure.ServiceBus.Message serviceBusMessage) { var msmqMessage = new System.Messaging.Message(serviceBusMessage) { Label = serviceBusMessage.Label }; return(msmqMessage); }
public static async void SendNewIdMessage(UserFood user) { var msg = new Microsoft.Azure.ServiceBus.Message() { MessageId = Guid.NewGuid().ToString(), Body = Encoding.ASCII.GetBytes("{\"AreaRestrictionsSet\": false, \"UserId\":" + user.codUserFood + "}") }; _iUMsg.SendMessagesAsync(msg); }
public static async void SendPersistedIdMessage(UserFood user) { var msg = new Microsoft.Azure.ServiceBus.Message() { MessageId = Guid.NewGuid().ToString(), Body = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(user.ToServiceModel())) }; _iUMsg.SendMessagesAsync(msg); }
/// <summary> /// Envia a mensagem com status de processing:true /// </summary> public static async void SendProcessingMessage(string processingGuid) { var msg = new Microsoft.Azure.ServiceBus.Message() { MessageId = Guid.NewGuid().ToString(), Body = Encoding.ASCII.GetBytes("{\"Processing\":true,\"UserId\":}") }; _iUMsg.SendMessagesAsync(msg); }
public async Task <IActionResult> SubmitDoc(string deviceId, string docType, IFormFile doc) { if (doc == null || doc.Length == 0) { return(BadRequest("file not selected")); } var proposedDocType = CognitiveTargetAction.Unidentified; var isValidType = Enum.TryParse <CognitiveTargetAction>(docType, out proposedDocType); if (!isValidType || proposedDocType == CognitiveTargetAction.Unidentified) { return(BadRequest("Invalid document type")); } long size = doc.Length; // full path to file in temp location string docName = "NA"; string docUri = null; if (size > 0) { using (var stream = doc.OpenReadStream()) { var docExtention = doc.FileName.Substring(doc.FileName.LastIndexOf('.')); docName = $"{deviceId}-{DateTime.UtcNow.ToString("ddMMyyHHmmss")}{docExtention}"; docUri = await storageRepository.CreateFileAsync(docName, stream); } } else { return(BadRequest("Submitted file size is 0")); } CognitiveRequest req = new CognitiveRequest { CreatedAt = DateTime.UtcNow, DeviceId = deviceId, FileUrl = docName, Id = Guid.NewGuid().ToString(), IsActive = true, IsDeleted = false, IsProcessed = false, Origin = "CognitiveOrchestrator.API.V1.0.0", Status = "Submitted", TakenAt = DateTime.UtcNow, TargetAction = proposedDocType.ToString() }; var sbMessage = new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(req))); var result = await serviceBusRepository.PublishMessage(sbMessage); return(Ok(req)); }
public static async void SendPersistedIdMessage(string guid) { var _iUMsg = new UserMessage(FacialService.Configuration); var msg = new Microsoft.Azure.ServiceBus.Message() { MessageId = Guid.NewGuid().ToString(), Body = Encoding.ASCII.GetBytes("Cadastro Persistido: " + guid.ToString()) }; _iUMsg.SendMessagesAsync(msg); }
public static AzureMessage CloneMessage(this AzureMessage original) { return(new AzureMessage { Body = original.Body, Label = original.Label, To = original.To, SessionId = original.SessionId, ContentType = original.ContentType }); }
public static TEntity Deserialize <TEntity>(this Microsoft.Azure.ServiceBus.Message message) where TEntity : Message { var entity = JsonConvert.DeserializeObject <TEntity>(Encoding.UTF8.GetString(message.Body), new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); entity.Id = message.MessageId; entity.Source = message; return(entity); }
public static void Send(string type, OutGoingMessage message) { var busMessage = new Microsoft.Azure.ServiceBus.Message { ContentType = "application/json", Label = type, Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)) }; var messageSender = new MessageSender(SharedConfig.ConnectionStringOrKey, SharedConfig.TopicName); messageSender.SendAsync(busMessage).ConfigureAwait(false).GetAwaiter().GetResult(); }
private Microsoft.Azure.ServiceBus.Message BuildOutputMessage(string messageType, object payload = null) { var outputMessage = new Microsoft.Azure.ServiceBus.Message(); outputMessage.ContentType = messageType; if (payload != null) { outputMessage.Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload)); } return(outputMessage); }
public static Message ToStandardMessage(this Microsoft.Azure.ServiceBus.Message sbMessage) { if (sbMessage == null) { throw new ArgumentNullException(nameof(sbMessage)); } return(new Message { Id = sbMessage.MessageId, Content = Encoding.UTF8.GetString(sbMessage.Body), Attributes = sbMessage.UserProperties }); }
public void When_the_topic_does_not_exist_it_should_be_created_and_the_message_is_sent_to_the_correct_topicclient() { Microsoft.Azure.ServiceBus.Message sentMessage = null; var messageBody = Encoding.UTF8.GetBytes("A message body"); _nameSpaceManagerWrapper.Setup(t => t.TopicExists("topic")).Returns(false); _topicClientProvider.Setup(f => f.Get("topic")).Returns(_topicClient.Object); _topicClient.Setup(f => f.Send(It.IsAny <Microsoft.Azure.ServiceBus.Message>())).Callback((Microsoft.Azure.ServiceBus.Message g) => sentMessage = g); _producer.Send(new Message(new MessageHeader(Guid.NewGuid(), "topic", MessageType.MT_NONE), new MessageBody(messageBody, "JSON"))); _nameSpaceManagerWrapper.Verify(x => x.CreateTopic("topic"), Times.Once); Assert.Equal(messageBody, sentMessage.Body); }
public void When_the_topic_exists_and_sending_a_message_with_a_delay_it_should_send_the_message_to_the_correct_topicclient() { Microsoft.Azure.ServiceBus.Message sentMessage = null; var messageBody = Encoding.UTF8.GetBytes("somebody"); _nameSpaceManagerWrapper.Setup(t => t.TopicExists("topic")).Returns(true); _topicClientProvider.Setup(f => f.Get("topic")).Returns(_topicClient.Object); _topicClient.Setup(f => f.ScheduleMessage(It.IsAny <Microsoft.Azure.ServiceBus.Message>(), It.IsAny <DateTimeOffset>())).Callback((Microsoft.Azure.ServiceBus.Message g, DateTimeOffset d) => sentMessage = g); _producer.SendWithDelay(new Message(new MessageHeader(Guid.NewGuid(), "topic", MessageType.MT_NONE), new MessageBody(messageBody, "JSON")), 1); Assert.That(sentMessage.Body, Is.EqualTo(messageBody)); _topicClient.Verify(x => x.Close(), Times.Once); }
public void When_sending_a_command_message_type_message_with_no_delay_it_should_set_the_correct_messagetype_property() { Microsoft.Azure.ServiceBus.Message sentMessage = null; var messageBody = Encoding.UTF8.GetBytes("A message body"); _nameSpaceManagerWrapper.Setup(t => t.TopicExists("topic")).Returns(true); _topicClientProvider.Setup(f => f.Get("topic")).Returns(_topicClient.Object); _topicClient.Setup(f => f.Send(It.IsAny <Microsoft.Azure.ServiceBus.Message>())).Callback((Microsoft.Azure.ServiceBus.Message g) => sentMessage = g); _producer.Send(new Message(new MessageHeader(Guid.NewGuid(), "topic", MessageType.MT_COMMAND), new MessageBody(messageBody, "JSON"))); Assert.Equal(messageBody, sentMessage.Body); Assert.Equal("MT_COMMAND", sentMessage.UserProperties["MessageType"]); _topicClient.Verify(x => x.Close(), Times.Once); }
public static string GetBodyString(this Microsoft.Azure.ServiceBus.Message message) { var strMessage = Encoding.UTF8.GetString(message.Body); //Windows Azure Client is not wire-compatible with .NET Core client //we check if the message comes from Windows client and cut off //64 header chars and 2 footer chars //see https://github.com/Azure/azure-service-bus-dotnet/issues/239 if (strMessage.StartsWith("@\u0006string", StringComparison.Ordinal)) { strMessage = strMessage.Substring(64, strMessage.Length - 66); } return(strMessage); }
public async Task AddToQueue(IMetadataQueueItem queueItem) { try { var queueClient = _serviceBusQueueClientFactory.GetQueueClientFor(_options.CurrentValue.MetadataQueue).Result; _logger.LogInformation("Queued form for further processing {0}", queueItem.Reference); var payload = JsonConvert.SerializeObject(queueItem); var message = new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(payload)); await queueClient.SendAsync(message); } catch (System.Exception e) { _logger.LogError(e, "Error occurred while communicating with Azure Service Bus"); throw; } }
public void QueueFormForProcessing(QueueMessage queueMessage) { try { var queueClient = _serviceBusQueueClientFactory.GetQueueClientFor(QueueName).Result; _logger.LogInformation("Queued form for further processing {0}", queueMessage.Reference); var payload = JsonConvert.SerializeObject(queueMessage); var message = new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(payload)); queueClient.SendAsync(message); } catch (System.Exception e) { _logger.LogError(e, "Error occurred while communicating with Azure Service Bus"); throw; } }
private async Task ProcessAsync(Microsoft.Azure.ServiceBus.Message message, Microsoft.Azure.ServiceBus.QueueClient client) //private async Task Execute(BrokeredMessage receivedMessage) { try { // Process the message var messageParams = ParseMessageParams(message.UserProperties); var messageAction = await ProcessMessageAsync <MessageProcessStatus>(messageParams, async() => { //message.Complete(); await client.CompleteAsync(message.SystemProperties.LockToken); return(MessageProcessStatus.Complete); }, async() => { // TODO: Update resent count and send error if it gets too large // Let message get resent await client.ScheduleMessageAsync(message, DateTimeOffset.UtcNow + TimeSpan.FromSeconds(10)); return(MessageProcessStatus.ReprocessImmediately); }, async (whyReturned) => { await client.DeadLetterAsync(message.SystemProperties.LockToken, whyReturned); return(MessageProcessStatus.Broken); }); } catch (Exception ex) { //var telemetryData = (new System.Collections.Generic.Dictionary<string, string> // { // { "receivedMessage.MessageId", receivedMessage.MessageId }, // { "receivedMessage.SessionId", receivedMessage.SessionId }, // { "receivedMessage.ContentType", receivedMessage.ContentType }, // { "receivedMessage.SequenceNumber", receivedMessage.SequenceNumber.ToString() }, // { "exception.Message", ex.Message }, // { "exception.StackTrace", ex.StackTrace }, // }) //.Concat(receivedMessage.Properties // .Select(prop => $"receivedMessage.Properties[{prop.Key}]".PairWithValue( // null == prop.Value ? string.Empty : prop.Value.ToString()))) //.ToDictionary(); //telemetry.TrackException(ex, telemetryData); await client.DeadLetterAsync(message.SystemProperties.LockToken, ex.Message); } }
public MessageTests() { // One-time setup // Only use this setup for testing, do not attempt to set systemproperties in production _servicebusMessage = new Microsoft.Azure.ServiceBus.Message { MessageId = "SomeCoolId1" }; var systemProperties = new Microsoft.Azure.ServiceBus.Message.SystemPropertiesCollection(); var bindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty; var sequenceNumber = 1; var enqueueTime = DateTime.Now.AddSeconds(5); systemProperties.GetType().InvokeMember("EnqueuedTimeUtc", bindings, Type.DefaultBinder, systemProperties, new object[] { enqueueTime }); systemProperties.GetType().InvokeMember("SequenceNumber", bindings, Type.DefaultBinder, systemProperties, new object[] { sequenceNumber }); // Set mocked-up systemprorperties for current message bindings = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty; _servicebusMessage.GetType().InvokeMember("SystemProperties", bindings, Type.DefaultBinder, _servicebusMessage, new object[] { systemProperties }); }
public static Microsoft.Azure.ServiceBus.Message ToAzureServiceBusMessage(this Message message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var sbMessage = new Microsoft.Azure.ServiceBus.Message(Encoding.UTF8.GetBytes(message.Content)); if (string.IsNullOrEmpty(message.Id) == false) { sbMessage.MessageId = message.Id; } foreach (var attributeKey in message.Attributes.Keys) { sbMessage.UserProperties.Add(attributeKey, message.Attributes[attributeKey]); } return(sbMessage); }
public async Task Send(int count = 10) { var sbkey = "Endpoint=sb://jpdallstatesb.servicebus.windows.net/;SharedAccessKeyName=sender;SharedAccessKey=nA/t52ONyg7mWAG7VU93Fe2sxPxC5bPZj2TqiS/XoHE="; var sender = new MessageSender(sbkey, "claims"); var r = new Random(); var types = new List <string>() { "Accident", "Property", "Theft", "Injury" }; for (var i = 0; i < count; i++) { var thing = new { Id = Guid.NewGuid(), ClaimType = types[r.Next(0, types.Count - 1)], CustomerId = Math.Round(DateTime.Now.Millisecond * Math.PI, 0) }; var data = JsonConvert.SerializeObject(thing); var msg = new Microsoft.Azure.ServiceBus.Message(System.Text.Encoding.UTF8.GetBytes(data)); msg.UserProperties.Add("ClaimType", thing.ClaimType); msg.Label = thing.ClaimType; await sender.SendAsync(msg); Console.WriteLine($"Sent message {thing.Id}"); } }
public async Task ProcessMessageAsync <TMessageContext>( MessageReceiver messageReceiver, Microsoft.Azure.ServiceBus.Message message, TMessageContext messageContext, MessageCorrelationInfo correlationInfo, CancellationToken cancellationToken) where TMessageContext : AzureServiceBusMessageContext { IEnumerable <MessageHandler> handlers = MessageHandler.SubtractFrom(_serviceProvider, _logger); if (!handlers.Any()) { throw new InvalidOperationException( $"Message pump cannot correctly process the message in the '{typeof(TMessageContext)}' " + "because no 'IMessageHandler<,>' was registered in the dependency injection container. " + $"Make sure you call the correct '.With...' extension on the {nameof(IServiceCollection)} during the registration of the message pump to register a message handler"); } bool isProcessed = false; string messageBody = Encoding.UTF8.GetString(message.Body); foreach (MessageHandler handler in handlers) { isProcessed = await ProcessMessageAsync(handler, messageReceiver, messageBody, messageContext, correlationInfo, cancellationToken); if (isProcessed) { return; } } var fallbackMessageHandler = _serviceProvider.GetService <IAzureServiceBusFallbackMessageHandler>(); if (fallbackMessageHandler is null) { throw new InvalidOperationException( "Message pump was not able to process the message"); } await fallbackMessageHandler.ProcessMessageAsync(message, messageContext, correlationInfo, cancellationToken); }