0

I'm designing class library with background service which has

public static class ServiceCollectionExtensions

Which have few methods which will be call outside. Now above mentioned extension will call Builder pattern which actually building a services/Adding the services. Now my problem is I'm designing a Mass-transit-Bus logic which will be added in between Extension class and Builder class. To clarify more here is glimpse of my code -

public static class ServiceCollectionExtensions
    {
        public static IConsumerBuilder<string> AddX(this IServiceCollection services)
        {
            services.AddConsumer<string>();
              
            services.AddSingleton(provider => ConfigureBus());//Configure basic Mass transit Bus 
              
            return new XBuilder<string>(services);
        }
        
         private static IBusControl ConfigureBus()
        {
            return Bus.Factory.CreateUsingRabbitMq(async x =>
            {

                x.Host("host", "virtualhost", h =>
                {
                    h.Username("Uname");
                    h.Password("pwd");
                });
     

            });
        }
        
    }
    
    internal sealed class XBuilder<TValue> : IXBuilder<TValue>
    {
     public XBuilder(IServiceCollection services)
        {
            Services = services;
        }
        public IServiceCollection Services { get; }
        
         IXBuilder<TValue> IXBuilder<TValue>.AddXHandler<THandler, T>(string configSection)
        {
          
            Services.AddOptions<THandler>(typeof(THandler).FullName).Configure<IConfiguration>((options, config) =>
            {
                config.GetSection(configSection).Bind(options);
                settings = config.GetSection(nameof(Settings)).Get<Settings>();
            });

            Services.AddHostedService<BackgraoundService<TValue>>();
            Services.AddScoped<IMyDtoHandler<T>, THandler>();

            ConfigureX<T>();

            return this;
        }
        
        //
        private void ConfigureX<T>(Settings settings) where T : class
        {
            var serviceProvider = Services.BuildServiceProvider();
            var busControl = serviceProvider.GetRequiredService<IBusControl>();
            busControl.ConnectReceiveEndpoint(settings.QName, configurator =>
            {
                var notworkingHandler = serviceProvider.GetService<IMyDtoHandler<T>>();

                if (notworkingHandler is null)
                    using (var scope = serviceProvider.CreateScope())
                    {
                        notworkingHandler = scope.ServiceProvider.GetRequiredService<IMyDtoHandler<T>>();

                    }
                configurator.Consumer(() => new EventHandlerAdaptor<IMyDtoHandler<T>, T>(notworkingHandler));
            });
        }
        
        
    }

in background service -

BackgraoundService at ExecuteAsync
{
//I need to do Bus.Start()

 _ = await IBusCntrl.StartAsync(cancellationToken).OrCanceled(cancellationToken); 
 
 while (cancellationToken.IsCancellationRequested)
        {
          await  StopBus(cancellationToken, IBusCntrl);
        }
        
 
}

Now my questions where i need suggestion(s)

  • What is the way to combined the MAsstransit Bus logic seperately which will have only implementation of Configure Bus , then ConfigureConsumer,ConfigureProducer ,GetBus(), StartBus, StopBus etc.
  • Even if I do separate Masstransit logic what about

var serviceProvider = Services.BuildServiceProvider(); var busControl = serviceProvider.GetRequiredService();

  • Is there any better way to separate out logic w.r.t. serviceProvider with IServiceCollection

I'm already using Builder Pattern not sure which one I should follow

  1. For separating out Bus logic which will be called from all over the places.
  2. And for IServiceCollection where i need to get few services by using BuildServiceProvider.
  3. How to make it generic way (as you have seen above code is more generic way and have only dependency on IMyDtoHandler.

Appreciate any guidance and thanks in advance.

HAppy coding.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.