Startup Code - Background Job

Flexbase Solution Structure

Let us look into the startup code for the background job end points. We have two Endpoints for the background job. One is for Handler and one is for the subscribers.

We have the basic configuration Builder and IConfiguration setup done in the program.cs. We will go to the create host builders and in that we are configuring the Serilog with the program.

Then we have the RegisterFlexServices. In that, we are using the AddFlexBase. Then the primary key generator is added and then we have the automaker configuration.

private static void RegisterFlexServices(IServiceCollection services, HostBuilderContext hostContext)
        {
            services.AddFlexBase(new List<AssembliesToLoad> { new AssembliesToLoad("ECommerceDemo*.dll") });

            //Add PK Generator
            services.AddTransient<IFlexPrimaryKeyGeneratorBridge, SequentialGuidPrimaryKeyGeneratorBridge>();

            services.AddAutoMapper(typeof(CoreMapperConfiguration).Assembly);

            ConfigureBusServices(services, hostContext);
            ConfigureDbServices(services, hostContext);
        }

In the DB Services, the connection provider is first configured, and we have by default as AppSettingConnectionProvider for write & read DB. Multi-tenant will be discussed when we look at the multi-tenant sections. We have configured the EFDbContext and the RepositoryBridge for the write and read DB.

private static void ConfigureDbServices(IServiceCollection services, HostBuilderContext hostContext)
        {
            //You can change your Db implementation here

            //Default Settings for connection provider
            services.AddTransient<IWriteDbConnectionProviderBridge, AppSettingsWriteDbConnectionProvider>();
            services.AddTransient<IReadDbConnectionProviderBridge, AppSettingsReadDbConnectionProvider>();

            //Configure multitenant application
            //services.AddTransient<IWriteDbConnectionProviderBridge, NativeWriteDbTenantConnectionProvider>();
            //services.AddTransient<IReadDbConnectionProviderBridge, NativeReadDbTenantConnectionProvider>();
            //services.AddTransient<IFlexNativeHostTenantProviderBridge, FlexNativeHostTenantProviderBridge>();

            services.AddTransient<FlexEFDbContext, ApplicationEFDbContext>();
            services.AddTransient<IFlexRepositoryBridge, FlexWriteDbRepositoryEFSQLServer>();
            services.AddTransient<IFlexQueryRepositoryBridge, FlexReadDbRepositoryEFSQLServer>(); 

        }

Then we have the bus configuration added to the startup code:

private static void ConfigureBusServices(IServiceCollection services, HostBuilderContext hostContext)
        {
            List<BusRouteConfig> routes = new List<BusRouteConfig>();

            //TODO: Set the default destination address
            string defaultDestinationEndPoint = "ECommerceDemo-EndPoint-Handlers";

            //TODO: Uncomment this line of code and Configure your route
            //routes.Add(new BusRouteConfig(typeof(OneOfYourCommand).Assembly, defaultDestinationEndPoint));

            Guard.AgainstNullAndEmpty("EndPoint name cannot be empty", _configuration.GetSection("FlexBase")["EndPoint"]);
            string endPointName = _configuration.GetSection("FlexBase")["EndPoint"];

            //You can change your Bus implementation here
            FlexBusGammaDefaultEndpointConfiguration endPointConfig = null;

            if (hostContext.HostingEnvironment.IsDevelopment())
            {
                endPointConfig = new LearningBusConfiguration(endPointName, routes);
            }
            if (hostContext.HostingEnvironment.IsStaging())
            {
                Guard.AgainstNullAndEmpty("AzureBusStorageConnectionString for connection string cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
                string azureConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
                endPointConfig = new AzureBusConfiguration(endPointName, routes, azureConnectionString, azureConnectionString);
            }
            if (hostContext.HostingEnvironment.IsProduction())
            {
                Guard.AgainstNullAndEmpty("AzureBusStorageConnectionString for connection string cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
                string azureConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
                endPointConfig = new AzureBusConfiguration(endPointName, routes, azureConnectionString, azureConnectionString);
            }

            services.InitializeBusGammaWithExternallyManagedServiceProvider(endPointConfig);

            services.AddSingleton<IFlexServiceBusBridge>(bus => new BusGammaServiceBusBridge(endPointConfig));
        }

This is the default destination Endpoint where the message should be sent or read from. Because this will be like an Endpoint where the WebAPI will send the messages which will be processed, and the subscribers will receive the messages. This is by default. We have given the same name as the Handler Endpoint. If you go to the AppSetting Each Endpoint will have a unique name. We call it ‘Endpoint.Handlers’. Any name can be given as wished. Using the name, the queue will be created in the Chosen queue provider with the bus. In the same way, we have a different name for the Subscribers and for the WebAPI.

In the WebAPI bus configuration, we were putting a destination Endpoint. Any message that is sent from the WebAPI will end up in the Handler Endpoint because ‘Ecommerce.EndPoint.Handler’ is the name of the destination.

It will be processed in this endpoint because the name of this endpoint is the same that we have defined for addressing the messages

Moving back to the program, we have the LearningBusConfiguration, and AzureBusConfiguration. It can be changed as per the need. Then we have added the FlexServiceBusBridge to the services.

This ends the configuration section of our endpoints.

Now as everything is configured with the DI container, from the DI container we are getting the instance of the bus starting after the application starts.

That's all now for the basic configuration in understanding the endpoint.

Last updated