This README provides an overview of extension methods that enhance the functionality of ASP.NET Core services. These methods are designed to make common tasks simpler and more convenient when working with ASP.NET Core applications.
ApplicationBuilderExtensions
This method applies pending migrations for the specified DbContext to the database, creating the database if it does not already exist.
app.UseDatabaseMigrations<MyDbContext>();
- app (Type:
IApplicationBuilder
): TheIApplicationBuilder
instance. - TDbContext (Type:
DbContext
): The type of the DbContext to use for the migration.
The UseDatabaseMigrations
method applies any pending migrations for the specified DbContext to the database. This ensures that the database schema is up to date with the current model.
This method asynchronously applies pending migrations for the specified DbContext to the database, creating the database if it does not already exist.
await app.UseDatabaseMigrationsAsync<MyDbContext>();
- app (Type:
IApplicationBuilder
): TheIApplicationBuilder
instance. - TDbContext (Type:
DbContext
): The type of the DbContext to use for the migration.
The UseDatabaseMigrationsAsync
method asynchronously applies any pending migrations for the specified DbContext to the database. This ensures that the database schema is up to date with the current model.
This method configures OpenTelemetry for the application, setting up tracing, metrics, and logging.
builder.ConfigureOpenTelemetry();
- builder (Type:
IHostApplicationBuilder
): TheIHostApplicationBuilder
instance.
The ConfigureOpenTelemetry
method sets up OpenTelemetry for the application, including tracing, metrics, and logging. It configures OpenTelemetry to use various instrumentation and exporters, and includes settings for tracing and metrics.
ConfigurationExtensions
This method retrieves the specified connection string or throws an exception if it is not found.
var connectionString = configuration.GetConnectionStringOrThrow("MyConnectionString");
- configuration (Type:
IConfiguration
): The configuration instance. - name (Type:
string
): The name of the connection string.
The GetConnectionStringOrThrow
method retrieves the specified connection string from the configuration. If the connection string is not found or is empty, an exception is thrown.
This method retrieves the specified configuration value or throws an exception if it is not found.
var value = configuration.GetRequiredValueOrThrow("MyConfigValue");
- configuration (Type:
IConfiguration
): The configuration instance. - name (Type:
string
): The name of the configuration value.
The GetRequiredValueOrThrow
method retrieves the specified configuration value from the configuration. If the value is not found or is empty, an exception is thrown.
This method adds API version support to the IServiceCollection
instance.
services.AddVersioning(majorVersion, minorVersion);
- services (Type:
IServiceCollection
): TheIServiceCollection
instance. - majorVersion (Type:
int
): The major version number. - minorVersion (Type:
int
): The minor version number.
The AddVersioning
method configures API versioning for your ASP.NET Core application. It allows you to specify the default API version, assume a default version when not specified, and read the version from the "api-version" header.
This method adds JWT authentication services to the IServiceCollection
instance.
services.AddJsonWebTokenAuthentication();
- services (Type:
IServiceCollection
): TheIServiceCollection
instance.
The AddJsonWebTokenAuthentication
method configures JWT authentication for your ASP.NET Core application. It sets up the default authentication and challenge schemes to use JWT bearer tokens and registers the necessary services.
WebHostBuilderExtensions
This method adds Kestrel server configuration to the IWebHostBuilder
instance.
webHostBuilder.AddKestrelConfiguration(port);
- webHostBuilder (Type:
IWebHostBuilder
): TheIWebHostBuilder
instance. - port (Type:
int
): The port number to listen on.
The AddKestrelConfiguration
method configures Kestrel server settings, specifying that it should listen on any IP address and use HTTP/1.1, HTTP/2, and HTTP/3 protocols with HTTPS