> ## Documentation Index
> Fetch the complete documentation index at: https://launchdarkly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# .NET SDK reference (server-side)

<View title="Developer" />

<View title="Federal docs" />

<View title="EU docs" />

This topic documents how to get started with the server-side .NET SDK, and links to reference information on all of the supported features.

<Note>
  **SDK quick links**

  LaunchDarkly's SDKs are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:

  <table>
    <thead>
      <tr>
        <th>Resource</th>
        <th>Location</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>SDK API documentation</td>
        <td>[SDK API docs](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/)</td>
      </tr>

      <tr>
        <td>Supported SDK Versions</td>
        <td>[.NET server SDK](/docs/sdk/concepts/supported-versions#net-server-sdk)</td>
      </tr>

      <tr>
        <td>GitHub repository</td>
        <td>[.NET (server-side) SDK](https://github.com/launchdarkly/dotnet-core/tree/main/pkgs/sdk/server)</td>
      </tr>

      <tr>
        <td>Sample application</td>
        <td>[.NET (server-side)](https://github.com/launchdarkly/hello-dotnet-server)<br /> [OpenFeature .NET (server-side)](https://github.com/launchdarkly/hello-openfeature-dotnet-server)</td>
      </tr>

      <tr>
        <td>Published module</td>
        <td>[NuGet](https://www.nuget.org/packages/LaunchDarkly.ServerSdk/)</td>
      </tr>
    </tbody>
  </table>
</Note>

<Warning>
  **For use in server-side applications only**

  This SDK is intended for use in multi-user .NET server applications. If you have a .NET application and want to set up LaunchDarkly in a mobile, desktop, or embedded application, read the [client-side .NET SDK reference](/docs/sdk/client-side/dotnet).

  To learn more about LaunchDarkly's different SDK types, read [Choosing an SDK type](/docs/sdk/concepts/client-side-server-side).
</Warning>

## Get started

<Note>
  **SDK version compatibility**

  The LaunchDarkly .NET SDK, version 8.9 and higher, is compatible with .NET 8.

  The LaunchDarkly .NET SDK, version 7.0 and higher, is compatible with .NET 6.0+, .NET Framework 4.6.2+, .NET Standard 2.0+, and .NET Core 3.1.

  Prior to version 7.0, the LaunchDarkly .NET SDK also supported .NET 5.0, .NET Framework 4.5.2 and .NET Framework 4.6.1, and .NET Core 2.1.
</Note>

After you complete the [Getting Started process](/docs/home/getting-started), follow these instructions to start using the LaunchDarkly SDK in your .NET application.

### Install the SDK

First, install the LaunchDarkly SDK as a dependency in your application using your application's dependency manager.

We recommend making the LaunchDarkly [observability plugin](/docs/sdk/observability) available as well. This plugin collects and sends observability data to LaunchDarkly. This means you can review error monitoring and logs from within the LaunchDarkly UI. It requires the .NET (server-side) SDK version 8.10 or later.

Here's how:

<CodeGroup>
  ```bash title="Shell" lines wrap theme={null}
  Install-Package LaunchDarkly.ServerSdk
  dotnet add package LaunchDarkly.Observability
  ```
</CodeGroup>

Next, import the LaunchDarkly SDK's namespaces in your application code. The namespace is not the same as the package name:

<CodeGroup>
  ```csharp title=".NET SDK v6.x and later (C#)" lines wrap theme={null}
  using LaunchDarkly.Sdk;
  using LaunchDarkly.Sdk.Server;
  using LaunchDarkly.Observability;


  // LaunchDarkly.Sdk defines general types like Context, which are also used in the client-side .NET SDK.
  // LaunchDarkly.Sdk.Server defines the LdClient and Configuration types for the server-side .NET SDK.
  // LaunchDarkly.Observability defines the optional observability,
  // which requires .NET (server-side) SDK version 8.10 or later.
  ```
</CodeGroup>

<Note>
  **The .NET (server-side) SDK uses an SDK key**

  The .NET (server-side) SDK uses an SDK key. Keys are specific to each project and environment. They are available on the **SDK keys** page under **Settings**. To learn more about key types, read [Keys](/docs/sdk/concepts/client-side-server-side#keys-and-credentials).
</Note>

### Initialize the client

After you install and import the SDK, create a single, shared instance of [`LdClient`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.LdClient.html). Specify your SDK key in the configuration to authorize your application to connect to a particular environment within LaunchDarkly.

To create a single instance:

<CodeGroup>
  ```csharp title="Initialize, .NET SDK v8.10+ with ASP.Net Core" expandable lines wrap theme={null}
  var builder = WebApplication.CreateBuilder(args);

  var config = Configuration.Builder("YOUR_SDK_KEY")
    .StartWaitTime(TimeSpan.FromSeconds(5))
    .Plugins(new PluginConfigurationBuilder()
        .Add(ObservabilityPlugin.Builder(builder.Services)
            .WithServiceName("your-service-name")
            .WithServiceVersion("example-sha")
            .Build()
        )
    ).Build();

  var client = new LdClient(config);

  // Client must be constructed before the web application.
  var app = builder.Build();
  ```

  ```csharp expandable lines wrap theme={null}
  // In Global.asax.cs
  protected void Application_Start()
  {
      // Other application specific code.
      var client = new LdClient(Configuration.Builder("YOUR_SDK_KEY")
          .StartWaitTime(TimeSpan.FromSeconds(5))
          .Plugins(new PluginConfigurationBuilder().Add(ObservabilityPlugin.Builder()
              .WithServiceName("your-service-name")
              .WithServiceVersion("example-sha")
              .Build()))
          .Build());
  }

  protected void Application_End() {
      Observe.Shutdown();
  }

  // <!-- In Web.config -->
  // <system.webServer>
  //   <!-- Any existing content should remain and the following should be added. -->
  //   <modules>
  //   <add name="TelemetryHttpModule" type="OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule,
  //     OpenTelemetry.Instrumentation.AspNet.TelemetryHttpModule" preCondition="integratedMode,managedHandler" />
  //   </modules>
  // </system.webServer>
  ```
</CodeGroup>

When you initialize the client, you can optionally provide configuration options. To learn more, read [SDK configuration](/docs/sdk/features/config#net-server-side). To learn more about the specific configuration that are available in this SDK, read [`ConfigurationBuilder`](https://launchdarkly.github.io/dotnet-core/pkgs/sdk/server/api/LaunchDarkly.Sdk.Server.ConfigurationBuilder.html).

<Warning>
  **LdClient should be a singleton**

  In the standard use case where there is only one SDK key, it's important to make the `LdClient` a singleton for each LaunchDarkly project. The client instance maintains internal state that allows LaunchDarkly to serve feature flags without making any remote requests. Do not instantiate a new client with every request.

  If you need to use more than one SDK key, the SDK allows you to create more than one `LdClient` instance. This is an uncommon requirement, but one the SDK supports.

  If you have multiple LaunchDarkly projects, you can create one `LDClient` for each. In this situation, the clients operate independently. For example, they do not share a single connection to LaunchDarkly.
</Warning>

### Evaluate a context

You can use `client` to check which variation a particular context will receive for a given feature flag. To learn more, read [Flag variation evaluation](/docs/sdk/features/evaluating) and [Flag variation evaluation details](/docs/sdk/features/evaluation-reasons). For more information about how contexts are specified, read [Context configuration](/docs/sdk/features/context-config).

In the v7 example, the context key is the string "example-context-key". In the v6 example, the user key is the string "example-user-key":

<CodeGroup>
  ```csharp title=".NET SDK v7.0+ (C#)" lines wrap theme={null}
  var context = Context.Builder("example-context-key")
    .Name("Sandy")
    .Build();

  var flagValue = client.BoolVariation("example-flag-key", context, false);

  if (flagValue) {
      // application code to show the feature
  }
  else {
      // the code to run if the feature is off
  }
  ```
</CodeGroup>

Contexts have a context kind of "user" by default. To specify a different kind, use the `Kind` method:

<CodeGroup>
  ```csharp title=".NET SDK v7.0+ (C#)" lines wrap theme={null}
  var context = Context.Builder("example-context-key")
    .Kind("device")
    .Name("Android")
    .Build();
  ```
</CodeGroup>

To learn more, read [Context kinds](/docs/home/flags/context-kinds).

## Transport Layer Security (TLS) and other networking issues

LaunchDarkly is deprecating support for TLS versions 1.0 and 1.1. .NET applications running on older operating systems are prone to use these older, less secure versions of TLS. It is possible to increase your application's security using AppContext switches.

To learn more, [read Microsoft's documentation](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls#configuring-security-via-appcontext-switches-for-net-framework-46-or-later-versions).

If you cannot update your application platform's configurations to support TLS version 1.2, you can update your application's SDK configuration to use new LaunchDarkly endpoints that support TLS versions 1.0 and later. Here is an example:

<CodeGroup>
  ```csharp title=".NET SDK v6.x and later (C#)" lines wrap theme={null}
  // Use `stream-tls10.launchdarkly.com` and `events-tls10.launchdarkly.com` TLSv1 endpoints
  var config = Configuration.Builder("YOUR_SDK_KEY")
      .ServiceEndpoints(
          Components.ServiceEndpoints()
            .Streaming("https://stream-tls10.launchdarkly.com")
            .Events("https://events-tls10.launchdarkly.com"))
      .Build();
  ```

  ```csharp title=".NET SDK v6.x and later (C#), federal instance" lines wrap theme={null}
  // Use `stream-tls10.launchdarkly.us` and `events-tls10.launchdarkly.us` TLSv1 endpoints
  var config = Configuration.Builder("YOUR_SDK_KEY")
      .ServiceEndpoints(
          Components.ServiceEndpoints()
            .Streaming("https://stream-tls10.launchdarkly.us")
            .Events("https://events-tls10.launchdarkly.us"))
      .Build();
  ```
</CodeGroup>

<Warning>
  **Potential network connectivity issues caused by DNS caching**

  LaunchDarkly servers operate in a load-balancing framework which may cause their IP addresses to change. This could result in the SDK failing to connect to LaunchDarkly if an old IP address is still in your system's DNS cache. In .NET, the DNS cache retains IP addresses for two minutes by default. If you notice intermittent connection failures that always resolve in two minutes, you may want to change this setting to a lower value as described in [Microsoft's documentation](https://learn.microsoft.com/en-us/dotnet/api/system.NET.servicepointmanager.dnsrefreshtimeout?view=netframework-4.7.2).
</Warning>

## Shut down the client

Shut down the client when your application terminates. To learn more, read [Shutting down](/docs/sdk/features/shutdown#net-server-side).

## Supported features

This SDK supports the following features:

* [SDK configuration](/docs/sdk/features/config#net-server-side), including
  * [Application metadata configuration](/docs/sdk/features/app-config#net-server-side)
  * [Migration configuration](/docs/sdk/features/migration-config#net-server-side)
  * [Service endpoint configuration](/docs/sdk/features/service-endpoint-configuration#net-server-side)
* [Anonymous contexts and users](/docs/sdk/features/anonymous#net-server-side)
* [Big segments](/docs/sdk/features/big-segments#net-server-side)
* [Context configuration](/docs/sdk/features/context-config#net-server-side)
* [Data saving mode](/docs/sdk/features/data-saving-mode#net-server-side)
* [Flag variation evaluation](/docs/sdk/features/evaluating#net-server-side)
* [Flag variation evaluation details](/docs/sdk/features/evaluation-reasons#net-server-side)
* [Flushing events](/docs/sdk/features/flush#net-server-side)
* [Getting all flags](/docs/sdk/features/all-flags#net-server-side)
* [Hooks](/docs/sdk/features/hooks#net-server-side)
* [Identifying and changing contexts](/docs/sdk/features/identify#net-server-side)
* [Logging configuration](/docs/sdk/features/logging#net-server-side)
* [Migrations](/docs/sdk/features/migrations#net-server-side)
* [Monitoring SDK status](/docs/sdk/features/monitoring#net-server-side)
* [Observability](/docs/sdk/observability/dotnet)
* [Offline mode](/docs/sdk/features/offline-mode#net-server-side)
* [OpenTelemetry](/docs/sdk/features/opentelemetry-server-side#net-server-side)
* [Private attributes](/docs/sdk/features/private-attributes#net-server-side)
* [Reading flags from a file](/docs/sdk/features/flags-from-files#net-server-side)
* [Relay Proxy configuration](/docs/sdk/features/relay-proxy-configuration)
  * [Using proxy mode](/docs/sdk/features/relay-proxy-configuration/proxy-mode#net-server-side)
  * [Using daemon mode](/docs/sdk/features/relay-proxy-configuration/daemon-mode#net-server-side)
* [Secure mode](/docs/sdk/features/secure-mode#net-server-side)
* [Tracking custom events](/docs/sdk/features/events#net-server-side)
* [Shutting down](/docs/sdk/features/shutdown#net-server-side)
* [Storing data](/docs/sdk/features/storing-data#net-server-side)
* [Subscribing to flag changes](/docs/sdk/features/flag-changes#net-server-side)
* [Test data sources](/docs/sdk/features/test-data-sources#net-server-side)
* [Web proxy configuration](/docs/sdk/features/web-proxy#net-server-side)
