public static IServiceCollection AddNServiceBus(this IServiceCollection services, IConfiguration config, bool isDevelopment)
        {
            return(services
                   .AddSingleton(p =>
            {
                var container = p.GetService <IContainer>();
                var configuration = config.GetSection(ProviderRegistrationsConfigurationKeys.NServiceBusSettings).Get <NServiceBusSettings>();

                var endpointName = "SFA.DAS.ProviderRegistrations.MessageHandlers";
                var endpointConfiguration = new EndpointConfiguration(endpointName)
                                            .UseErrorQueue($"{endpointName}-errors")
                                            .UseInstallers()
                                            .UseLicense(configuration.NServiceBusLicense)
                                            .UseMessageConventions()
                                            .UseNewtonsoftJsonSerializer()
                                            .UseNLogFactory()
                                            .UseOutbox()
                                            .UseSqlServerPersistence(() => container.GetInstance <DbConnection>())
                                            .UseStructureMapBuilder(container)
                                            .UseUnitOfWork();

                if (isDevelopment)
                {
                    endpointConfiguration.UseLearningTransport(s => s.AddRouting());
                }
                else
                {
                    var transport = endpointConfiguration.UseTransport <AzureServiceBusTransport>();
                    var ruleNameShortener = new RuleNameShortener();

                    var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();
                    transport.CustomTokenProvider(tokenProvider);
                    transport.ConnectionString(configuration.ServiceBusConnectionString);
                    transport.RuleNameShortener(ruleNameShortener.Shorten);
                    transport.Routing().AddRouting();
                }

                var endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

                return endpoint;
            })
                   .AddHostedService <NServiceBusHostedService>());
        }
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            var nameShortener            = new RuleNameShortener();
            var endpointConfigurationRaw = RawEndpointConfiguration.Create(_attribute.Endpoint, OnMessage, _poisonMessageQueue);

            if (_nServiceBusOptions.EndpointConfiguration != null)
            {
                endpointConfigurationRaw = _nServiceBusOptions.EndpointConfiguration.Invoke(endpointConfigurationRaw);
            }
            else
            {
                var tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();

                endpointConfigurationRaw.UseTransport <AzureServiceBusTransport>()
                .RuleNameShortener(nameShortener.Shorten)
                .CustomTokenProvider(tokenProvider)
                .ConnectionString(_attribute.Connection)
                .Transactions(TransportTransactionMode.ReceiveOnly);
            }

            if (!string.IsNullOrEmpty(EnvironmentVariables.NServiceBusLicense))
            {
                endpointConfigurationRaw.UseLicense(EnvironmentVariables.NServiceBusLicense);
            }
            endpointConfigurationRaw.DefaultErrorHandlingPolicy(_poisonMessageQueue, ImmediateRetryCount);
            endpointConfigurationRaw.AutoCreateQueue();

            _endpoint = await RawEndpoint.Start(endpointConfigurationRaw).ConfigureAwait(false);

            if (_nServiceBusOptions.OnStarted != null)
            {
                _nServiceBusOptions.OnStarted.Invoke(_endpoint);
            }

            await _endpoint.SubscriptionManager.Subscribe(_parameter.ParameterType, new ContextBag());
        }
 public RuleNameShortenerTestsFixture()
 {
     RuleNameShortener = new RuleNameShortener();
 }