DRN.Framework.Hosting
0.5.0-preview003
Prefix Reserved
See the version list below for details.
dotnet add package DRN.Framework.Hosting --version 0.5.0-preview003
NuGet\Install-Package DRN.Framework.Hosting -Version 0.5.0-preview003
<PackageReference Include="DRN.Framework.Hosting" Version="0.5.0-preview003" />
paket add DRN.Framework.Hosting --version 0.5.0-preview003
#r "nuget: DRN.Framework.Hosting, 0.5.0-preview003"
// Install DRN.Framework.Hosting as a Cake Addin #addin nuget:?package=DRN.Framework.Hosting&version=0.5.0-preview003&prerelease // Install DRN.Framework.Hosting as a Cake Tool #tool nuget:?package=DRN.Framework.Hosting&version=0.5.0-preview003&prerelease
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 Progredi: Always Progressive
Commit Info
Author: Duran Serkan KILIÇ
Date: 2024-06-06 21:36:58 +0300
Hash: fdca99ce9c130f9468cd19406d2a878b55ef7ba8
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- DRN.Framework.Utils (>= 0.5.0-preview003)
- Serilog.AspNetCore (>= 8.0.1)
- Serilog.Settings.Configuration (>= 8.0.0)
- Serilog.Sinks.Graylog (>= 3.1.1)
- Swashbuckle.AspNetCore (>= 6.6.2)
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: 2024-11-27 22:45:36 +0300 Hash: 53bc7797be574a95ac2f526262817302182e3f7f |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.7.0-preview005 | 0 | 11/27/2024 |
0.7.0-preview004 | 44 | 11/23/2024 |
0.7.0-preview003 | 74 | 11/20/2024 |
0.7.0-preview002 | 69 | 11/17/2024 |
0.7.0-preview001 | 74 | 11/14/2024 |
0.6.0 | 87 | 11/10/2024 |
0.6.0-preview002 | 81 | 11/10/2024 |
0.6.0-preview001 | 78 | 11/10/2024 |
0.5.1-preview002 | 80 | 9/30/2024 |
0.5.1-preview001 | 82 | 9/22/2024 |
0.5.0 | 119 | 8/30/2024 |
0.5.0-preview011 | 70 | 8/30/2024 |
0.5.0-preview010 | 121 | 8/25/2024 |
0.5.0-preview009 | 108 | 8/8/2024 |
0.5.0-preview008 | 88 | 8/7/2024 |
0.5.0-preview007 | 83 | 8/2/2024 |
0.5.0-preview006 | 75 | 7/30/2024 |
0.5.0-preview005 | 95 | 7/27/2024 |
0.5.0-preview004 | 95 | 7/15/2024 |
0.5.0-preview003 | 105 | 6/6/2024 |
0.5.0-preview002 | 104 | 6/5/2024 |
0.5.0-preview001 | 105 | 6/4/2024 |
0.4.0 | 117 | 5/19/2024 |
0.4.0-preview006 | 86 | 5/19/2024 |
0.4.0-preview005 | 81 | 5/12/2024 |
0.4.0-preview004 | 70 | 5/12/2024 |
0.4.0-preview003 | 82 | 5/11/2024 |
0.4.0-preview002 | 87 | 5/8/2024 |
0.4.0-preview001 | 109 | 5/5/2024 |
0.3.1-preview001 | 104 | 4/26/2024 |
0.3.0 | 115 | 4/23/2024 |
0.3.0-preview002 | 97 | 4/23/2024 |
0.3.0-preview001 | 91 | 4/23/2024 |
0.2.2-preview010 | 102 | 4/11/2024 |
0.2.2-preview009 | 104 | 3/18/2024 |
0.2.2-preview008 | 114 | 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.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.
### Breaking Changes
### New Features
* DrnProgramBase and IDrnProgram added to minimize development efforts with sensible defaults
* HttpScopeLogger and HttpRequestLogger middlewares added to support structured logging
### Bug Fixes
---
**Semper Progredi: Always Progressive**
## Commit Info
Author: Duran Serkan KILIÇ
Date: 2024-06-06 21:36:58 +0300
Hash: fdca99ce9c130f9468cd19406d2a878b55ef7ba8