{"id":18853052,"url":"https://github.com/featbit/featbit-dotnet-sdk","last_synced_at":"2025-04-14T10:22:53.406Z","repository":{"id":65832858,"uuid":"583675710","full_name":"featbit/featbit-dotnet-sdk","owner":"featbit","description":"FeatBit Server-Side SDK for .NET","archived":false,"fork":false,"pushed_at":"2025-03-21T13:15:13.000Z","size":167,"stargazers_count":15,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T23:41:11.716Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.featbit.co","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/featbit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2022-12-30T14:35:09.000Z","updated_at":"2025-03-21T13:15:15.000Z","dependencies_parsed_at":"2023-02-22T21:00:25.222Z","dependency_job_id":"9acf6a8b-dbfc-4c9e-ba10-c494df17adc4","html_url":"https://github.com/featbit/featbit-dotnet-sdk","commit_stats":{"total_commits":47,"total_committers":1,"mean_commits":47.0,"dds":0.0,"last_synced_commit":"fbf50d26d387ec99beb651dd50698641123335fc"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-dotnet-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-dotnet-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-dotnet-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/featbit%2Ffeatbit-dotnet-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/featbit","download_url":"https://codeload.github.com/featbit/featbit-dotnet-sdk/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248860278,"owners_count":21173394,"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-11-08T03:42:47.945Z","updated_at":"2025-04-14T10:22:53.390Z","avatar_url":"https://github.com/featbit.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FeatBit Server-Side SDK for .NET\n\n## Introduction\n\nThis is the .NET Server-Side SDK for the 100% open-source feature flags management\nplatform [FeatBit](https://github.com/featbit/featbit).\n\nThe FeatBit Server-Side SDK for .NET is designed primarily for use in multi-user systems such as web servers and\napplications. It is not intended for use in desktop and embedded systems applications.\n\nFor using FeatBit in *client-side* .NET applications, refer to\nour [Client-Side .NET SDK](https://github.com/featbit/featbit-dotnet-client-sdk).\n\n## Data Synchronization\n\nWe use websocket to make the local data synchronized with the FeatBit server, and then store them in memory by\ndefault. Whenever there is any change to a feature flag or its related data, this change will be pushed to the SDK and\nthe average synchronization time is less than 100 ms. Be aware the websocket connection may be interrupted due to\ninternet outage, but it will be resumed automatically once the problem is gone.\n\nIf you want to use your own data source, see [Offline Mode](#offline-mode).\n\n## Get Started\n\n### Installation\n\nThe latest stable version is available on [NuGet](https://www.nuget.org/packages/FeatBit.ServerSdk/).\n\n```sh\ndotnet add package FeatBit.ServerSdk\n```\n\nUse the `--version` option to specify\na [preview version](https://www.nuget.org/packages/FeatBit.ServerSdk/absoluteLatest) to install.\n\n### Prerequisite\n\nBefore using the SDK, you need to obtain the environment secret and SDK URLs.\n\nFollow the documentation below to retrieve these values\n\n- [How to get the environment secret](https://docs.featbit.co/sdk/faq#how-to-get-the-environment-secret)\n- [How to get the SDK URLs](https://docs.featbit.co/sdk/faq#how-to-get-the-sdk-urls)\n\n### Quick Start\n\nThe following code demonstrates basic usage of FeatBit.ServerSdk.\n\n```cs\nusing FeatBit.Sdk.Server;\nusing FeatBit.Sdk.Server.Model;\nusing FeatBit.Sdk.Server.Options;\n\n// setup SDK options\nvar options = new FbOptionsBuilder(\"\u003creplace-with-your-env-secret\u003e\")\n    .Event(new Uri(\"https://app-eval.featbit.co\"))\n    .Streaming(new Uri(\"wss://app-eval.featbit.co\"))\n    .Build();\n\n// Creates a new client instance that connects to FeatBit with the custom option.\nvar client = new FbClient(options);\nif (!client.Initialized)\n{\n    Console.WriteLine(\"FbClient failed to initialize. All Variation calls will use fallback value.\");\n}\nelse\n{\n    Console.WriteLine(\"FbClient successfully initialized!\");\n}\n\n// flag to be evaluated\nconst string flagKey = \"game-runner\";\n\n// create a user\nvar user = FbUser.Builder(\"anonymous\").Build();\n\n// evaluate a boolean flag for a given user\nvar boolVariation = client.BoolVariation(flagKey, user, defaultValue: false);\nConsole.WriteLine($\"flag '{flagKey}' returns {boolVariation} for user {user.Key}\");\n\n// evaluate a boolean flag for a given user with evaluation detail\nvar boolVariationDetail = client.BoolVariationDetail(flagKey, user, defaultValue: false);\nConsole.WriteLine(\n    $\"flag '{flagKey}' returns {boolVariationDetail.Value} for user {user.Key}. \" +\n    $\"Reason Kind: {boolVariationDetail.Kind}, Reason Description: {boolVariationDetail.Reason}\"\n);\n\n// close the client to ensure that all insights are sent out before the app exits\nawait client.CloseAsync();\n```\n\n### Examples\n\n- [Console App](/examples/ConsoleApp/Program.cs)\n- [ASP.NET Core](/examples/WebApiApp/Program.cs)\n\n## SDK\n\n### FbClient\n\nThe FbClient is the heart of the SDK which providing access to FeatBit server. Applications should instantiate a **single instance** for the lifetime of the application.\n\n#### FbClient Using Default Options\n\n```csharp\nusing FeatBit.Sdk.Server;\n\n// Creates a new client instance that connects to FeatBit with the default option.\nvar client = new FbClient(\"\u003creplace-with-your-env-secret\u003e\");\n```\n\n#### FbClient Using Custom Options\n\n```csharp\nusing FeatBit.Sdk.Server;\nusing FeatBit.Sdk.Server.Options;\nusing Microsoft.Extensions.Logging;\n\nvar consoleLoggerFactory = LoggerFactory.Create(x =\u003e x.AddConsole());\n\nvar options = new FbOptionsBuilder(\"\u003creplace-with-your-env-secret\u003e\")\n    .Streaming(new Uri(\"ws://localhost:5100\"))\n    .Event(new Uri(\"http://localhost:5100\"))\n    .StartWaitTime(TimeSpan.FromSeconds(3))\n    .LoggerFactory(consoleLoggerFactory)\n    .Build();\n\n// Creates a new client instance that connects to FeatBit with the custom option.\nvar client = new FbClient(options);\n```\n\n#### Dependency Injection\n\nWe can register the FeatBit services using standard conventions. And by default, the SDK will use the default\n`ILoggerFactory` provided by the host unless you specify a custom one.\n\n\u003e **Note**\n\u003e The `AddFeatBit` extension method will block the current thread for a maximum duration specified in `FbOptions.StartWaitTime`.\n\n```csharp\nusing FeatBit.Sdk.Server.DependencyInjection;\n\nvar builder = WebApplication.CreateBuilder(args);\nbuilder.Services.AddControllers();\n\n// add FeatBit service\nbuilder.Services.AddFeatBit(options =\u003e\n{\n    options.EnvSecret = \"\u003creplace-with-your-env-secret\u003e\";\n    options.StreamingUri = new Uri(\"wss://app-eval.featbit.co\");\n    options.EventUri = new Uri(\"https://app-eval.featbit.co\");\n    options.StartWaitTime = TimeSpan.FromSeconds(3);\n});\n\nvar app = builder.Build();\napp.Run();\n```\n\nThen the `IFbClient` interface can be obtained through dependency injection.\n\n```csharp\npublic class HomeController : ControllerBase\n{\n    private readonly IFbClient _fbClient;\n\n    public HomeController(IFbClient fbClient)\n    {\n        _fbClient = fbClient;\n    }\n}\n```\n\n#### Logging\n\nThe SDK supports standard .NET logging via [Microsoft.Extensions.Logging](https://learn.microsoft.com/dotnet/core/extensions/logging).\n\n```csharp\n// Create a Microsoft.Extensions.Logging LoggerFactory, configuring it with the providers,\n// log levels and other desired configuration as usual.\n// Take the console logger factory as an example.\nvar loggerFactory = LoggerFactory.Create(builder =\u003e builder.AddConsole());\n\n// Pass the LoggerFactory to the SDK so the SDK will use it to log messages.\nvar options = new FbOptionsBuilder(secret)\n    .LoggerFactory(consoleLoggerFactory)\n    .Build();\n```\n\nIf you're using ASP.NET Core, you can use the `AddFeatBit` extension method, which automatically uses the logger\nfactory provided by ASP.NET Core.\n\n### FbUser\n\nFbUser defines the attributes of a user for whom you are evaluating feature flags. FbUser has two built-in\nattributes: `key` and `name`. The only mandatory attribute of a FbUser is the key, which must uniquely identify each\nuser.\n\nBesides these built-in properties, you can define any additional attributes associated with the user\nusing `Custom(string key, string value)` method on `IFbUserBuilder`. Both built-in attributes and custom attributes can\nbe referenced in targeting rules, and are included in analytics data.\n\nThere is only one method for building FbUser.\n\n```csharp\nvar bob = FbUser.Builder(\"a-unique-key-of-user\")\n    .Name(\"bob\")\n    .Custom(\"age\", \"15\")\n    .Custom(\"country\", \"FR\")\n    .Build();\n```\n\n### Evaluating flags\n\nBy using the feature flag data it has already received, the SDK **locally calculates** the value of a feature flag for a\ngiven user.\n\nThere is a `Variation` method that returns a flag value, and a `VariationDetail` method that returns an object\ndescribing how the value was determined for each type.\n\n- BoolVariation/BoolVariationDetail\n- StringVariation/StringVariationDetail\n- DoubleVariation/DoubleVariationDetail\n- FloatVariation/FloatVariationDetail\n- IntVariation/IntVariationDetail\n- JsonVariation/JsonVariationDetail (in consideration)\n\n\u003e **Note**\n\u003e Since the current version does not have native support for retrieving JSON variations, you can use the `StringVariation` method as an alternative to get the JSON string.\n\nVariation calls take the feature flag key, a FbUser, and a default value. If any error makes it impossible to\nevaluate the flag (for instance, the feature flag key does not match any existing flag), default value is returned.\n\n```csharp\nusing FeatBit.Sdk.Server;\nusing FeatBit.Sdk.Server.Model;\n\n// Creates a new client instance that connects to FeatBit with the default option.\nvar client = new FbClient(\"\u003creplace-with-your-env-secret\u003e\");\n\n// The flag key to be evaluated\nconst string flagKey = \"game-runner\";\n\n// The user\nvar user = FbUser.Builder(\"anonymous\").Build();\n\n// Evaluate a boolean flag for a given user\nvar boolVariation = client.BoolVariation(flagKey, user, defaultValue: false);\nConsole.WriteLine($\"flag '{flagKey}' returns {boolVariation} for user {user.Key}\");\n\n// evaluate a boolean flag for a given user with evaluation detail\nvar boolVariationDetail = client.BoolVariationDetail(flagKey, user, defaultValue: false);\nConsole.WriteLine(\n    $\"flag '{flagKey}' returns {boolVariationDetail.Value} for user {user.Key}. \" +\n    $\"Reason Kind: {boolVariationDetail.Kind}, Reason Description: {boolVariationDetail.Reason}\"\n);\n```\n\n### Offline Mode\n\nIn some situations, you might want to stop making remote calls to FeatBit. Here is how:\n\n```csharp\nvar options = new FbOptionsBuilder()\n    .Offline(true)\n    .Build();\n\nvar client = new FbClient(options);\n```\n\nWhen you put the SDK in offline mode, no events are sent to the server and all feature flag evaluations return\nfallback values because there are no feature flags or segments available. If you want to use your own data source in\nthis case, the sdk allows users to populate feature flags and segments data from a JSON string. Here is an\nexample: [featbit-bootstrap.json](/tests/FeatBit.ServerSdk.Tests/Bootstrapping/featbit-bootstrap.json).\n\n\u003e **_NOTE:_** Populating data from a JSON string is only supported in offline mode.\n\nThe format of the data in flags and segments is defined by FeatBit and is subject to change. Rather than trying to\nconstruct these objects yourself, it's simpler to request existing flags directly from the FeatBit server in JSON format\nand use this output as the starting point for your file. Here's how:\n\n```shell\n# replace http://localhost:5100 with your evaluation server url\ncurl -H \"Authorization: \u003cyour-env-secret\u003e\" http://localhost:5100/api/public/sdk/server/latest-all \u003e featbit-bootstrap.json\n```\n\nThen use that file to initialize FbClient:\n\n```csharp\nusing FeatBit.Sdk.Server.Options;\n\nvar json = File.ReadAllText(\"featbit-bootstrap.json\");\n\nvar options = new FbOptionsBuilder()\n    .Offline(true)\n    .UseJsonBootstrapProvider(json)\n    .Build();\n\nvar client = new FbClient(options);\n```\n\n### Disable Events Collection\n\nBy default, the SDK automatically sends events (flag evaluation events and metric events for A/B testing) to the FeatBit\nserver, unless the SDK is in offline mode.\n\nIf you prefer to disable this event collection while the SDK is in online mode, you\ncan configure this behavior using the `DisableEvents` option.\n\n```csharp\nvar options = new FbOptionsBuilder()\n    .DisableEvents(true)\n    .Build();\n```\n\n### Experiments (A/B/n Testing)\n\nWe support automatic experiments for pageviews and clicks, you just need to set your experiment on our SaaS platform,\nthen you should be able to see the result in near real time after the experiment is started.\n\nIn case you need more control over the experiment data sent to our server, we offer a method to send custom event.\n\n```csharp\nclient.Track(user, eventName, numericValue);\n```\n\n**numericValue** is not mandatory, the default value is **1.0**.\n\nMake sure `track` is called after the related feature flag is called, otherwise the custom event won't be included\ninto the experiment result.\n\n## Supported .NET versions\n\nThis version of the SDK is built for the following targets:\n\n- .NET 6.0: runs on .NET 6.0 and above (including higher major versions).\n- .NET Core 3.1: runs on .NET Core 3.1+.\n- .NET Framework 4.6.2: runs on .NET Framework 4.6.2 and above.\n- .NET Standard 2.0/2.1: runs in any project that is targeted to .NET Standard 2.x rather than to a specific runtime\n  platform.\n\nThe .NET build tools should automatically load the most appropriate build of the SDK for whatever platform your\napplication or library is targeted to.\n\n\u003e **_NOTE:_** This SDK requires the `System.Text.Json` API to be available, which is included in the runtime for .NET\n\u003e Core 3.1 and later versions, but not on other platforms, so on other platforms the SDK brings\n\u003e in `System.Text.Json` as a NuGet package dependency.\n\n## Getting support\n\n- If you have a specific question about using this sdk, we encourage you\n  to [ask it in our slack](https://join.slack.com/t/featbit/shared_invite/zt-1ew5e2vbb-x6Apan1xZOaYMnFzqZkGNQ).\n- If you encounter a bug or would like to request a\n  feature, [submit an issue](https://github.com/featbit/dotnet-server-sdk/issues/new).\n\n## See Also\n\n- [Connect To .NET Sdk](https://docs.featbit.co/getting-started/connect-an-sdk#net)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeatbit%2Ffeatbit-dotnet-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffeatbit%2Ffeatbit-dotnet-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffeatbit%2Ffeatbit-dotnet-sdk/lists"}