Exemple #1
0
                protected override Task OnStart(IMessageSession session)
                {
                    var section = settings.GetConfigSection <TestConfigurationSection>();

                    Assert.AreEqual(section.TestSetting, "TestValue");
                    return(TaskEx.CompletedTask);
                }
Exemple #2
0
        public void Initialize(ReadOnlySettings settings, TransportInfrastructure transportInfrastructure, PipelineSettings pipelineSettings)
        {
            var unicastBusConfig        = settings.GetConfigSection <UnicastBusConfig>();
            var conventions             = settings.Get <Conventions>();
            var configuredUnicastRoutes = settings.GetOrDefault <ConfiguredUnicastRoutes>();
            var distributorAddress      = settings.GetOrDefault <string>("LegacyDistributor.Address");

            List <DistributionStrategy> distributionStrategies;

            if (settings.TryGet(out distributionStrategies))
            {
                foreach (var distributionStrategy in distributionStrategies)
                {
                    DistributionPolicy.SetDistributionStrategy(distributionStrategy);
                }
            }

            unicastBusConfig?.MessageEndpointMappings.Apply(Publishers, UnicastRoutingTable, transportInfrastructure.MakeCanonicalForm, conventions);
            configuredUnicastRoutes?.Apply(UnicastRoutingTable, conventions);

            pipelineSettings.Register(b =>
            {
                var router = new UnicastSendRouter(settings.GetOrDefault <string>("BaseInputQueueName"), settings.EndpointName(), settings.InstanceSpecificQueue(), distributorAddress, DistributionPolicy, UnicastRoutingTable, EndpointInstances, i => transportInfrastructure.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i)));
                return(new UnicastSendRouterConnector(router));
            }, "Determines how the message being sent should be routed");

            pipelineSettings.Register(new UnicastReplyRouterConnector(), "Determines how replies should be routed");

            EnforceBestPractices = ShouldEnforceBestPractices(settings);
            if (EnforceBestPractices)
            {
                EnableBestPracticeEnforcement(conventions, pipelineSettings);
            }
        }
Exemple #3
0
        /// <summary>
        /// Finds the configured error queue for an endpoint.
        /// The error queue can be configured in code using 'EndpointConfiguration.SendFailedMessagesTo()',
        /// via the 'Error' attribute of the 'MessageForwardingInCaseOfFaultConfig' configuration section,
        /// or using the 'HKEY_LOCAL_MACHINE\SOFTWARE\ParticularSoftware\ServiceBus\ErrorQueue' registry key.
        /// </summary>
        /// <param name="settings">The configuration settings of this endpoint.</param>
        /// <returns>The configured error queue of the endpoint.</returns>
        /// <exception cref="Exception">When the configuration for the endpoint is invalid.</exception>
        public static string ErrorQueueAddress(this ReadOnlySettings settings)
        {
            string errorQueue;

            if (settings.TryGet("errorQueue", out errorQueue))
            {
                Logger.Debug("Error queue retrieved from code configuration via 'EndpointConfiguration.SendFailedMessagesTo().");
                return(errorQueue);
            }

            var section = settings.GetConfigSection <MessageForwardingInCaseOfFaultConfig>();

            if (section != null)
            {
                if (!string.IsNullOrWhiteSpace(section.ErrorQueue))
                {
                    Logger.Debug("Error queue retrieved from <MessageForwardingInCaseOfFaultConfig> element in config file.");
                    return(section.ErrorQueue);
                }

                throw new Exception(
                          @"'MessageForwardingInCaseOfFaultConfig' configuration section is found but 'ErrorQueue' value is empty.
Take one of the following actions:
- set the error queue at configuration time using 'EndpointConfiguration.SendFailedMessagesTo()'
- Add a valid value to to the app.config. For example:
 <MessageForwardingInCaseOfFaultConfig ErrorQueue=""error""/>");
            }

            var registryErrorQueue = RegistryReader.Read("ErrorQueue");

            if (registryErrorQueue != null)
            {
                if (!string.IsNullOrWhiteSpace(registryErrorQueue))
                {
                    Logger.Debug("Error queue retrieved from registry settings.");
                    return(registryErrorQueue);
                }
                throw new Exception(
                          @"'ErrorQueue' read from registry but the value is empty.
Take one of the following actions:
- set the error queue at configuration time using 'EndpointConfiguration.SendFailedMessagesTo()'
- add a 'MessageForwardingInCaseOfFaultConfig' section to the app.config
- give 'HKEY_LOCAL_MACHINE\SOFTWARE\ParticularSoftware\ServiceBus\ErrorQueue' a valid value for the error queue");
            }

            throw new Exception(
                      @"Faults forwarding requires an error queue to be specified.
Take one of the following actions:
- set the error queue at configuration time using 'EndpointConfiguration.SendFailedMessagesTo()'
- add a 'MessageForwardingInCaseOfFaultConfig' section to the app.config
- configure a global error queue in the registry using the powershell command: Set-NServiceBusLocalMachineSettings -ErrorQueue {address of error queue}");
        }
Exemple #4
0
        static ImmediateConfig GetImmediateRetryConfig(ReadOnlySettings settings, bool transactionsOn)
        {
            if (!transactionsOn)
            {
                Logger.Warn("Immediate Retries will be disabled. Immediate Retries are not supported when running with TransportTransactionMode.None. Failed messages will be moved to the error queue instead.");
                //Transactions must be enabled since Immediate Retries requires the transport to be able to rollback
                return(new ImmediateConfig(0));
            }

            var retriesConfig       = settings.GetConfigSection <TransportConfig>();
            var maxImmediateRetries = retriesConfig?.MaxRetries ?? settings.Get <int>(NumberOfImmediateRetries);

            return(new ImmediateConfig(maxImmediateRetries));
        }
        protected override string GetLocalAddress(ReadOnlySettings settings)
        {
            var configSection = settings.GetConfigSection<AzureServiceBusQueueConfig>();
            if (configSection == null)
            {
                //hack: just to get the defaults, we should refactor this to support specifying the values on the NServiceBus/Transport connection string as well
                configSection = new AzureServiceBusQueueConfig();
            }

            ServiceBusEnvironment.SystemConnectivity.Mode = (ConnectivityMode)Enum.Parse(typeof(ConnectivityMode), configSection.ConnectivityMode);

            var queueName = settings.HasSetting("NServiceBus.LocalAddress") ? settings.Get<string>("NServiceBus.LocalAddress") : settings.EndpointName();
            return NamingConventions.QueueNamingConvention(settings, null, queueName, false);
            
        }
        /// <summary>
        /// Gets the explicitly configured error queue address if one is defined.
        /// The error queue can be configured in code using 'EndpointConfiguration.SendFailedMessagesTo()',
        /// via the 'Error' attribute of the 'MessageForwardingInCaseOfFaultConfig' configuration section,
        /// or using the 'HKEY_LOCAL_MACHINE\SOFTWARE\ParticularSoftware\ServiceBus\ErrorQueue' registry key.
        /// </summary>
        /// <param name="settings">The configuration settings of this endpoint.</param>
        /// <param name="errorQueue">The configured error queue.</param>
        /// <returns>True if an error queue has been explicitly configured.</returns>
        /// <exception cref="Exception">When the configuration for the endpoint is invalid.</exception>
        public static bool TryGetExplicitlyConfiguredErrorQueueAddress(this ReadOnlySettings settings, out string errorQueue)
        {
            Guard.AgainstNull(nameof(settings), settings);
            if (settings.HasExplicitValue(SettingsKey))
            {
                Logger.Debug("Error queue retrieved from code configuration via 'EndpointConfiguration.SendFailedMessagesTo()'.");
                errorQueue = settings.Get <string>(SettingsKey);
                return(true);
            }

            var section = settings.GetConfigSection <MessageForwardingInCaseOfFaultConfig>();

            if (section != null)
            {
                if (!string.IsNullOrWhiteSpace(section.ErrorQueue))
                {
                    Logger.Debug("Error queue retrieved from <MessageForwardingInCaseOfFaultConfig> element in config file.");
                    errorQueue = section.ErrorQueue;
                    return(true);
                }

                var message = "'MessageForwardingInCaseOfFaultConfig' configuration section exists, but 'ErrorQueue' value is empty. " +
                              $"Specify a value, or remove the configuration section so that the default error queue name, '{DefaultErrorQueueName}', will be used instead.";

                throw new Exception(message);
            }

            var registryErrorQueue = RegistryReader.Read("ErrorQueue");

            if (registryErrorQueue != null)
            {
                if (!string.IsNullOrWhiteSpace(registryErrorQueue))
                {
                    Logger.Debug("Error queue retrieved from registry settings.");
                    errorQueue = registryErrorQueue;
                    return(true);
                }

                var message = "'ErrorQueue' read from the registry, but the value is empty. Specify a value, or remove 'ErrorQueue' " +
                              $"from the registry so that the default error queue name, '{DefaultErrorQueueName}', will be used instead.";

                throw new Exception(message);
            }

            errorQueue = null;
            return(false);
        }
        internal static Result GetConfiguredAuditQueue(ReadOnlySettings settings)
        {
            Result configResult;

            if (settings.TryGet(out configResult))
            {
                return(configResult);
            }

            var      auditConfig = settings.GetConfigSection <AuditConfig>();
            string   address;
            TimeSpan?timeToBeReceived = null;

            if (auditConfig == null)
            {
                address = ReadAuditQueueNameFromRegistry();
            }
            else
            {
                var ttbrOverride = auditConfig.OverrideTimeToBeReceived;

                if (ttbrOverride > TimeSpan.Zero)
                {
                    timeToBeReceived = ttbrOverride;
                }
                if (string.IsNullOrWhiteSpace(auditConfig.QueueName))
                {
                    address = ReadAuditQueueNameFromRegistry();
                }
                else
                {
                    address = auditConfig.QueueName;
                }
            }
            if (address == null)
            {
                return(null);
            }
            return(new Result
            {
                Address = address,
                TimeToBeReceived = timeToBeReceived
            });
        }
Exemple #8
0
        public static bool GetConfiguredAuditQueue(ReadOnlySettings settings, out Result result)
        {
            if (settings.TryGet(out result))
            {
                return(true);
            }

            var      auditConfig = settings.GetConfigSection <AuditConfig>();
            string   address;
            TimeSpan?timeToBeReceived = null;

            if (auditConfig == null)
            {
                address = ReadAuditQueueNameFromRegistry();
            }
            else
            {
                var ttbrOverride = auditConfig.OverrideTimeToBeReceived;

                if (ttbrOverride > TimeSpan.Zero)
                {
                    timeToBeReceived = ttbrOverride;
                }
                if (string.IsNullOrWhiteSpace(auditConfig.QueueName))
                {
                    address = ReadAuditQueueNameFromRegistry();
                }
                else
                {
                    address = auditConfig.QueueName;
                }
            }
            if (address == null)
            {
                result = null;
                return(false);
            }
            result = new Result
            {
                Address          = address,
                TimeToBeReceived = timeToBeReceived
            };
            return(true);
        }
        internal static bool GetConfiguredAuditQueue(ReadOnlySettings settings, out Result result)
        {
            if (settings.TryGet(out result))
            {
                return true;
            }

            var auditConfig = settings.GetConfigSection<AuditConfig>();
            string address;
            TimeSpan? timeToBeReceived = null;
            if (auditConfig == null)
            {
                address = ReadAuditQueueNameFromRegistry();
            }
            else
            {
                var ttbrOverride = auditConfig.OverrideTimeToBeReceived;

                if (ttbrOverride > TimeSpan.Zero)
                {
                    timeToBeReceived = ttbrOverride;
                }
                if (string.IsNullOrWhiteSpace(auditConfig.QueueName))
                {
                    address = ReadAuditQueueNameFromRegistry();
                }
                else
                {
                    address = auditConfig.QueueName;
                }
            }
            if (address == null)
            {
                result = null;
                return false;
            }
            result = new Result
            {
                Address = address,
                TimeToBeReceived = timeToBeReceived
            };
            return true;
        }
        public string Determine(ReadOnlySettings settings)
        {
            var configSection = settings.GetConfigSection<AzureServiceBusQueueConfig>();
            var connectionString = configSection != null ? configSection.ConnectionString : string.Empty;

            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = defaultconnectionString;
            }

            if (configSection != null && !string.IsNullOrEmpty(configSection.IssuerKey) && !string.IsNullOrEmpty(configSection.ServiceNamespace))
                connectionString = string.Format("Endpoint=sb://{0}.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue={1}", configSection.ServiceNamespace, configSection.IssuerKey);

            if (string.IsNullOrEmpty(connectionString) && (configSection == null || string.IsNullOrEmpty(configSection.IssuerKey) || string.IsNullOrEmpty(configSection.ServiceNamespace)))
            {
                throw new ConfigurationErrorsException("No Servicebus Connection information specified, either set the ConnectionString or set the IssuerKey and ServiceNamespace properties");
            }

            return connectionString;
        }
Exemple #11
0
        public static Address GetMasterNodeAddress(ReadOnlySettings settings)
        {
            var unicastBusConfig = settings.GetConfigSection <UnicastBusConfig>();

            //allow users to override data address in config
            if (unicastBusConfig != null && !string.IsNullOrWhiteSpace(unicastBusConfig.DistributorDataAddress))
            {
                return(Address.Parse(unicastBusConfig.DistributorDataAddress));
            }

            var masterNode = GetMasterNode(settings);

            if (string.IsNullOrWhiteSpace(masterNode))
            {
                return(Address.Parse(settings.EndpointName()));
            }

            ValidateHostName(masterNode);

            return(new Address(settings.EndpointName(), masterNode));
        }
        public static Address GetMasterNodeAddress(ReadOnlySettings settings)
        {
            var unicastBusConfig = settings.GetConfigSection<UnicastBusConfig>();

            //allow users to override data address in config
            if (unicastBusConfig != null && !string.IsNullOrWhiteSpace(unicastBusConfig.DistributorDataAddress))
            {
                return Address.Parse(unicastBusConfig.DistributorDataAddress);
            }

            var masterNode = GetMasterNode(settings);

            if (string.IsNullOrWhiteSpace(masterNode))
            {
                return Address.Parse(settings.EndpointName());
            }

            ValidateHostName(masterNode);

            return new Address(settings.EndpointName(), masterNode);
        }
        public static Address GetConfiguredErrorQueue(ReadOnlySettings settings)
        {
            var errorQueue = Address.Undefined;

            var section = settings.GetConfigSection <MessageForwardingInCaseOfFaultConfig>();

            if (section != null)
            {
                if (string.IsNullOrWhiteSpace(section.ErrorQueue))
                {
                    throw new ConfigurationErrorsException(
                              "'MessageForwardingInCaseOfFaultConfig' configuration section is found but 'ErrorQueue' value is missing." +
                              "\n The following is an example for adding such a value to your app config: " +
                              "\n <MessageForwardingInCaseOfFaultConfig ErrorQueue=\"error\"/> \n");
                }

                Logger.Debug("Error queue retrieved from <MessageForwardingInCaseOfFaultConfig> element in config file.");

                errorQueue = Address.Parse(section.ErrorQueue);
            }
            else
            {
                var regRederType       = Type.GetType("NServiceBus.Utils.RegistryReader, NServiceBus.Core", true);
                var readMethod         = regRederType.GetMethod("Read", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                var registryErrorQueue = (string)readMethod.Invoke(null, new object[] { "ErrorQueue", null });
                if (!string.IsNullOrWhiteSpace(registryErrorQueue))
                {
                    Logger.Debug("Error queue retrieved from registry settings.");
                    errorQueue = Address.Parse(registryErrorQueue);
                }
            }

            if (errorQueue == Address.Undefined)
            {
                throw new ConfigurationErrorsException("Faults forwarding requires an error queue to be specified. Please add a 'MessageForwardingInCaseOfFaultConfig' section to your app.config" +
                                                       "\n or configure a global one using the powershell command: Set-NServiceBusLocalMachineSettings -ErrorQueue {address of error queue}");
            }

            return(errorQueue);
        }
        public static Address GetConfiguredErrorQueue(ReadOnlySettings settings)
        {
            var errorQueue = Address.Undefined;

            var section = settings.GetConfigSection<MessageForwardingInCaseOfFaultConfig>();
            if (section != null)
            {
                if (string.IsNullOrWhiteSpace(section.ErrorQueue))
                {
                    throw new ConfigurationErrorsException(
                        "'MessageForwardingInCaseOfFaultConfig' configuration section is found but 'ErrorQueue' value is missing." +
                        "\n The following is an example for adding such a value to your app config: " +
                        "\n <MessageForwardingInCaseOfFaultConfig ErrorQueue=\"error\"/> \n");
                }

                Logger.Debug("Error queue retrieved from <MessageForwardingInCaseOfFaultConfig> element in config file.");

                errorQueue = Address.Parse(section.ErrorQueue);
            }
            else
            {
                var regRederType = Type.GetType("NServiceBus.Utils.RegistryReader, NServiceBus.Core", true);
                var readMethod = regRederType.GetMethod("Read", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
                var registryErrorQueue = (string)readMethod.Invoke(null,new object[]{"ErrorQueue", null});
                if (!string.IsNullOrWhiteSpace(registryErrorQueue))
                {
                    Logger.Debug("Error queue retrieved from registry settings.");
                    errorQueue = Address.Parse(registryErrorQueue);
                }
            }

            if (errorQueue == Address.Undefined)
            {
                throw new ConfigurationErrorsException("Faults forwarding requires an error queue to be specified. Please add a 'MessageForwardingInCaseOfFaultConfig' section to your app.config" +
                                                       "\n or configure a global one using the powershell command: Set-NServiceBusLocalMachineSettings -ErrorQueue {address of error queue}");
            }

            return errorQueue;

        }
Exemple #15
0
        static DelayedConfig GetDelayedRetryConfig(ReadOnlySettings settings, bool transactionsOn)
        {
            if (!transactionsOn)
            {
                Logger.Warn("Delayed Retries will be disabled. Delayed retries are not supported when running with TransportTransactionMode.None. Failed messages will be moved to the error queue instead.");
                //Transactions must be enabled since Delayed Retries requires the transport to be able to rollback
                return(new DelayedConfig(0, TimeSpan.Zero));
            }

            var numberOfRetries = settings.Get <int>(NumberOfDelayedRetries);
            var timeIncrease    = settings.Get <TimeSpan>(DelayedRetriesTimeIncrease);

            var retriesConfig = settings.GetConfigSection <SecondLevelRetriesConfig>();

            if (retriesConfig != null)
            {
                numberOfRetries = retriesConfig.Enabled ? retriesConfig.NumberOfRetries : 0;
                timeIncrease    = retriesConfig.TimeIncrease;
            }

            return(new DelayedConfig(numberOfRetries, timeIncrease));
        }
Exemple #16
0
        bool HasAlternateTimeoutManagerBeenConfigured(ReadOnlySettings settings)
        {
            var unicastConfig = settings.GetConfigSection<UnicastBusConfig>();

            return unicastConfig != null && !string.IsNullOrWhiteSpace(unicastConfig.TimeoutManagerAddress);
        }
Exemple #17
0
        bool HasAlternateTimeoutManagerBeenConfigured(ReadOnlySettings settings)
        {
            var unicastConfig = settings.GetConfigSection <UnicastBusConfig>();

            return(unicastConfig != null && !string.IsNullOrWhiteSpace(unicastConfig.TimeoutManagerAddress));
        }
        static DelayedConfig GetDelayedRetryConfig(ReadOnlySettings settings, bool transactionsOn)
        {
            if (!transactionsOn)
            {
                Logger.Warn("Delayed Retries will be disabled. Delayed retries are not supported when running with TransportTransactionMode.None. Failed messages will be moved to the error queue instead.");
                //Transactions must be enabled since Delayed Retries requires the transport to be able to rollback
                return new DelayedConfig(0, TimeSpan.Zero);
            }

            var numberOfRetries = settings.Get<int>(NumberOfDelayedRetries);
            var timeIncrease = settings.Get<TimeSpan>(DelayedRetriesTimeIncrease);

            var retriesConfig = settings.GetConfigSection<SecondLevelRetriesConfig>();
            if (retriesConfig != null)
            {
                numberOfRetries = retriesConfig.Enabled ? retriesConfig.NumberOfRetries : 0;
                timeIncrease = retriesConfig.TimeIncrease;
            }

            return new DelayedConfig(numberOfRetries, timeIncrease);
        }
        static ImmediateConfig GetImmediateRetryConfig(ReadOnlySettings settings, bool transactionsOn)
        {
            if (!transactionsOn)
            {
                Logger.Warn("Immediate Retries will be disabled. Immediate Retries are not supported when running with TransportTransactionMode.None. Failed messages will be moved to the error queue instead.");
                //Transactions must be enabled since Immediate Retries requires the transport to be able to rollback
                return new ImmediateConfig(0);
            }

            var retriesConfig = settings.GetConfigSection<TransportConfig>();
            var maxImmediateRetries = retriesConfig?.MaxRetries ?? settings.Get<int>(NumberOfImmediateRetries);

            return new ImmediateConfig(maxImmediateRetries);
        }
Exemple #20
0
        public static string GetMasterNode(ReadOnlySettings settings)
        {
            var section = settings.GetConfigSection <MasterNodeConfig>();

            return(section != null ? section.Node : null);
        }
 public static string GetMasterNode(ReadOnlySettings settings)
 {
     var section = settings.GetConfigSection<MasterNodeConfig>();
     return section != null ? section.Node : null;
 }