Program.cs
When you start an Azure Functions project, a Program.cs
file is automatically generated. The Main
function in this file serves as the entry point for the application. In this example, the Azure Functions project is configured with the name Backlot.Server
.
The primary responsibilities of the Program.cs
is to:
Initialize the base environment.
Configure middleware components.
Set up application-specific configurations.
Configure logging services.
Build and run the host.
Base Initialization
The initialization begins with a call to Base.Start()
, which sets up the foundational environment for the application. This step is vital for preparing any necessary pre-conditions or configurations required before the host is built.
Host Builder Configuration
The HostBuilder
is then used to configure and build the application host. The ConfigureFunctionsWorkerDefaults
method is called to set up the default function worker, incorporating several middleware components:
AutofacScopeExecutor
: Ensures that Autofac is identified first for dependency injection.Defender
: Provides defense mechanisms to check for the presence of specific forbidden keywords, and if found, it returns a BadRequest response with a message.AuthenticationInitializer
: Sets up authentication processes.SerilogContextEnrichment
: Enhances logging with Serilog by adding contextual information.
Application Configuration with Backlot
The ChaplinAppConfiguration
method configures the application using Backlot-specific services. This involves setting up repositories, a file system, a configuration manager, a director, and a secret for token calculation.
Repository Initialization
The example shows how to use
MemoryRelationRepository
,MemoryPersistedRoleRepository
, andDummyUnitOfWork
..ChaplinAppConfiguration<MemoryRelationRepository, MemoryPersistedRoleRepository, DummyUnitOfWork>(The example shows how to use
RavenRelationRepository
,RavenPersistedRoleRepository
, andRavenUnitOfWork
to integrate RavenDB..ChaplinAppConfiguration<RavenRelationRepository, RavenPersistedRoleRepository, RavenUnitOfWork>(File System Initialization
The file system initialized using
LocalDiskStorage
.fileSystem: (context) => new LocalDiskStorage(),The file system initialized using BlobStorage can be initialized with the connection string from the configuration.
fileSystem: (context) => new BlobStorage(context.Configuration["Chaplin.BlobConnectionString"]),Configuration Manager Initialization
The configuration manager is initialized with a
JsonSettingsManager
and aDuplexConfigurationSettingsManager
, managing settings from both JSON files and the application configuration.configurationManager: (context, fs) => { var jsonSettingsManager = new JsonSettingsManager(context.Configuration["Chaplin.Environment"] ?? "local", fs); return new DuplexConfigurationSettingsManager(context.Configuration, jsonSettingsManager); },Director Introduction
The director is introduced, combining the file system, configuration manager, and builder.
director: (_, fs, cm, builder) => new Director(fs, cm, builder),Secret Configuration
The secret is retrieved from the configuration and used for token calculations.
secret: (context) => context.Configuration["Chaplin.Secret"]
Logging Configuration with Serilog
Logging is configured using Serilog, an advanced logging library. The configuration ensures that logs are written to the console and allows for additional logging targets, such as Azure Table Storage.
Building and Running the Host
Finally, the host is built and run asynchronously, marking the culmination of the configuration process and the start of the application’s execution.
Complete Backlot Program.cs with RavenDB and BlobStorage
Here's how the Backlot configuration looks when using RavenDB as the database and BlobStorage as the file system: