LaunchDarkly Example Doesn't Work in .NET Class Library but Does in a ConsoleApp

120 Views Asked by At

I'm trying to integrate LaunchDarkly into my project. I'm seeing a problem where the LdClient connects in one version and doesn't in another. The code is essentially identical. I got their example running via a simple Console app in .NET Framework 4.8 using the Server SDK. You can find a version of it here. This is what my working project looks like:

using System;
using System.Net.Sockets;
using System.Runtime.Remoting.Contexts;
using LaunchDarkly.Sdk;
using LaunchDarkly.Sdk.Server;
using Context = LaunchDarkly.Sdk.Context;
namespace HelloDotNet
{
    class Program
    {
        // Set SdkKey to your LaunchDarkly SDK key.
        public const string SdkKey = "my-sdk-key";
        // Set FeatureFlagKey to the feature flag key you want to evaluate.
        public const string FeatureFlagKey = "my-flag";
        private static void ShowMessage(string s)
        {
            Console.WriteLine("*** " + s);
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            var ldConfig = Configuration.Default(SdkKey);
            var client = new LdClient(ldConfig);
            if (client.Initialized)
            {
                ShowMessage("SDK successfully initialized!");
            }
            else
            {
                ShowMessage("SDK failed to initialize");
                Environment.Exit(1);
            }
            // Set up the evaluation context. This context should appear on your LaunchDarkly contexts
            // dashboard soon after you run the demo.
            var context = Context.Builder("test")
                .Name("Sandy")
                .Build();
            var flagValue = client.BoolVariation(FeatureFlagKey, context, false);
            ShowMessage(string.Format("Feature flag '{0}' is {1} for this context",
                FeatureFlagKey, flagValue));
            // Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
            // events to LaunchDarkly before the program exits. If analytics events are not delivered,
            // the context attributes and flag usage statistics will not appear on your dashboard. In
            // a normal long-running application, the SDK would continue running and events would be
            // delivered automatically in the background.
            client.Dispose();

            Console.ReadKey();
        }
    }
}

However, if I then transfer that code to just a class in a ClassLibrary, the LdClient never connects and client.Initalized is always false. In turn, this means I can never see my flags toggle. For example, the client in this Singleton never connects:

using LaunchDarkly.Sdk.Server;
using System;
using LaunchDarkly.Sdk;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Context = LaunchDarkly.Sdk.Context;

namespace FeatureFlags
{ 
    public class FeatureFlagsManager
    {
        //Load from something better in the future, hardcoded for now
        private readonly string _sdkKey = "my-sdk-key";
        private Configuration _ldConfig;
        private LdClient _ldClient;
        private Context _context;
        private static readonly Lazy<FeatureFlagsManager> lazy = new Lazy<FeatureFlagsManager>(() => new FeatureFlagsManager());
        public static FeatureFlagsManager Instance => lazy.Value;

        private FeatureFlagsManager()
        {
            _ldConfig = Configuration.Default(_sdkKey);
            _ldClient = new LdClient(_ldConfig);

            //load key and name from something in the future
            _context = Context.Builder("test").Name("Sandy").Build();
        }

        public bool GetFeatureFlagBool(string featureFlagKey)
        {
            bool enabled = _ldClient.BoolVariation(featureFlagKey, _context, false);
            return enabled;
        }
    }
}

I also observe that if I turn the first, working example into a class library and put the contents of main into a method and try to run the method, the client does not initialize.

1

There are 1 best solutions below

1
Reza Kajbaf On

I had the same issue. After much gnashing of teeth, it turned out to be a bug in the latest version and specific to .NET Framework 4.8. I downgraded my NuGet package to 7.1.0 and it worked again.

I submitted this issue to LaunchDarkly.

https://github.com/launchdarkly/dotnet-server-sdk/issues/184

Hope this helps!