Flagsmith / flagsmith-dotnet-client

.NET Standard Client for Flagsmith. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://www.flagsmith.com/
https://www.flagsmith.com/
BSD 3-Clause "New" or "Revised" License
19 stars 12 forks source link

Not getting flags after hitting request second time from method GetIdentityFlags #76

Closed Aniketsingh1 closed 8 months ago

Aniketsingh1 commented 10 months ago

I have incorporated SDK 5.1.0 and implemented the below code in ASP.NET MVC application (not core) which is running on .NET 4.7.2.

using Flagsmith;
using System;
using System.Configuration;
using System.Threading.Tasks;

namespace FeatureFlagHelper
{
    public class FlagsmithClientHelper
    {
        private readonly IFlagsmithClient _flagsmithClient;

        public FlagsmithClientHelper()
        {
            FlagsmithConfiguration configuration = new FlagsmithConfiguration()
            {
                EnvironmentKey = ConfigurationManager.AppSettings["FlagsmithEnvironmentKey"],
                EnableClientSideEvaluation = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableClientSideEvaluation"]),
                EnvironmentRefreshIntervalSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["EnvironmentRefreshIntervalSeconds"]),
                EnableAnalytics = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableAnalytics"]),
                RequestTimeout = Convert.ToDouble(ConfigurationManager.AppSettings["RequestTimeout"]),
                Retries = Convert.ToInt32(ConfigurationManager.AppSettings["Retries"])

            };

            _flagsmithClient = new FlagsmithClient(configuration);
        }

        public async Task<bool> IsFlagEnabled(string flagName, string identity)
        {
            var flags = await this._flagsmithClient.GetIdentityFlags(identity, null);
            var flagStatus = await flags.IsFeatureEnabled(flagName.ToLower());

            return flagStatus;
        }
    }
}

I am calling this class post initializing and am trying to get the flagStatus whether it is turned on or off. Like this

 public static bool IsSomeFlagEnabled(string email)
        {
            var flagsmithClientHelper = new FlagsmithClientHelper();
            return flagsmithClientHelper.IsFlagEnabled("issomeflagenabled", "abc@gmail.com").Result;
        } 

Now this code works perfectly fine when it fetches the flag status the very first time but once the second calls comes to get the flag status this piece of code goes in wait and the debugger never returns to the next statement:

var flags = await this._flagsmithClient.GetIdentityFlags(identity, null);

After above code the debugger doesn't return to the code. it's like the thread has gone in infinite wait. I have to reset IIS and start the application again to get rid of this infinite wait issue. Please help me with the fix.

image

matthewelwell commented 10 months ago

@Aniketsingh1 please correct the formatting to ensure all code snippets are correctly formatted using backticks.

Aniketsingh1 commented 10 months ago

@matthewelwell done. Please check.

matthewelwell commented 10 months ago

Thanks, we will take a look and get back to you as soon as we are able to.

novakzaballa commented 8 months ago

Hi @Aniketsingh1, thank you for your patience. While we officially support only .Net Core, our SDK is based on the Standard .Net runtimes, so it is possible to use it with .Net Framework as well, with subtle differences in the implementation depending on the specific version. In your case, could the issue have to do with a dead-lock caused by calling your helper functions from a single thread context like the request thread? Please refer to this document https://devblogs.microsoft.com/dotnet/configureawait-faq/ to manage the async calls properly. Also, we recommend to initialize the client object as a singleton during the app initialization as per our example in the Example folder of the SDK code repo. Another potential issue is that we use HttpClient async requests to get data from the API, which it seems in .Net Framework is missing a call to the ConfigureAwait(false) method, you should be able to solve this issue by following one of the alternative recommendations in the document referred above.

novakzaballa commented 8 months ago

I will close this issue, please contact us in case of any further issues so we can re-open it if needed.