private static byte[] CreateBody(SendInput input, SendOptions options) { if (input.Data == null) { return(null); } var contentTypeString = options.ContentType; var encoding = GetEncodingFromContentType(contentTypeString, Encoding.UTF8); // This format matches the older Frends.ServiceBus format to ensure interoperability switch (options.BodySerializationType) { case BodySerializationType.String: { return(SerializeObject <string>(input.Data)); } case BodySerializationType.ByteArray: { return(SerializeObject <byte[]>(encoding.GetBytes(input.Data))); } case BodySerializationType.Stream: default: return(encoding.GetBytes(input.Data)); } }
/// <summary> /// Send data to the Service Bus, don't wait for a reply. See https://github.com/FrendsPlatform/Frends.ServiceBus /// </summary> /// <returns>Object: {MessageId, SessionId, ContentType}</returns> public static async Task <SendResult> Send([PropertyTab] SendInput input, [PropertyTab] SendOptions options, CancellationToken cancellationToken = new CancellationToken()) { cancellationToken.ThrowIfCancellationRequested(); if (options.CreateQueueOrTopicIfItDoesNotExist) { switch (options.DestinationType) { case QueueOrTopic.Queue: await EnsureQueueExists(input.QueueOrTopicName, input.ConnectionString).ConfigureAwait(false); break; case QueueOrTopic.Topic: await EnsureTopicExists(input.QueueOrTopicName, input.ConnectionString).ConfigureAwait(false); break; default: throw new Exception($"Unexpected destination type: {options.DestinationType}"); } } return(await DoQueueSendOperation(input.ConnectionString, input.QueueOrTopicName, TimeSpan.FromSeconds(options.TimeoutSeconds), options.UseCachedConnection, async client => { var result = new SendResult(); byte[] body = CreateBody(input, options); var message = new Message(body); message.MessageId = string.IsNullOrEmpty(options.MessageId) ? Guid.NewGuid().ToString() : options.MessageId; result.MessageId = message.MessageId; message.SessionId = string.IsNullOrEmpty(options.SessionId) ? message.SessionId : options.SessionId; result.SessionId = message.SessionId; message.ContentType = options.ContentType; result.ContentType = message.ContentType; message.CorrelationId = options.CorrelationId; message.ReplyToSessionId = options.ReplyToSessionId; message.ReplyTo = options.ReplyTo; message.To = options.To; message.TimeToLive = options.TimeToLiveSeconds.HasValue ? TimeSpan.FromSeconds(options.TimeToLiveSeconds.Value) : TimeSpan.MaxValue; message.ScheduledEnqueueTimeUtc = options.ScheduledEnqueueTimeUtc; foreach (var property in input.Properties) { message.UserProperties.Add(property.Name, property.Value); } cancellationToken.ThrowIfCancellationRequested(); await client.SendAsync(message).ConfigureAwait(false); return result; }).ConfigureAwait(false)); }