Class HostingEnvironmentMiddleware
- Namespace
- Cuemon.AspNetCore.Hosting
- Assembly
- Cuemon.AspNetCore.dll
Provides a hosting environment middleware implementation for ASP.NET Core.
public class HostingEnvironmentMiddleware : ConfigurableMiddleware<IHostEnvironment, HostingEnvironmentOptions>, IConfigurable<HostingEnvironmentOptions>
- Inheritance
-
HostingEnvironmentMiddleware
- Implements
- Inherited Members
Examples
The following example demonstrates how to register and use HostingEnvironmentMiddleware in the ASP.NET Core pipeline.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
namespace Cuemon.AspNetCore.Hosting;
public static class HostingEnvironmentMiddlewareExample
{
public static async Task DemonstrateAsync()
{
var context = new DefaultHttpContext();
var middleware = new HostingEnvironmentMiddleware(
httpContext => httpContext.Response.WriteAsync("Hello"),
Options.Create(new HostingEnvironmentOptions
{
HeaderName = "X-Environment",
SuppressHeaderPredicate = _ => false
}));
await middleware.InvokeAsync(context, new SampleHostEnvironment());
Console.WriteLine(context.Response.Headers["X-Environment"].ToString());
}
private sealed class SampleHostEnvironment : IHostEnvironment
{
public string ApplicationName { get; set; } = "Docs";
public IFileProvider ContentRootFileProvider { get; set; } = new NullFileProvider();
public string ContentRootPath { get; set; } = ".";
public string EnvironmentName { get; set; } = Environments.Staging;
}
}
Constructors
HostingEnvironmentMiddleware(RequestDelegate, IOptions<HostingEnvironmentOptions>)
Initializes a new instance of the HostingEnvironmentMiddleware class.
public HostingEnvironmentMiddleware(RequestDelegate next, IOptions<HostingEnvironmentOptions> setup)
Parameters
nextRequestDelegateThe delegate of the request pipeline to invoke.
setupIOptions<HostingEnvironmentOptions>The HostingEnvironmentOptions which need to be configured.
HostingEnvironmentMiddleware(RequestDelegate, Action<HostingEnvironmentOptions>)
Initializes a new instance of the HostingEnvironmentMiddleware class.
public HostingEnvironmentMiddleware(RequestDelegate next, Action<HostingEnvironmentOptions> setup)
Parameters
nextRequestDelegateThe delegate of the request pipeline to invoke.
setupAction<HostingEnvironmentOptions>The HostingEnvironmentOptions which need to be configured.
Methods
InvokeAsync(HttpContext, IHostEnvironment)
Executes the HostingEnvironmentMiddleware.
public override Task InvokeAsync(HttpContext context, IHostEnvironment di)
Parameters
contextHttpContextThe context of the current request.
diIHostEnvironmentThe dependency injected IHostEnvironment of InvokeAsync(HttpContext, IHostEnvironment).
Returns
- Task
A task that represents the execution of this middleware.