DRN.Framework.Hosting 0.7.0-preview029

Prefix Reserved
This is a prerelease version of DRN.Framework.Hosting.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package DRN.Framework.Hosting --version 0.7.0-preview029
                    
NuGet\Install-Package DRN.Framework.Hosting -Version 0.7.0-preview029
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DRN.Framework.Hosting" Version="0.7.0-preview029" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DRN.Framework.Hosting" Version="0.7.0-preview029" />
                    
Directory.Packages.props
<PackageReference Include="DRN.Framework.Hosting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DRN.Framework.Hosting --version 0.7.0-preview029
                    
#r "nuget: DRN.Framework.Hosting, 0.7.0-preview029"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DRN.Framework.Hosting@0.7.0-preview029
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DRN.Framework.Hosting&version=0.7.0-preview029&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DRN.Framework.Hosting&version=0.7.0-preview029&prerelease
                    
Install as a Cake Tool

DRN.Framework.Hosting

Introduction

DRN.Framework.Hosting package provides practical, effective distributed application hosting code with sensible defaults, configuration options.

This package manages configuration, logging, http server (Kestrel) codes and configuration. Since each distributed app at least requires an endpoint to support health checking, this packages assumes each distributed application is also a web application.

QuickStart: Basics

Here's a basic test demonstration to take your attention and get you started:

using DRN.Framework.Hosting.DrnProgram;
using Sample.Application;
using Sample.Infra;

namespace Sample.Hosted;

public class Program : DrnProgramBase<Program>, IDrnProgram
{
    public static async Task Main(string[] args) => await RunAsync(args);

    protected override void AddServices(IServiceCollection services) => services
        .AddSampleInfraServices()
        .AddSampleApplicationServices();
}

You can easily test your application with DRN.Framework.Testing package.

public class StatusControllerTests(ITestOutputHelper outputHelper)
{
    [Theory]
    [DataInline]
    public async Task StatusController_Should_Return_Status(TestContext context)
    {
        context.ApplicationContext.LogToTestOutput(outputHelper);
        var application = context.ApplicationContext.CreateApplication<Program>();
        await context.ContainerContext.Postgres.ApplyMigrationsAsync();

        var client = application.CreateClient();
        var status = await client.GetFromJsonAsync<ConfigurationDebugViewSummary>("Status");
        var programName = typeof(Program).GetAssemblyName();
        status?.ApplicationName.Should().Be(programName);
    }
}

Configuration

DRN hosting package applies configuration in following order:

    public static IConfigurationBuilder AddDrnSettings(this IConfigurationBuilder builder, string applicationName, string[]? args = null,
        string settingJsonName = "appsettings",
        IServiceCollection? sc = null)
    {
        if (string.IsNullOrWhiteSpace(settingJsonName))
            settingJsonName = "appsettings";

        var environment = GetEnvironment(settingJsonName, args, sc);
        builder.AddJsonFile($"{settingJsonName}.json", true);
        builder.AddJsonFile($"{settingJsonName}.{environment.ToString()}.json", true);

        if (applicationName.Length > 0)
            try
            {
                var assembly = Assembly.Load(new AssemblyName(applicationName));
                builder.AddUserSecrets(assembly, true);
            }
            catch (FileNotFoundException e)
            {
                _ = e;
            }

        builder.AddSettingsOverrides(args, sc);
        builder.AddInMemoryCollection(new[]
        {
            new KeyValuePair<string, string?>(nameof(IAppSettings.ApplicationName), applicationName)
        });

        return builder;
    }
    
    //In the future, DRN.Nexus's remote configuration support will also be added to AddSettingsOverrides.
    private static void AddSettingsOverrides(this IConfigurationBuilder builder, string[]? args, IServiceCollection? sc)
    {
        builder.AddEnvironmentVariables("ASPNETCORE_");
        builder.AddEnvironmentVariables("DOTNET_");
        builder.AddEnvironmentVariables();
        builder.AddMountDirectorySettings(sc);

        if (args != null && args.Length > 0)
            builder.AddCommandLine(args);
    }
    
    /// <summary>
    /// Mounted settings like kubernetes secrets or configmaps
    /// </summary>
    public static IConfigurationBuilder AddMountDirectorySettings(this IConfigurationBuilder builder, IServiceCollection? sc = null)
    {
        var overrideService = sc?.BuildServiceProvider().GetService<IMountedSettingsConventionsOverride>();
        var mountOverride = overrideService?.MountedSettingsDirectory;
        if (overrideService != null)
            builder.AddObjectToJsonConfiguration(overrideService);

        builder.AddKeyPerFile(MountedSettingsConventions.KeyPerFileSettingsMountDirectory(mountOverride), true);
        var jsonDirectory = MountedSettingsConventions.JsonSettingDirectoryInfo(mountOverride);
        if (!jsonDirectory.Exists) return builder;

        foreach (var files in jsonDirectory.GetFiles())
            builder.AddJsonFile(files.FullName);

        return builder;
    }

You can easily obtain effective configuration with appSettings. Api controller is used for demonstration. Do not expose your configuration.

[ApiController]
[Route("[controller]")]
public class StatusController(IAppSettings appSettings) : ControllerBase
{
    [HttpGet]
    [ProducesResponseType(200)]
    public ActionResult Status()
    {
        return Ok(appSettings.GetDebugView().ToSummary());
    }
}

Logging

DrnProgramBase applies Serilog configurations. Console and Graylog sinks are supported by default. To configure logging you can add serilog configs in appsettings.json

{
  "Serilog": {
    "Docs": "https://github.com/serilog/serilog-settings-configuration",
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.Graylog"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft.Hosting.Lifetime": "Information",
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
          "outputTemplate": "[BEGIN {Timestamp:HH:mm:ss.fffffff} {Level:u3} {SourceContext}]{NewLine}{Message:lj}{NewLine}[END {Timestamp:HH:mm:ss.fffffff} {Level:u3} {SourceContext}]{NewLine}"
        }
      },
      {
        "Name": "Graylog",
        "Args": {
          "hostnameOrAddress": "localhost",
          "port": "12201",
          "transportType": "Udp"
        }
      }
    ]
  }
}

Kestrel

DrnProgramBase applies Kestrel configurations. To configure logging you should add kestrel configs in appsettings.json

{
  "Kestrel": {
    "Docs": "https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints",
    "EndpointDefaults": {
      "Protocols": "Http1"
    },
    "Endpoints": {
      "All": {
        "Url": "http://*:5988"
      }
    }
  }
}

DrnProgramBase RunAsync

DrnProgramBase handles most of the application level wiring and standardizes JsonDefaults across all of the System.Text.Json usages.

    protected static async Task RunAsync(string[]? args = null)
    {
        _ = JsonConventions.DefaultOptions;
        Configuration = new ConfigurationBuilder().AddDrnSettings(GetApplicationName(), args).Build();
        AppSettings = new AppSettings(Configuration);

        Log.Logger = new TProgram().ConfigureLogger().CreateBootstrapLogger().ForContext<TProgram>();
        var scopedLog = new ScopedLog().WithLoggerName(typeof(TProgram).FullName);
        try
        {
            scopedLog.AddToActions("Creating Application");
            var application = CreateApplication(args);

            scopedLog.AddToActions("Running Application");
            Log.Information("{@Logs}", scopedLog.Logs);

            await application.RunAsync();

            scopedLog.AddToActions("Application Shutdown Gracefully");
        }
        catch (Exception exception)
        {
            scopedLog.AddException(exception);
        }
        finally
        {
            if (scopedLog.HasException)
                Log.Error("{@Logs}", scopedLog.Logs);
            else
                Log.Information("{@Logs}", scopedLog.Logs);

            await Log.CloseAndFlushAsync();
        }
    }

    public static WebApplication CreateApplication(string[]? args)
    {
        var program = new TProgram();
        var options = new WebApplicationOptions
        {
            Args = args,
            ApplicationName = GetApplicationName(),
            EnvironmentName = AppSettings.Environment.ToString()
        };

        var applicationBuilder = DrnProgramConventions.GetApplicationBuilder<TProgram>(options, program.DrnProgramOptions.AppBuilderType);
        applicationBuilder.Configuration.AddDrnSettings(GetApplicationName(), args);
        program.ConfigureApplicationBuilder(applicationBuilder);
        program.AddServices(applicationBuilder.Services);

        var application = applicationBuilder.Build();
        program.ConfigureApplication(application);

        return application;
    }

DrnDefaults

DrnProgramBase has a DrnProgramOptions property which defines behavior and defaults to WebApplication and WebApplicationBuilder. See following document for new hosting model introduced with .NET 6,

DrnDefaults are added to empty WebApplicationBuilder and WebApplication and considered as sensible and configurable. Further Overriding and fine-tuning options for DrnDefaults can be added in versions after 0.3.0.

    protected DrnProgramOptions DrnProgramOptions { get;  init; } = new();

    protected abstract void AddServices(IServiceCollection services);

    protected virtual LoggerConfiguration ConfigureLogger()
        => new LoggerConfiguration().ReadFrom.Configuration(Configuration);

    protected virtual void ConfigureApplicationBuilder(WebApplicationBuilder applicationBuilder)
    {
        applicationBuilder.Host.UseSerilog();
        applicationBuilder.WebHost.UseKestrelCore().ConfigureKestrel(kestrelServerOptions =>
            kestrelServerOptions.Configure(applicationBuilder.Configuration.GetSection("Kestrel")));
        applicationBuilder.Services.ConfigureHttpJsonOptions(options => JsonConventions.SetJsonDefaults(options.SerializerOptions));
        applicationBuilder.Services.AddLogging();
        if (DrnProgramOptions.AppBuilderType != DrnAppBuilderType.DrnDefaults) return;

        var mvcBuilder = applicationBuilder.Services.AddMvc(ConfigureMvcOptions)
            .AddJsonOptions(options => JsonConventions.SetJsonDefaults(options.JsonSerializerOptions));
        var programAssembly = typeof(TProgram).Assembly;
        var partName = typeof(TProgram).GetAssemblyName();
        var applicationParts = mvcBuilder.PartManager.ApplicationParts;
        var controllersAdded = applicationParts.Any(p => p.Name == partName);
        if (!controllersAdded) mvcBuilder.AddApplicationPart(programAssembly);

        applicationBuilder.Services.AddSwaggerGen();
        applicationBuilder.Services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.All; });
        applicationBuilder.Services.PostConfigure<HostFilteringOptions>(options =>
        {
            if (options.AllowedHosts != null && options.AllowedHosts.Count != 0) return;
            var separator = new[] { ';' };
            // "AllowedHosts": "localhost;127.0.0.1;[::1]"
            var hosts = applicationBuilder.Configuration["AllowedHosts"]?.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            // Fall back to "*" to disable.
            options.AllowedHosts = hosts?.Length > 0 ? hosts : ["*"];
        });
    }

    protected virtual void ConfigureApplication(WebApplication application)
    {
        application.Services.ValidateServicesAddedByAttributes();
        if (DrnProgramOptions.AppBuilderType != DrnAppBuilderType.DrnDefaults) return;

        application.UseForwardedHeaders();
        application.UseMiddleware<HttpScopeLogger>();
        application.UseHostFiltering();

        if (DrnProgramOptions.UseHttpRequestLogger)
            application.UseMiddleware<HttpRequestLogger>();

        if (application.Environment.IsDevelopment())
        {
            application.UseSwagger();
            application.UseSwaggerUI();
        }

        application.UseRouting();
        ConfigureApplicationPreAuth(application);
        application.UseAuthentication();
        application.UseAuthorization();
        ConfigureApplicationPostAuth(application);
        application.MapControllers();
    }

    protected virtual void ConfigureApplicationPreAuth(WebApplication application)
    {

    }

    protected virtual void ConfigureApplicationPostAuth(WebApplication application)
    {

    }

    protected virtual void ConfigureMvcOptions(MvcOptions options)
    {
    }

Semper Progressivus: Always Progressive

Commit Info

Author: Duran Serkan KILIÇ
Date: 2025-08-01 16:19:04 +0300
Hash: b17fe3115a044704f257a2e3aef1a5a9210931f2

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DRN.Framework.Hosting:

Package Downloads
DRN.Framework.Testing

DRN.Framework.Testing package encapsulates testing dependencies and provides practical, effective helpers such as resourceful data attributes and test context. This package enables a new encouraging testing technique called as DTT(Duran's Testing Technique). With DTT, any developer can write clean and hassle-free unit and integration tests without complexity. ## Commit Info Author: Duran Serkan KILIÇ Date: 2025-12-13 16:05:12 +0300 Hash: 9b54af2609bb6c0568ec7d0eea0f32bfa9c23ba8

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.7.0-preview054 51 12/13/2025
0.7.0-preview053 53 12/12/2025
0.7.0-preview052 412 12/9/2025
0.7.0-preview051 288 12/7/2025
0.7.0-preview050 190 12/7/2025
0.7.0-preview049 172 11/26/2025
0.7.0-preview048 177 11/24/2025
0.7.0-preview047 127 11/7/2025
0.7.0-preview046 192 11/6/2025
0.7.0-preview045 194 11/3/2025
0.7.0-preview044 192 11/2/2025
0.7.0-preview043 129 11/1/2025
0.7.0-preview042 192 10/29/2025
0.7.0-preview041 111 10/25/2025
0.7.0-preview040 164 10/12/2025
0.7.0-preview039 115 10/11/2025
0.7.0-preview038 190 10/8/2025
0.7.0-preview037 182 9/28/2025
0.7.0-preview036 201 9/22/2025
0.7.0-preview035 178 8/31/2025
0.7.0-preview034 199 8/31/2025
0.7.0-preview033 217 8/28/2025
0.7.0-preview032 225 8/27/2025
0.7.0-preview031 174 8/10/2025
0.7.0-preview030 93 8/1/2025
0.7.0-preview029 104 8/1/2025
0.7.0-preview028 109 8/1/2025
0.7.0-preview027 150 7/31/2025
0.7.0-preview026 137 7/29/2025
0.7.0-preview025 136 7/27/2025
0.7.0-preview024 118 7/11/2025
0.7.0-preview023 128 7/11/2025
0.7.0-preview022 180 6/29/2025
0.7.0-preview021 176 6/23/2025
0.7.0-preview020 141 5/31/2025
0.7.0-preview019 165 3/23/2025
0.7.0-preview018 100 3/2/2025
0.7.0-preview017 121 2/23/2025
0.7.0-preview016 106 2/22/2025
0.7.0-preview015 87 2/21/2025
0.7.0-preview014 105 2/20/2025
0.7.0-preview013 107 2/9/2025
0.7.0-preview012 98 2/8/2025
0.7.0-preview011 102 2/2/2025
0.7.0-preview010 91 1/20/2025
0.7.0-preview009 84 1/19/2025
0.7.0-preview008 81 1/16/2025
0.7.0-preview007 83 12/29/2024
0.7.0-preview006 84 12/23/2024
0.7.0-preview005 83 11/27/2024
0.7.0-preview004 92 11/23/2024
0.7.0-preview003 144 11/20/2024
0.7.0-preview002 136 11/17/2024
0.7.0-preview001 151 11/14/2024
0.6.0 217 11/10/2024
0.6.0-preview002 150 11/10/2024
0.6.0-preview001 144 11/10/2024
0.5.1-preview002 136 9/30/2024
0.5.1-preview001 146 9/22/2024
0.5.0 181 8/30/2024
0.5.0-preview011 132 8/30/2024
0.5.0-preview010 195 8/25/2024
0.5.0-preview009 176 8/8/2024
0.5.0-preview008 145 8/7/2024
0.5.0-preview007 137 8/2/2024
0.5.0-preview006 134 7/30/2024
0.5.0-preview005 154 7/27/2024
0.5.0-preview004 160 7/15/2024
0.5.0-preview003 160 6/6/2024
0.5.0-preview002 159 6/5/2024
0.5.0-preview001 159 6/4/2024
0.4.0 165 5/19/2024
0.4.0-preview006 150 5/19/2024
0.4.0-preview005 127 5/12/2024
0.4.0-preview004 128 5/12/2024
0.4.0-preview003 141 5/11/2024
0.4.0-preview002 148 5/8/2024
0.4.0-preview001 186 5/5/2024
0.3.1-preview001 161 4/26/2024
0.3.0 179 4/23/2024
0.3.0-preview002 147 4/23/2024
0.3.0-preview001 145 4/23/2024
0.2.2-preview010 158 4/11/2024
0.2.2-preview009 159 3/18/2024
0.2.2-preview008 165 3/18/2024

Not every version includes changes, features or bug fixes. This project can increment version to keep consistency with other DRN.Framework projects.  

## Version 0.7.0

My family celebrates the enduring legacy of Mustafa Kemal Atatürk's enlightenment ideals.

### New Features

* IdentityControllerBase classes added which are controller version of Identity Api endpoints.
* DrnProgramBase
 * ConfigureSecurityHeaders virtual method added.
 * ConfigureApplicationPreScopeStart will add security headers configured by ConfigureSecurityHeaders

## Version 0.6.0

My family celebrates the enduring legacy of Mustafa Kemal Atatürk's enlightenment ideals. This release is dedicated to the memory of Mustafa Kemal Atatürk, founder of the Republic of Türkiye, and to his vision for a modern, enlightened, democratic nation. In his eternal rest, he continues to guide us through his ideals of freedom, progress, and national sovereignty.

### New Features

* DrnProgramBase
 * MvcBuilder configuration separated into virtual method
 * RazorRuntimeCompilation support added
 * Exception is no longer swallowed by DrnProgramBase to fail integration tests gracefully
* Multifactor Authentication
 * Mfa detail added to scopedlog with ScopedUserMiddleware
 * Mfa and Mfa exempt policies added with AuthPolicy helper class
 * DrnProgramBase.ConfigureAuthorizationOptions enforces Mfa by default
   * MfaExempt policy can be used with Authorize attribute to bypass mfa
   * ConfigureMFARedirection and ConfigureMFAExemption virtual methods added to DrnProgramBase
* PageCollectionBase and EndpointCollectionBase classes added to manage page and endpoint references

### Breaking Changes

* DrnProgramBase refactored
 * Static properties removed to improve application stability during integration tests
 * New overridable virtual methods added to improve configurability
 * Overridable virtual method parameters changed to accept instance parameters since static properties does not exist anymore.

## Version 0.5.0

My family celebrates the enduring legacy of Mustafa Kemal Atatürk's enlightenment ideals. This release is dedicated to August 30 Victory Day, a day that marks the decisive victory achieved by the Turkish people against imperialism during the Turkish War of Independence, leading to the establishment of the Republic of Türkiye.

### New Features

* ScopedUserMiddleware
 * sets IScopedUser with current user belongs to the request scope
 * updates IScopedLog with UserId and UserAuthenticated info
* HttpScopeHandler
 * Initializes ScopeContext with TraceId, IScopedLog and IScopedUser
 * DrnException handling as default application exception handling
 * DrnExceptions can be used to short circuit the processing pipeline
 * FlurlHttpException handling as default gateway exception handling
 * In Development environment - HttpResponse returns ScopedLog as developer exception result
 * l5d-client-id is added to scoped log by default
* HttpRequestLogger
 * Request and response logs improvements
* DrnProgramBase
 * HostOptions become configurable with Configuration.GetSection("HostOptions")
 * overrideable ConfigureSwaggerOptions
 * Added swagger support by default in development environment

### Breaking Changes

* DrnProgramBase
 * DrnProgramOptions - Removed

## Version 0.4.0

My family celebrates the enduring legacy of Mustafa Kemal Atatürk's enlightenment ideals. This release is dedicated to 19 May Commemoration of Atatürk, Youth and Sports Day.

### Breaking Changes

* HttpScopeLogger is renamed as HttpScopeHandler

### New Features

* EndpointsApiExplorer - added to service collection by DrnProgramBase to support OpenAPI Specification
* NexusClient - added for initial service discovery and remote configuration management development
* DrnProgramBase has new overridable configuration methods
 * ConfigureApplicationPreScopeStart
 * ConfigureApplicationPostScopeStart
 * MapApplicationEndpoints

## Version 0.3.0

My family celebrates the enduring legacy of Mustafa Kemal Atatürk's enlightenment ideals. This release is dedicated to 23 April National Sovereignty and Children's Day.

### New Features

* DrnProgramBase and IDrnProgram - added to minimize development efforts with sensible defaults
* HttpScopeLogger and HttpRequestLogger middlewares - added to support structured logging

---

**Semper Progressivus: Always Progressive**  
 
## Commit Info  
Author: Duran Serkan KILIÇ  
Date: 2025-08-01 16:19:04 +0300  
Hash: b17fe3115a044704f257a2e3aef1a5a9210931f2