{"id":18894654,"url":"https://github.com/ademcatamak/scopestate","last_synced_at":"2026-06-24T21:31:25.138Z","repository":{"id":116948239,"uuid":"243015822","full_name":"AdemCatamak/ScopeState","owner":"AdemCatamak","description":"Scope State Management for .Net","archived":false,"fork":false,"pushed_at":"2020-03-10T14:17:56.000Z","size":58,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-29T11:43:29.445Z","etag":null,"topics":["http-interceptor","masstransit-observer","middleware","scope","tracing"],"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/AdemCatamak.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,"publiccode":null,"codemeta":null}},"created_at":"2020-02-25T14:10:57.000Z","updated_at":"2021-01-06T09:46:06.000Z","dependencies_parsed_at":null,"dependency_job_id":"815799b6-6f83-4318-b377-7662eebe393a","html_url":"https://github.com/AdemCatamak/ScopeState","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AdemCatamak/ScopeState","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdemCatamak%2FScopeState","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdemCatamak%2FScopeState/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdemCatamak%2FScopeState/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdemCatamak%2FScopeState/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AdemCatamak","download_url":"https://codeload.github.com/AdemCatamak/ScopeState/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdemCatamak%2FScopeState/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34750952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-24T02:00:07.484Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["http-interceptor","masstransit-observer","middleware","scope","tracing"],"created_at":"2024-11-08T08:23:58.686Z","updated_at":"2026-06-24T21:31:25.133Z","avatar_url":"https://github.com/AdemCatamak.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ScopeState\n\n**Appveyor**\n![AppVeyor](https://img.shields.io/appveyor/ci/ademcatamak/scopestate.svg) ![AppVeyor tests](https://img.shields.io/appveyor/tests/ademcatamak/scopestate.svg)\n\n**Travis**\n![Travis (.com)](https://travis-ci.com/AdemCatamak/scopestate.svg?branch=master)\n\n**GitHub**\n![.github/workflows/github.yml](https://github.com/AdemCatamak/scopestate/workflows/.github/workflows/github.yml/badge.svg?branch=master)\n\n\n**_ScopeState \u0026\u0026 ScopeState.NetCoreDIExtensions_**\n\nScopeState library provides storage for data that may be necessary for your application life cycle. \n\nFor example; when WebRequest arrives your Web-Api, you can storage information that is taken from RequestHeader via IScopeStateAccessor. Thanks to this, you do not have to pass HttpContext or HttpRequest to other layers.\n\nIn ScopeState.NetCoreDIExtensions package, there is a parameterless function that inject BasicScopeStateAccesor to system. This accessor gives BasicScopeState which has only `TraceId` property.\n```\nservices.AddScopeStateAccessor();\n```\n\nIf you want to declare your custom `ScopeState`, you can look at example below.\n```\npublic class AppScopeState : BaseScopeState\n{\n    public CultureInfo Culture { get; set; }\n}\n\npublic class AppScopeStateAccessor : BaseScopeStateAccessor\u003cAppScopeState\u003e\n{\n}\n```\n```\nservices.AddScopeStateAccessor\u003cAppScopeStateAccessor, AppScopeState\u003e();\n```\n\n**_ScopeState.WebMiddleware_**\n\nDefault middleware could work with `IScopeStateAccessor\u003cBasicScopeState\u003e`. This middleware check is there any information exist in `x-trace-id` RequestHeader. If header has value, header's value is set into `BasicScopeState's TraceId` otherwise random value is stored into `BasicScopeState's TraceId` \n```\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n    ...\n    app.UseScopeStateMiddleware();\n    ...\n}\n```\nIf you define your own ScopeState object, you should give procedure how your ScopeState does created.\n\n```\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n    ...\n    app.UseScopeStateMiddleware\u003cAppScopeState\u003e(provider =\u003e provider.GetService\u003cIScopeStateAccessor\u003cAppScopeState\u003e\u003e(),\n                                                           httpContext =\u003e\n                                                           {\n                                                           AppScopeState appScopeState = new AppScopeState();\n                                                                   \n                                                           CultureInfo cultureInfo = CultureInfo.InvariantCulture;\n                                                           if (!httpContext.Request.Headers.TryGetValue(\"Accept-Language\", out StringValues languageName))\n                                                           {\n                                                               try\n                                                               {\n                                                                   cultureInfo = new CultureInfo(languageName);\n                                                               }\n                                                               catch (Exception)\n                                                               {\n                                                                   cultureInfo = CultureInfo.CurrentCulture;\n                                                               }\n                                                           }\n    \n                                                           appScopeState.Culture = cultureInfo;\n                                                                   \n                                                           if (httpContext.Request.Headers.TryGetValue(\"x-trace-id\", out StringValues traceId))\n                                                           {\n                                                               appScopeState.TraceId = traceId;\n                                                           }\n    \n                                                           return appScopeState;\n                                                           }\n    ...                                              \n}\n```\n\n\n**_ScopeState.MassTransitMiddleware_**\n\nConnectPublishObserver and ConnectSendObserver take ScopeState object via IScopeStateAccessor. After than ScopeState.TraceId value is set into headers as `x-trace-id`.\n```\nbusControl.ConnectPublishObserver(new BasicPublishObserver(provider.GetService\u003cILogger\u003cBasicPublishObserver\u003e\u003e()));\nbusControl.ConnectSendObserver(new BasicSendObserver(provider.GetService\u003cILogger\u003cBasicSendObserver\u003e\u003e()));\n```\n\nThere is an example of using pipeline below if you have own ScopeState model.\n```\nstatic void PreSend(SendContext context, AppScopeState scopeState)\n{\n  if (context.Headers == null) return;\n\n  string cultureName = scopeState?.Culture?.Name;\n  if (!string.IsNullOrEmpty(cultureName) \u0026\u0026 !context.Headers.TryGetHeader(\"x-culture-name\", out object _))\n      context.Headers.Set(\"x-culture-name\", cultureName);\n\n  string traceId = scopeState?.TraceId;\n  if (!string.IsNullOrEmpty(traceId) \u0026\u0026 !context.Headers.TryGetHeader(\"x-trace-id\", out object _))\n      context.Headers.Set(\"x-trace-id\", traceId);\n}\n\nbusControl.UseScopeStatePublishPipeline(provider.GetService\u003cIScopeStateAccessor\u003cAppScopeState\u003e\u003e(),\n                                        PreSend);\nbusControl.UseScopeStateSendPipeline(provider.GetService\u003cIScopeStateAccessor\u003cAppScopeState\u003e\u003e(),\n                                     PreSend);\n```\nIn ConsumePipeline, message's `x-trace-id` header is set into ScopeState.TraceId if header is not empty. Otherwise, random value is set into ScopeState.TraceId.\n```\nbusControl.ConnectConsumeObserver(new BasicConsumeObserver(provider.GetService\u003cILogger\u003cBasicConsumeObserver\u003e\u003e()));\n```\nThere is an example of using pipeline below if you have own ScopeState model.\n\n```\nbusControl.UseScopeStateConsumePipeline(provider.GetService\u003cIScopeStateAccessor\u003cAppScopeState\u003e\u003e(),\n                                        context =\u003e\n                                        {\n                                            var cultureScopeState = new AppScopeState();\n                                            if (context.Headers == null) return cultureScopeState;\n\n                                            CultureInfo cultureInfo = CultureInfo.InvariantCulture;\n                                            if (context.Headers.TryGetHeader(\"x-culture-name\", out object languageNameObj))\n                                            {\n                                                var languageName = languageNameObj.ToString();\n                                                try\n                                                {\n                                                    cultureInfo = new CultureInfo(languageName);\n                                                }\n                                                catch (Exception)\n                                                {\n                                                    cultureInfo = CultureInfo.CurrentCulture;\n                                                }\n                                            }\n\n                                            string traceId = null;\n                                            if (context.Headers.TryGetHeader(\"x-trace-id\", out object traceIdObj))\n                                            {\n                                                traceId = traceIdObj.ToString();\n                                            }\n\n                                            cultureScopeState.Culture = cultureInfo;\n                                            if (!string.IsNullOrEmpty(traceId))\n                                                cultureScopeState.TraceId = traceId;\n\n                                            return cultureScopeState;\n});\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fademcatamak%2Fscopestate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fademcatamak%2Fscopestate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fademcatamak%2Fscopestate/lists"}