{"id":23334058,"url":"https://github.com/simplesoft-pt/aspnetcore.middleware","last_synced_at":"2025-04-09T18:50:54.836Z","repository":{"id":75309662,"uuid":"125578373","full_name":"simplesoft-pt/AspNetCore.Middleware","owner":"simplesoft-pt","description":"Utilitary middleware for ASP.NET Core applications","archived":false,"fork":false,"pushed_at":"2020-06-25T12:41:37.000Z","size":113,"stargazers_count":4,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T15:05:56.473Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simplesoft-pt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"publiccode":null,"codemeta":null}},"created_at":"2018-03-16T23:00:34.000Z","updated_at":"2018-11-06T15:00:28.000Z","dependencies_parsed_at":"2023-07-04T15:37:15.609Z","dependency_job_id":null,"html_url":"https://github.com/simplesoft-pt/AspNetCore.Middleware","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FAspNetCore.Middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FAspNetCore.Middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FAspNetCore.Middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simplesoft-pt%2FAspNetCore.Middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simplesoft-pt","download_url":"https://codeload.github.com/simplesoft-pt/AspNetCore.Middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248092260,"owners_count":21046446,"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":[],"created_at":"2024-12-21T00:37:33.752Z","updated_at":"2025-04-09T18:50:54.808Z","avatar_url":"https://github.com/simplesoft-pt.png","language":"C#","readme":"# ASP.NET Core - Middleware\nCollection of utilitary middleware for ASP.NET Core 2+ applications.\nCheck the [documentation](https://github.com/simplesoft-pt/AspNetCore.Middleware/wiki/) for more details.\n\n## Installation\nThe collection is available via [NuGet](https://www.nuget.org/packages?q=simplesoft.aspnetcore.middleware) packages:\n\n| NuGet | Description | Version |\n| --- | --- | --- |\n| [SimpleSoft.AspNetCore.Middleware](https://www.nuget.org/packages/simplesoft.aspnetcore.middleware) | base middleware and other utility code | [![NuGet](https://img.shields.io/nuget/vpre/simplesoft.aspnetcore.middleware.svg)](https://www.nuget.org/packages/simplesoft.aspnetcore.middleware) |\n| [SimpleSoft.AspNetCore.Middleware.HealthCheck](https://www.nuget.org/packages/simplesoft.aspnetcore.middleware.healthcheck) | middleware for health check the application status, helpful for monitoring or load balance | [![NuGet](https://img.shields.io/nuget/vpre/simplesoft.aspnetcore.middleware.healthcheck.svg)](https://www.nuget.org/packages/simplesoft.aspnetcore.middleware.healthcheck) |\n| [SimpleSoft.AspNetCore.Middleware.Metadata](https://www.nuget.org/packages/simplesoft.aspnetcore.middleware.metadata) | middleware that exposes a metadata endpoint, helpul as an _is alive_ and to know the application version, environment or name | [![NuGet](https://img.shields.io/nuget/vpre/simplesoft.aspnetcore.middleware.metadata.svg)](https://www.nuget.org/packages/simplesoft.aspnetcore.middleware.metadata) |\n\n### Package Manager\n```powershell\nInstall-Package SimpleSoft.AspNetCore.Middleware\nInstall-Package SimpleSoft.AspNetCore.Middleware.HealthCheck\nInstall-Package SimpleSoft.AspNetCore.Middleware.Metadata\n```\n\n### .NET CLI\n```powershell\ndotnet add package SimpleSoft.AspNetCore.Middleware\ndotnet add package SimpleSoft.AspNetCore.Middleware.HealthCheck\ndotnet add package SimpleSoft.AspNetCore.Middleware.Metadata\n```\n## Compatibility\nThe middlewares were implemented having ASP.NET Core 2 in mind, so they support `.NETStandard 2.0` and up.\n\n## Examples\nSimple code snippets showing how to use the middlewares. For a more detailed usage, just check the wiki.\n\n### Metadata [[wiki]](https://github.com/simplesoft-pt/AspNetCore.Middleware/wiki/Metadata)\n```csharp\n[assembly: AssemblyVersion(\"1.0.0.0\")]\n[assembly: AssemblyFileVersion(\"1.2.0.18001\")]\n[assembly: AssemblyProduct(\"1.2.0-rc01\")]\n\nnamespace ExampleApi\n{\n    public class Startup\n    {   \n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\n        {\n            app.UseMetadata(new MetadataOptions\n            {\n                BeforeInvoke = ctx =\u003e\n                {\n                    if (!\"localhost\".Equals(ctx.Request.Host.Host))\n                        ctx.Abort();\n                    return Task.CompletedTask;\n                },\n                IndentJson = env.IsDevelopment(),\n                Environment = env.EnvironmentName\n            });\n        }\n    }\n}\n```\n```json\n//    GET http://localhost:5000/api/_meta\n//    200 OK\n{\n    \"name\": \"ExampleApi\",\n    \"environment\": \"Development\",\n    \"startedOn\": \"2018-04-11T23:13:33.0606389+01:00\",\n    \"version\": {\n        \"major\": 1,\n        \"minor\": 2,\n        \"patch\": 0,\n        \"revision\": 18001,\n        \"alias\": \"1.2.0.rc01\"\n    }\n}\n```\n\n### Health Checks [[wiki]](https://github.com/simplesoft-pt/AspNetCore.Middleware/wiki/HealthCheck)\n```csharp\nnamespace ExampleApi\n{\n    public class Startup\n    {\n        public void ConfigureServices(IServiceCollection services)\n        {\n            //  needed for middleware routes\n            services.AddRouting();\n\n            //  needed if using cached health checks\n            services.AddMemoryCache();\n\n            services.AddHealthCheck(builder =\u003e\n            {\n                builder.AddSql(\"db-sql-server\",\n                    () =\u003e new SqlConnection(\"Data Source=localhost;Database=Master;Integrated Security=true\"),\n                    \"SELECT 1\", true, \"sql-server\");\n\n                builder.AddSql(\"db-mysql\",\n                    p =\u003e new MySqlConnection(\"Server=localhost;Database=mysql;Integrated Security=yes\"),\n                    \"SELECT 1\", false, \"mysql\");\n\n                builder.AddCached(cachedBuilder =\u003e\n                {\n                    cachedBuilder.AddHttp(\"http-stat-200\",\n                        \"https://httpstat.us/200\", 2000, true, true, \"httpstat\");\n\n                    cachedBuilder.AddHttp(\"http-stat-500\",\n                        p =\u003e \"https://httpstat.us/500\", 2000, true, true, \"httpstat\");\n\n                    cachedBuilder.AddHttp(\"http-stat-timeout\",\n                        new Uri(\"https://httpstat.us/200?sleep=5000\"), 2000, true, true, \"httpstat\");\n                }, TimeSpan.FromSeconds(30));\n\n                builder.AddDelegate(\"delegate\", async ct =\u003e\n                {\n                    await Task.Delay(200, ct);\n                    if (DateTimeOffset.Now.Millisecond % 3 == 0)\n                        throw new Exception(\"Example health check exception\");\n                    return HealthCheckStatus.Green;\n                }, false, \"custom\");\n            });\n        }\n        \n        public void Configure(IApplicationBuilder app, IHostingEnvironment env)\n        {\n            app.UseHealthCheck(new HealthCheckOptions\n            {\n                BeforeInvoke = ctx =\u003e\n                {\n                    if (\"localhost\".Equals(ctx.Request.Host.Host))\n                        return Task.CompletedTask;\n                    \n                    if (ctx.User.Identity.IsAuthenticated)\n                    {\n                        if (ctx.User.IsInRole(\"admin\"))\n                            return Task.CompletedTask;\n\n                        ctx.Response.StatusCode = 403;\n                        return ctx.Response.WriteAsync(\"Forbidden\");\n                    }\n\n                    ctx.Response.StatusCode = 401;\n                    return ctx.Response.WriteAsync(\"Unauthorized\");\n                },\n                Path = \"_health\",\n                IndentJson = env.IsDevelopment(),\n                StringEnum = true\n            });\n        }\n    }\n}\n```\n```json\n//    GET http://localhost:5000/api/_health\n//    500 Internal Server Error\n{\n  \"status\": \"red\",\n  \"startedOn\": \"2018-04-11T23:42:26.54719+01:00\",\n  \"terminatedOn\": \"2018-04-11T23:42:28.7647715+01:00\",\n  \"dependencies\": {\n    \"db-sql-server\": {\n      \"status\": \"green\",\n      \"required\": true,\n      \"tags\": [\n        \"sql-server\",\n        \"database\",\n        \"sql\"\n      ]\n    },\n    \"db-mysql\": {\n      \"status\": \"red\",\n      \"required\": false,\n      \"tags\": [\n        \"mysql\",\n        \"database\",\n        \"sql\"\n      ]\n    },\n    \"http-stat-200\": {\n      \"status\": \"green\",\n      \"required\": true,\n      \"tags\": [\n        \"cached\",\n        \"httpstat\",\n        \"http\"\n      ]\n    },\n    \"http-stat-500\": {\n      \"status\": \"red\",\n      \"required\": true,\n      \"tags\": [\n        \"cached\",\n        \"httpstat\",\n        \"http\"\n      ]\n    },\n    \"http-stat-timeout\": {\n      \"status\": \"red\",\n      \"required\": true,\n      \"tags\": [\n        \"cached\",\n        \"httpstat\",\n        \"http\"\n      ]\n    },\n    \"delegate\": {\n      \"status\": \"green\",\n      \"required\": false,\n      \"tags\": [\n        \"custom\"\n      ]\n    }\n  }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplesoft-pt%2Faspnetcore.middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimplesoft-pt%2Faspnetcore.middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimplesoft-pt%2Faspnetcore.middleware/lists"}