Skip to content

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.

The primary responsibilities of the Program.cs is to:

  1. Configure middleware components.
  2. Set up the Backlot director and its dependencies.
  3. Configure logging services.
  4. Build and run the host.

The HostBuilder is 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.
var host = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureFunctionsWorkerDefaults(worker =>
{
worker.UseMiddleware<FunctionsHttpMiddleware<AutofacScopeExecutor>>(); // always identify Autofac first.
worker.UseMiddleware<FunctionsHttpMiddleware<Defender>>();
worker.UseMiddleware<FunctionsHttpMiddleware<AuthenticationInitializer>>();
worker.UseMiddleware<FunctionsHttpMiddleware<SerilogContextEnrichment>>();
})

The ConfigureBacklotWeb<TDirector> extension method (namespace Backlot.Http.DependencyInjection.Autofac) configures the host using Backlot-specific services. It registers the default Autofac container and the media formatters, and calls a factory to create your director. The factory receives the IConfiguration and the Autofac ContainerBuilder.

The repositories, the unit of work and the secret are not passed to this method. They are defined by the director: the repositories and unit of work are its type arguments, and the secret is its SecretKey property.

  1. Repository Initialization

Use MemoryRelationRepository, MemoryPersistedRoleRepository, and DummyUnitOfWork as type arguments of the director.

public class Director(IFileSystem fileSystem, IConfigurationManager configurationManager, ContainerBuilder builder)
: AutofacContainerDirector<
MemoryRelationRepository,
MemoryPersistedRoleRepository,
DummyUnitOfWork,
BasicUserContext,
CacheFactory>(fileSystem, configurationManager, builder)
  1. File System Initialization

The file system initialized using LocalDiskStorage.

var fs = new LocalDiskStorage();
  1. Configuration Manager Initialization

    The configuration manager is initialized with a JsonSettingsManager and a DuplexConfigurationSettingsManager, managing settings from both JSON files and the application configuration.

    var jsonSettingsManager = new JsonSettingsManager(configuration["Backlot.Environment"] ?? "local", fs);
    var duplexSettingsManager = new DuplexConfigurationSettingsManager(configuration, jsonSettingsManager);
  2. Director Introduction

    The director is created, combining the file system, configuration manager, and builder.

    return new Director(fs, duplexSettingsManager, builder);
.ConfigureBacklotWeb((configuration, builder) =>
{
var fs = new LocalDiskStorage();
var jsonSettingsManager = new JsonSettingsManager(configuration["Backlot.Environment"] ?? "local", fs);
var duplexSettingsManager = new DuplexConfigurationSettingsManager(configuration, jsonSettingsManager);
return new Director(fs, duplexSettingsManager, builder);
})

Logging is configured using Serilog. The configuration ensures that logs are written to the console and allows for additional logging targets, such as Azure Table Storage.

.ConfigureServices((ctx, collection) => {
collection.AddLogging(lb => lb
.AddSerilog(ctx.Configuration,
cfg => cfg.WriteTo.Console(),
level: LogEventLevel.Debug
)
);
})

Finally, the host is built and run asynchronously, marking the culmination of the configuration process and the start of the application’s execution.

.Build();
await host.RunAsync();

Here’s how the Backlot configuration looks when using the Director with RavenDB repositories and BlobStorage as the file system:

namespace Backlot.Server
{
public static class Program
{
public static async Task Main()
{
var host = new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureFunctionsWorkerDefaults(worker =>
{
worker.UseMiddleware<FunctionsHttpMiddleware<AutofacScopeExecutor>>();
worker.UseMiddleware<FunctionsHttpMiddleware<Defender>>();
worker.UseMiddleware<FunctionsHttpMiddleware<AuthenticationInitializer>>();
worker.UseMiddleware<FunctionsHttpMiddleware<SerilogContextEnrichment>>();
})
.ConfigureBacklotWeb((configuration, builder) =>
{
var fs = new BlobStorage(configuration["Backlot.BlobConnectionString"]);
var jsonSettingsManager = new JsonSettingsManager(configuration["Backlot.Environment"] ?? "local", fs);
var duplexSettingsManager = new DuplexConfigurationSettingsManager(configuration, jsonSettingsManager);
return new Director(fs, duplexSettingsManager, builder);
})
.ConfigureServices((ctx, collection) => { collection.AddLogging(lb => lb
.AddSerilog(ctx.Configuration,
cfg => cfg.WriteTo.AzureTableStorage(ctx.Configuration["Backlot.BlobConnectionString"]),
level: LogEventLevel.Debug
)
);
})
.Build();
await host.RunAsync();
}
}
}