/// <summary>
        /// Returns whether <see cref="DoNotEnforceBestPractices(NServiceBus.Extensibility.ExtendableOptions)" /> has ben called or
        /// not.
        /// </summary>
        /// <returns><c>true</c> if best practice enforcement has ben disabled, <c>false</c> otherwise.</returns>
        public static bool IgnoredBestPractices(this ExtendableOptions options)
        {
            EnforceBestPracticesOptions bestPracticesOptions;

            options.Context.TryGet(out bestPracticesOptions);
            return(!(bestPracticesOptions?.Enabled ?? true));
        }
        public void ShouldApplyImmediateDispatch(ExtendableOptions options)
        {
            Dispatch.Immediately.Apply(options);

            Assert.IsTrue(options.RequiredImmediateDispatch());
            Assert.IsTrue(Dispatch.Immediately.IsApplied(options));
        }
    public static bool HasValue(this ExtendableOptions options)
    {
        var messageId = options.GetMessageId();

        if (messageId is not null)
        {
            return(true);
        }

        if (options is SendOptions sendOptions)
        {
            if (sendOptions.GetDeliveryDate().HasValue || sendOptions.GetDeliveryDelay().HasValue)
            {
                return(true);
            }
        }

        var headers = options.GetHeaders();

        if (headers.Any())
        {
            return(true);
        }

        var extensions = options.GetExtensions();

        if (extensions is not null)
        {
            return(ContextBagHelper.HasContent(extensions));
        }

        return(false);
    }
        /// <summary>
        /// Allows headers to be set for the outgoing message.
        /// </summary>
        /// <param name="options">The options to extend.</param>
        /// <param name="key">The header key.</param>
        /// <param name="value">The header value.</param>
        public static void SetHeader(this ExtendableOptions options, string key, string value)
        {
            Guard.AgainstNull(nameof(options), options);
            Guard.AgainstNullAndEmpty(nameof(key), key);

            options.OutgoingHeaders[key] = value;
        }
        /// <summary>
        /// Returns whether immediate dispatch has been requested by <see cref="RequireImmediateDispatch" /> or not.
        /// </summary>
        /// <param name="options">The options being extended.</param>
        /// <returns><c>True</c> if immediate dispatch was requested, <c>False</c> otherwise.</returns>
        public static bool RequiredImmediateDispatch(this ExtendableOptions options)
        {
            Guard.AgainstNull(nameof(options), options);

            options.GetExtensions().TryGet(out RoutingToDispatchConnector.State state);

            return(state?.ImmediateDispatch ?? false);
        }
        /// <summary>
        /// Returns whether <see cref="DoNotEnforceBestPractices(NServiceBus.Extensibility.ExtendableOptions)" /> has ben called or
        /// not.
        /// </summary>
        /// <returns><c>true</c> if best practice enforcement has ben disabled, <c>false</c> otherwise.</returns>
        public static bool IgnoredBestPractices(this ExtendableOptions options)
        {
            Guard.AgainstNull(nameof(options), options);
            EnforceBestPracticesOptions bestPracticesOptions;

            options.Context.TryGet(out bestPracticesOptions);
            return(!(bestPracticesOptions?.Enabled ?? true));
        }
Esempio n. 7
0
        public void ShouldAllowCtorUsage(ExtendableOptions options)
        {
            var header = new Header("key", "value");

            header.Apply(options);

            Assert.That(options.GetHeaders(), Contains.Item(new KeyValuePair <string, string>("key", "value")));
        }
        /// <summary>
        /// Requests that the message be dispatched to the transport immediately.
        /// This means that the message is ACKed by the transport as soon as the call to send returns.
        /// The message will not be enlisted in any current receive transaction even if the transport support it.
        /// </summary>
        /// <param name="options">The options being extended.</param>
        public static void RequireImmediateDispatch(this ExtendableOptions options)
        {
            Guard.AgainstNull(nameof(options), options);

            options.GetExtensions().Set(new RoutingToDispatchConnector.State
            {
                ImmediateDispatch = true
            });
        }
Esempio n. 9
0
        static IOutgoingLogicalMessageContext CreateContext(ExtendableOptions options)
        {
            var context = new TestableOutgoingLogicalMessageContext
            {
                Extensions = options.Context
            };

            return(context);
        }
        static IOutgoingLogicalMessageContext CreateContext(ExtendableOptions options)
        {
            var context = new TestableOutgoingLogicalMessageContext
            {
                Extensions = options.Context
            };

            return context;
        }
        public void ShouldApplyMessageId(ExtendableOptions options)
        {
            var messageId = Guid.NewGuid().ToString();

            MessageId.Set(messageId).Apply(options);

            Assert.AreEqual(options.GetMessageId(), messageId);
            Assert.IsTrue(MessageId.Set(messageId).IsApplied(options));
        }
Esempio n. 12
0
    public static Dictionary <string, string> GetCleanedHeaders(this ExtendableOptions options)
    {
        var dictionary = new Dictionary <string, string>();

        foreach (var header in options.GetHeaders())
        {
            var key = header.Key;

            if (key.StartsWith("NServiceBus."))
            {
                key = key[12..];
        internal override bool IsApplied(ExtendableOptions options)
        {
            string headerValue;

            if (options.GetHeaders().TryGetValue(key, out headerValue))
            {
                return(headerValue == value);
            }

            return(false);
        }
Esempio n. 14
0
        public void ShouldBeVerifyable(ExtendableOptions options)
        {
            var header1 = Header.Add("key1", "value1");
            var header2 = Header.Add("key2", "value2");

            // only apply header1
            header1.Apply(options);

            Assert.IsTrue(header1.IsApplied(options));
            Assert.IsFalse(header2.IsApplied(options));
        }
Esempio n. 15
0
        /// <summary>
        /// Allows customization of the outgoing native message sent using <see cref="IMessageSession"/>.
        /// </summary>
        /// <param name="options">Option being extended.</param>
        /// <param name="customization">Customization action.</param>
        public static void CustomizeNativeMessage(this ExtendableOptions options, Action <ServiceBusMessage> customization)
        {
            var extensions = options.GetExtensions();

            if (extensions.TryGet <Action <ServiceBusMessage> >(NativeMessageCustomizationBehavior.CustomizationKey, out _))
            {
                throw new InvalidOperationException("Native outgoing message has already been customized. Do not apply native outgoing message customization more than once per message.");
            }

            extensions.Set(NativeMessageCustomizationBehavior.CustomizationKey, customization);
        }
        static IOutgoingAttachments GetAttachments(this ExtendableOptions options)
        {
            var contextBag = options.GetExtensions();

            if (contextBag.TryGet <IOutgoingAttachments>(out var attachments))
            {
                return(attachments);
            }

            attachments = new OutgoingAttachments();
            contextBag.Set(attachments);
            return(attachments);
        }
Esempio n. 17
0
        public void ShouldAddMultipleHeaders(ExtendableOptions options)
        {
            var header1 = Header.Add("key1", "value1");
            var header2 = Header.Add("key2", "value2");
            var header3 = Header.Add("key3", "value3");

            header1.Apply(options);
            header2.Apply(options);
            header3.Apply(options);

            Assert.That(options.GetHeaders(), Contains.Item(new KeyValuePair <string, string>("key1", "value1")));
            Assert.That(options.GetHeaders(), Contains.Item(new KeyValuePair <string, string>("key2", "value2")));
            Assert.That(options.GetHeaders(), Contains.Item(new KeyValuePair <string, string>("key3", "value3")));
        }
        async Task CaptureMessages(Func <IMessageSession, Task> operation, ExtendableOptions options)
        {
            var pendingOperations = new PendingTransportOperations();

            options.GetExtensions().Set(pendingOperations);

            await operation(rootSession).ConfigureAwait(false);

            var messageRecords  = pendingOperations.Operations.Select(o => o.ToMessageRecord(requestId, TransactionContext.AttemptId)).Cast <SideEffectRecord>().ToList();
            var messagesToCheck = pendingOperations.Operations.Select(o => o.ToCheck()).ToArray();

            await TransactionContext.AddSideEffects(messageRecords).ConfigureAwait(false);

            await messageStore.Create(messagesToCheck).ConfigureAwait(false);
        }
Esempio n. 19
0
        static IOutgoingAttachments GetAttachments(this ExtendableOptions options)
        {
            Guard.AgainstNull(options, nameof(options));
            var contextBag = options.GetExtensions();

            // check the context for a IOutgoingAttachments in case a mocked instance is injected for testing
            if (contextBag.TryGet <IOutgoingAttachments>(out var attachments))
            {
                return(attachments);
            }

            attachments = new OutgoingAttachments();
            contextBag.Set(attachments);
            return(attachments);
        }
        public static void RegisterCancellationToken(this ExtendableOptions options, CancellationToken cancellationToken)
        {
            var extensions = options.GetExtensions();

            RequestResponseStateLookup.State state;
            if (extensions.TryGet(out state))
            {
                state.CancellationToken = cancellationToken;
            }
            else
            {
                state = new RequestResponseStateLookup.State
                {
                    CancellationToken = cancellationToken
                };
            }
            extensions.Set(state);
        }
Esempio n. 21
0
        static RequestResponseStateLookup.State RegisterTokenSource(this ExtendableOptions options, TaskCompletionSourceAdapter adapter)
        {
            var extensions = options.GetExtensions();

            RequestResponseStateLookup.State state;
            if (extensions.TryGet(out state))
            {
                state.TaskCompletionSource = adapter;
            }
            else
            {
                state = new RequestResponseStateLookup.State
                {
                    TaskCompletionSource = adapter,
                    CancellationToken    = CancellationToken.None
                };
            }
            extensions.Set(state);
            return(state);
        }
    public static Dictionary <string, string> GetCleanedHeaders(this ExtendableOptions options)
    {
        var dictionary = new Dictionary <string, string>();

        foreach (var header in options.GetHeaders())
        {
            var key = header.Key;
            if (key.StartsWith("NServiceBus."))
            {
                key = key.Substring(12);
            }
            if (header.Key == Headers.SagaType)
            {
                dictionary.Add(key, Type.GetType(header.Value, throwOnError: true).FullName);
                continue;
            }

            dictionary.Add(key, header.Value);
        }

        return(dictionary);
    }
Esempio n. 23
0
        /// <summary>
        /// Allows the user to set the message id.
        /// </summary>
        /// <param name="options">Options to extend.</param>
        /// <param name="messageId">The message id to use.</param>
        public static void SetMessageId(this ExtendableOptions options, string messageId)
        {
            Guard.AgainstNullAndEmpty(messageId, messageId);

            options.UserDefinedMessageId = messageId;
        }
Esempio n. 24
0
 void SetTimeoutHeaders(ExtendableOptions options)
 {
     options.SetHeader(Headers.SagaId, Entity.Id.ToString());
     options.SetHeader(Headers.IsSagaTimeoutMessage, bool.TrueString);
     options.SetHeader(Headers.SagaType, GetType().AssemblyQualifiedName);
 }
Esempio n. 25
0
 internal override void Apply(ExtendableOptions options)
 {
     options.SetMessageId(messageId);
 }
Esempio n. 26
0
 internal override bool IsApplied(ExtendableOptions options)
 {
     return(options.GetMessageId() == messageId);
 }
Esempio n. 27
0
 internal abstract bool IsApplied(ExtendableOptions options);
Esempio n. 28
0
 internal abstract void Apply(ExtendableOptions options);
 internal override void Apply(ExtendableOptions options)
 {
     options.SetHeader(key, value);
 }
Esempio n. 30
0
 /// <summary>
 /// Returns the configured message id.
 /// </summary>
 /// <param name="options">Options to extend.</param>
 /// <returns>The message id if configured or <c>null</c>.</returns>
 public static string GetMessageId(this ExtendableOptions options)
 {
     Guard.AgainstNull(nameof(options), options);
     return(options.UserDefinedMessageId);
 }
 public void WhenNotApplied(ExtendableOptions options)
 {
     Assert.IsFalse(MessageId.Set(Guid.NewGuid().ToString()).IsApplied(options));
 }