Пример #1
0
        private static object CreateBody(SendInput input, SendOptions options)
        {
            if (input.Data == null)
            {
                return(null);
            }

            var contentTypeString = options.ContentType;

            var encoding = GetEncodingFromContentType(contentTypeString, Encoding.UTF8);


            switch (options.BodySerializationType)
            {
            case BodySerializationType.String:
                return(input.Data);

            case BodySerializationType.ByteArray:
                return(encoding.GetBytes(input.Data));

            case BodySerializationType.Stream:
            default:
                var stream = new MemoryStream(encoding.GetBytes(input.Data))
                {
                    Position = 0
                };
                return(stream);
            }
        }
Пример #2
0
        /// <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();

                var body = CreateBody(input, options);

                var bodyStream = body as Stream;
                using (var message = bodyStream != null ? new BrokeredMessage(bodyStream, true) : new BrokeredMessage(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.Properties.Add(property.Name, property.Value);
                    }

                    cancellationToken.ThrowIfCancellationRequested();

                    await client.SendAsync(message).ConfigureAwait(false);

                    return result;
                }
            }).ConfigureAwait(false));
        }