Table of Contents

ApplicationBuilderExtensions Class

Definition

Namespace
Cuemon.Extensions.AspNetCore.Hosting
Assemblies
Cuemon.Extensions.AspNetCore.dll
Source
src/Cuemon.Extensions.AspNetCore/Hosting/ApplicationBuilderExtensions.cs

Extension methods for the IApplicationBuilder interface.

public static class ApplicationBuilderExtensions
Inheritance
ApplicationBuilderExtensions

Examples

The following example demonstrates how to add the hosting environment HTTP header middleware to an ASP.NET Core application pipeline and invoke it to observe the configured X-Environment response header.

// Program.cs
using System;
using System.Linq;
using System.Net.Http;
using Cuemon.Extensions.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    EnvironmentName = Environments.Development
});
await using var app = builder.Build();
app.Urls.Add("http://127.0.0.1:0");

app.UseHostingEnvironment(options =>
{
    options.HeaderName = "X-Environment";
});
app.Run(context => context.Response.WriteAsync("ok"));

await app.StartAsync();
try
{
    using var client = new HttpClient { BaseAddress = new Uri(app.Urls.First()) };
    using var response = await client.GetAsync("/");
    Console.WriteLine($"X-Environment: {string.Join(", ", response.Headers.GetValues("X-Environment"))}");
}
finally
{
    await app.StopAsync();
}

Methods

Name Description
UseHostingEnvironment(IApplicationBuilder, Action<HostingEnvironmentOptions>)

Adds a hosting environment HTTP header to the IApplicationBuilder request execution pipeline.