Flexbase
Search…
Startup Code - WebAPI
Flexbase Solution Structure
Lets understand the startup file that is given by default in the hosting layer.
Particularly the WebAPI startup file. We go back to the solution and then to the startup file.
For a startup, the Swagger is configured for the specific startup. All the default configurations are there for the Swagger.
1
services.AddSwaggerGen(c =>
2
{
3
c.SwaggerDoc("v1", new OpenApiInfo
4
{
5
Version = "v1",
6
Title = "ECommerceDemo.EndPoint.WebAPI",
7
Description = "ECommerceDemo.EndPoint.WebAPI",
8
TermsOfService = new Uri("https://example.com/terms"),
9
Contact = new OpenApiContact
10
{
11
Name = "Your Project",
12
Email = string.Empty,
13
Url = new Uri("http://www.yourwebsite.com"),
14
},
15
License = new OpenApiLicense
16
{
17
Name = "Use under LICX",
18
Url = new Uri("http://www.yourwebsite.com/license"),
19
}
20
});
21
c.CustomSchemaIds(x => x.FullName);
22
});
Copied!
Then we have FlexBase registered with the services. This will register all the Flexbase dependency with the DI container. Along with that, we give the ‘EcommerceDemo*.dll’ that is starting with e-commerce. We need the assembly there also, and you can have multiple assemblies.
1
private void RegisterFlexServices(IServiceCollection services)
2
{
3
services.AddFlexBase(new List<AssembliesToLoad> { new AssembliesToLoad("ECommerceDemo*.dll") });
4
5
//Add PK Generator
6
services.AddTransient<IFlexPrimaryKeyGeneratorBridge, SequentialGuidPrimaryKeyGeneratorBridge>();
7
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
8
services.AddTransient<IFlexHostHttpContextAccesorBridge, FlexDefaultHttpContextAccessorBridge>();
9
10
services.AddAutoMapper(typeof(CoreMapperConfiguration).Assembly,typeof(HttpContextInfoAutomapperConfiguration).Assembly);
11
12
ConfigureBusServices(services);
13
ConfigureDbServices(services);
14
15
}
Copied!
Then we have the PrimaryKeyGeneratorBridge as a sequential GUID primary key. We will discuss that when we go into Crud operations. To configure the HTTP context accessor, it needs to be configured Then there is HttpContextAccessorr which we will see in detail. These are all required dependencies for Flex to work.
The AutoMapper is configured,
And then we have the default database configuration.
1
private void ConfigureDbServices(IServiceCollection services)
2
{
3
4
//Default Settings for connection provider
5
services.AddTransient<IWriteDbConnectionProviderBridge, AppSettingsWriteDbConnectionProvider>();
6
services.AddTransient<IReadDbConnectionProviderBridge, AppSettingsReadDbConnectionProvider>();
7
8
//Configure multitenant application
9
//services.AddTransient<IWriteDbConnectionProviderBridge, NativeWriteDbTenantConnectionProvider>();
10
//services.AddTransient<IReadDbConnectionProviderBridge, NativeReadDbTenantConnectionProvider>();
11
//services.AddTransient<IFlexNativeHostTenantProviderBridge, FlexNativeHostTenantProviderBridge>();
12
13
services.AddTransient<FlexEFDbContext, ApplicationEFDbContext>();
14
15
services.AddTransient<IFlexRepositoryBridge, FlexWriteDbRepositoryEFSQLServer>();
16
services.AddTransient<IFlexQueryRepositoryBridge, FlexReadDbRepositoryEFSQLServer>();
17
}
Copied!
The EFDbContext is registered with the container and the FlexRepositoryBridge and FlexQueryRepositoryBridge which is used for the write operations and query operations, respectively. Both the repositories can be kept separate. If there is a need for only one database then the connection string needs to be kept same for both the connection providers.
One can configure their own repository based on the needs. Most of it is done by default for the ease of the user.
Then we have the configuration for Bus services.
1
private void ConfigureBusServices(IServiceCollection services)
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("End point 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
16
FlexBusGammaDefaultEndpointConfiguration endPointConfig = null;
17
18
if (_env.IsDevelopment())
19
{
20
endPointConfig = new LearningBusConfiguration(endPointName, routes);
21
}
22
if (_env.IsStaging())
23
{
24
Guard.AgainstNullAndEmpty("AzureBusStorageConnectionString for connection string cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
25
string azureConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
26
endPointConfig = new AzureBusConfiguration(endPointName, routes, azureConnectionString, azureConnectionString);
27
}
28
if (_env.IsProduction())
29
{
30
Guard.AgainstNullAndEmpty("AzureBusStorageConnectionString for connection string cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
31
string azureConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
32
endPointConfig = new AzureBusConfiguration(endPointName, routes, azureConnectionString, azureConnectionString);
33
}
34
35
services.InitializeBusGammaWithExternallyManagedServiceProvider(endPointConfig);
36
37
services.AddSingleton<IFlexServiceBusBridge>(bus => new BusGammaServiceBusBridge(endPointConfig));
38
}
Copied!
The first thing that needs to be done is the routing. There's only one configuration that needs to be done is the route which configures the route for one of your command. We will see that when we configure for our Crud operations. All the Bus configuration as explained earlier is configured and the Flex Bus Service Bridges added to the dependency container.
That's all about the configuration startup. In the program.cs, all the logger configuration is done. Underneath we use Microsoft ILogger so that it becomes easier to implement one’s own logger. ILogger is used across Flexbase internally. ILogger is configured to use Serilog by default.
1
.UseSerilog((context, loggerConfig) =>
2
{
3
if (context.HostingEnvironment.IsDevelopment())
4
{
5
loggerConfig.WithFlexDefaultWebConfiguration("ECommerceDemo.EndPoint.WebAPI", _configuration)
6
.MinimumLevel.Verbose()
7
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
8
.MinimumLevel.Override("System", LogEventLevel.Warning)
9
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
10
.WriteToColoredConsole()
11
.WritePerformanceMonitorToColoredConsole()
12
.WriteUsageToColoredConsole()
13
.WriteToFile("ECommerceDemo.EndPoint.WebAPI.log.txt")
14
.WritePerformanceMonitorToFile("ECommerceDemo.EndPoint.WebAPI.log.perf.txt")
15
.WriteUsageToFile("ECommerceDemo.EndPoint.WebAPI.log.usage.txt");
16
}
17
if (context.HostingEnvironment.IsStaging())
18
{
19
Guard.AgainstNullAndEmpty("AzureStorageConnectionString cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
20
string azureStorageConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
21
22
loggerConfig.WithFlexDefaultWebConfiguration("ECommerceDemo.EndPoint.WebAPI", _configuration)
23
.MinimumLevel.Debug()
24
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
25
.MinimumLevel.Override("System", LogEventLevel.Warning)
26
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
27
.WriteToColoredConsole()
28
.WritePerformanceMonitorToColoredConsole()
29
.WriteUsageToColoredConsole()
30
.WriteToAzureTableStorage(azureStorageConnectionString, "GeneralLogs")
31
.WritePerformanceMonitorToAzureTableStorage(azureStorageConnectionString, "PerformanceTable")
32
.WriteUsageToAzureTableStorage(azureStorageConnectionString, "UsageTable");
33
}
34
if (context.HostingEnvironment.IsProduction())
35
{
36
Guard.AgainstNullAndEmpty("AzureStorageConnectionString cannot be empty", _configuration.GetSection("FlexBase")["AzureStorageConnectionString"]);
37
string azureStorageConnectionString = _configuration.GetSection("FlexBase")["AzureStorageConnectionString"];
38
39
loggerConfig.WithFlexDefaultWebConfiguration("ECommerceDemo.EndPoint.WebAPI", _configuration)
40
.MinimumLevel.Information()
41
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
42
.MinimumLevel.Override("System", LogEventLevel.Warning)
43
.MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Information)
44
.WriteToColoredConsole()
45
.WritePerformanceMonitorToColoredConsole()
46
.WriteUsageToColoredConsole()
47
.WriteToAzureTableStorage(azureStorageConnectionString, "GeneralLogs")
48
.WritePerformanceMonitorToAzureTableStorage(azureStorageConnectionString, "PerformanceTable")
49
.WriteUsageToAzureTableStorage(azureStorageConnectionString, "UsageTable");
50
}
51
});
Copied!

Video

Last modified 1yr ago
Copy link
Contents
Video