Flexbase
Search…
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.
1
private static void RegisterFlexServices(IServiceCollection services, HostBuilderContext hostContext)
2
{
3
services.AddFlexBase(new List<AssembliesToLoad> { new AssembliesToLoad("ECommerceDemo*.dll") });
4
5
//Add PK Generator
6
services.AddTransient<IFlexPrimaryKeyGeneratorBridge, SequentialGuidPrimaryKeyGeneratorBridge>();
7
8
services.AddAutoMapper(typeof(CoreMapperConfiguration).Assembly);
9
10
ConfigureBusServices(services, hostContext);
11
ConfigureDbServices(services, hostContext);
12
}
Copied!
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.
1
private static void ConfigureDbServices(IServiceCollection services, HostBuilderContext hostContext)
2
{
3
//You can change your Db implementation here
4
5
//Default Settings for connection provider
6
services.AddTransient<IWriteDbConnectionProviderBridge, AppSettingsWriteDbConnectionProvider>();
7
services.AddTransient<IReadDbConnectionProviderBridge, AppSettingsReadDbConnectionProvider>();
8
9
//Configure multitenant application
10
//services.AddTransient<IWriteDbConnectionProviderBridge, NativeWriteDbTenantConnectionProvider>();
11
//services.AddTransient<IReadDbConnectionProviderBridge, NativeReadDbTenantConnectionProvider>();
12
//services.AddTransient<IFlexNativeHostTenantProviderBridge, FlexNativeHostTenantProviderBridge>();
13
14
services.AddTransient<FlexEFDbContext, ApplicationEFDbContext>();
15
services.AddTransient<IFlexRepositoryBridge, FlexWriteDbRepositoryEFSQLServer>();
16
services.AddTransient<IFlexQueryRepositoryBridge, FlexReadDbRepositoryEFSQLServer>();
17
18
}
Copied!
Then we have the bus configuration added to the startup code:
1
private static void ConfigureBusServices(IServiceCollection services, HostBuilderContext hostContext)
2
{
3
List<BusRouteConfig> routes = new List<BusRouteConfig>();
4
5
//TODO: Set the default destination address
6
string defaultDestinationEndPoint = "ECommerceDemo-EndPoint-Handlers";
7
8
//TODO: Uncomment this line of code and Configure your route
9
//routes.Add(new BusRouteConfig(typeof(OneOfYourCommand).Assembly, defaultDestinationEndPoint));
10
11
Guard.AgainstNullAndEmpty("EndPoint name cannot be empty", _configuration.GetSection("FlexBase")["EndPoint"]);
12
string endPointName = _configuration.GetSection("FlexBase")["EndPoint"];
13
14
//You can change your Bus implementation here
15
FlexBusGammaDefaultEndpointConfiguration endPointConfig = null;
16
17
if (hostContext.HostingEnvironment.IsDevelopment())
18
{
19
endPointConfig = new LearningBusConfiguration(endPointName, routes);
20
}
21
if (hostContext.HostingEnvironment.IsStaging())
22
{
23
Guard.AgainstNullAndEmpty("AzureBusStorageConnectionString for connection string cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
24
string azureConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
25
endPointConfig = new AzureBusConfiguration(endPointName, routes, azureConnectionString, azureConnectionString);
26
}
27
if (hostContext.HostingEnvironment.IsProduction())
28
{
29
Guard.AgainstNullAndEmpty("AzureBusStorageConnectionString for connection string cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
30
string azureConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
31
endPointConfig = new AzureBusConfiguration(endPointName, routes, azureConnectionString, azureConnectionString);
32
}
33
34
services.InitializeBusGammaWithExternallyManagedServiceProvider(endPointConfig);
35
36
services.AddSingleton<IFlexServiceBusBridge>(bus => new BusGammaServiceBusBridge(endPointConfig));
37
}
Copied!
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 modified 1yr ago
Copy link