{"id":19448442,"url":"https://github.com/metaljase/webnon-webhostingexamples","last_synced_at":"2025-04-25T02:31:09.443Z","repository":{"id":229931372,"uuid":"772132226","full_name":"metaljase/WebNon-WebHostingExamples","owner":"metaljase","description":"Examples of how .NET and ASP.NET Core apps can be hosted using Host.CreateDefaultBuilder, Host.CreateApplicationBuilder, and WebApplication.CreateBuilder.","archived":false,"fork":false,"pushed_at":"2024-09-04T14:32:20.000Z","size":44,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-05T19:42:57.164Z","etag":null,"topics":["asp-net-core","console-application","createapplicationbuilder","createbuilder","createdefaultbuilder","csharp","dotnet","dotnet-core","hosting","logging","minimal-api","options-pattern","serilog","web-api"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/metaljase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-03-14T15:41:45.000Z","updated_at":"2024-09-04T14:32:30.000Z","dependencies_parsed_at":"2024-03-27T02:27:19.005Z","dependency_job_id":"41f816c7-b2e8-40ff-a23f-6db6b9577fed","html_url":"https://github.com/metaljase/WebNon-WebHostingExamples","commit_stats":null,"previous_names":["metaljase/webnon-webhostingexamples"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metaljase%2FWebNon-WebHostingExamples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metaljase%2FWebNon-WebHostingExamples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metaljase%2FWebNon-WebHostingExamples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/metaljase%2FWebNon-WebHostingExamples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/metaljase","download_url":"https://codeload.github.com/metaljase/WebNon-WebHostingExamples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223978469,"owners_count":17235179,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","console-application","createapplicationbuilder","createbuilder","createdefaultbuilder","csharp","dotnet","dotnet-core","hosting","logging","minimal-api","options-pattern","serilog","web-api"],"created_at":"2024-11-10T16:26:50.520Z","updated_at":"2024-11-10T16:26:50.951Z","avatar_url":"https://github.com/metaljase.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# What is WebNon-WebHostingExamples?\n\nWebNon-WebHostingExamples contains four .NET 8 projects and four ASP.NET Core 8 projects, demonstrating how web apps and non-web apps can be hosted using [Host.CreateDefaultBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.host.createdefaultbuilder?view=dotnet-plat-ext-8.0), [Host.CreateApplicationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.host.createapplicationbuilder?view=dotnet-plat-ext-8.0), and [WebApplication.CreateBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.webapplication.createbuilder?view=aspnetcore-8.0).\n\nFor comparison purposes, the API examples are functionally the same, but demonstrate different hosting methods.  Likewise with the console app examples.  The source code contains comments that highlight some of the differences and similarities between the hosting methods.\n\n\nAll example hosting projects demonstrate:\n- Dependency injection.\n- Loading/overriding of app settings from appsettings.json, appsettings.\\{Environment\\}.json, user secrets, environment variables, and command-line arguments.\n- Strongly typed access to app settings using the [options pattern](https://learn.microsoft.com/en-us/dotnet/core/extensions/options), which also demonstrates how validation can be performed on app settings.\n- Logging.\n\nThe following projects use the [built-in logging provider](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging-providers) to write to the console.  They also demonstrate validating app settings using Data Annotations.\n- `Metalhead.Examples.Hosting.SimpleCDB.Api` Minimal API using `Host.CreateDefaultBuilder`.\n- `Metalhead.Examples.Hosting.SimpleCDB.ConsoleApp` Console app using `Host.CreateDefaultBuilder`.\n- `Metalhead.Examples.Hosting.SimpleCB.Api` Minimal API using `WebApplication.CreateBuilder`.\n- `Metalhead.Examples.Hosting.SimpleCAB.ConsoleApp` Console app using `Host.CreateApplicationBuilder`.\n\nThe following projects use the [Serilog logging provider](https://serilog.net/) to write to the console and file.  They also demonstrate validating app settings using IValidateOptions\\\u003cTOptions\\\u003e, and how single-dash command-line arguments can be used to override app settings.\n- `Metalhead.Examples.Hosting.CDB.Api` Minimal API using `Host.CreateDefaultBuilder`.\n- `Metalhead.Examples.Hosting.CDB.ConsoleApp` Console app using `Host.CreateDefaultBuilder`.\n- `Metalhead.Examples.Hosting.CB.Api` Minimal API using `WebApplication.CreateBuilder`.\n- `Metalhead.Examples.Hosting.CAB.ConsoleApp` Console app using `Host.CreateApplicationBuilder`.\t\t\t\n\nLog files for the console apps are written to the build output directory.  Log files for the APIs are written to the project directory.\n\n# How does it work?\nEach of the hosts demonstrated ([Host.CreateDefaultBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.host.createdefaultbuilder?view=dotnet-plat-ext-8.0), [Host.CreateApplicationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.host.createapplicationbuilder?view=dotnet-plat-ext-8.0), and [WebApplication.CreateBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.webapplication.createbuilder?view=aspnetcore-8.0)) load app settings as default.  Therefore, **it is often unnecessary to write additional code to load app settings**.\n\nThe host loads ***host*** configuration from the following sources (in order):\n1. Environment variables\n2. Command-line arguments\n\nNext, the host loads ***app*** configuration from the following sources (in order):\n1. `appsettings.json`\n2. appsettings.\\{Environment\\}.json (e.g. `appsettings.Development.json`)\n3. User secrets\n4. Environment variables\n5. Command-line arguments\n\nAny values for settings that are set in subsequent sources override values set in previous sources. For example, if 'WindSpeedUnit' is set to 'MPH' in `appsettings.json`, but is set to 'KPH' via command-line arguments, then 'KPH' will be used.\n\n# Setup instructions\n1. Clone the WebNon-WebHostingExamples repository.\n2. Open the .NET solution in Visual Studio 2022 (or a compatible alternative).\n3. Update `appsettings.json` and `appsettings.Development.json` as necessary, e.g. path to log file.\n4. Set one of the above projects as the startup project.\n5. Build the solution and run!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetaljase%2Fwebnon-webhostingexamples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmetaljase%2Fwebnon-webhostingexamples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmetaljase%2Fwebnon-webhostingexamples/lists"}