0

I have the following code that works fine as is - aka no errors and it builds / runs:

if (featureManager.IsEnabledAsync("FeatureToggleABC").Result)
{
    backgroundTask.Run<NewToggleUserTask>(u => u.AddUserIntoGroup(userName, groupName, connParam));
    return "Running task with new toggle";
}

Per Microsoft, it's best NOT to use strings for toggle names incase we finger fumble. It will just assume that the name is still a legit toggle, and set the value to false.

So I've created an enum like this:

namespace Widgets.Enums
{
    public enum WidgetFeatureFlags
    {
        FeatureToggleABC
    }
}

And i've tried to change the if statement to look like this:

if (featureManager.IsEnabledAsync(nameof(WidgetFeatureFlags.FeatureToggleABC)).Result)
{
    backgroundTask.Run<NewToggleUserTask>(u => u.AddUserIntoGroup(userName, groupName, connParam));
    return "Running task with new toggle";
}

But I'm getting an error that says:

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'bool'

This works:

var featureName = nameof(WidgetFeatureFlags.FeatureToggleABC);
if (featureManager.IsEnabledAsync(featureName).Result)
{

I'm sure it's something simple I've missed but I haven't been able to resolve it. I've tried casting the results but so far no dice.

EDIT 1

Method signatures:

    Task<bool> IsEnabledAsync(string feature);

And the method that calls IsEnabledAsync:

 public string GetUserMember(string userName, string groupName, string tName)

Edit 2

The featureManager is created using this IFeatureManager class:

namespace Microsoft.FeatureManagement
{
    //
    // Summary:
    //     Used to evaluate whether a feature is enabled or disabled.
    public interface IFeatureManager
    {

     IAsyncEnumerable<string> GetFeatureNamesAsync();
     Task<bool> IsEnabledAsync(string feature); 
     Task<bool> IsEnabledAsync<TContext>(string feature, TContext context);
    }
}
Wyck
  • 10,311
  • 6
  • 39
  • 60
dot
  • 14,928
  • 41
  • 110
  • 218
  • 2
    As an alternative, you could replace the enum with a (static) class containing string constants. This would eliminate the need for `nameof`. – Markus Jul 26 '22 at 14:25
  • 1
    The error has nothing to do with `nameof` or `enums`. The code should use `if(await featureManager.IsEnabledAsync(...))` instead of blocking anyway. That `.Result` is a bug – Panagiotis Kanavos Jul 26 '22 at 14:27
  • The error complains that a `Task` is used where a `bool` was expected. Not even a `Task` but a `Task`. What is the signature of `IsEnabledAsync` *and its overloads*? – Panagiotis Kanavos Jul 26 '22 at 14:28
  • @PanagiotisKanavos although the code is calling an async method, the function that's making the call isn't async. so ... adding the "await" is incorrect / causes other errors. – dot Jul 26 '22 at 14:29
  • @PanagiotisKanavos per your request - please check out Edit 1. Thanks! – dot Jul 26 '22 at 14:30
  • It's *still* got nothing to do with `nameof` or enums. It's just about calling an async method from a sync method. Is there definitely not an `IsEnabled` method? – Jon Skeet Jul 26 '22 at 14:33
  • 2
    Make it asynchronous then. In any case the error has nothing to do with `nameof`. What is the *full* compilation error and where does it appear? The error says you tried to use a `Task` where a `bool` was expected. The method you posted doesn't return a `Task`. Do you have another method with the same name? Created a similar extension method perhaps? Post enough code so *we* can replicate the error – Panagiotis Kanavos Jul 26 '22 at 14:33
  • Indeed, a [mcve] would be really helpful here. – Jon Skeet Jul 26 '22 at 14:35
  • What library are you using? `Micorosft.FeatureManagement` is an Azure SDK component with a pre-release warning. Even so it should be easy to just add it to a Console application and write 2-3 lines that replicate the error. Since it's a pre-release you'll have to specify the version you used or even better, include your `csproj` – Panagiotis Kanavos Jul 26 '22 at 14:35
  • Besides, if it's a preview it may change at any time. `Preview` isn't even a `Beta`. The signature may have changed – Panagiotis Kanavos Jul 26 '22 at 14:37
  • 1
    I know you said that you're calling this async method from one which is not async, but the point is that async code is viral, that is to say once you start it needs to go all the way up the chain of calls...... you cant just throw a `.Result` in there and not have consequences. Not the cause of this error as others have said, but worth keeping in mind – Jamiec Jul 26 '22 at 14:42
  • thanks everyone for weighing in. I can change the signature of this method to be async. But question. Until i decided to refactor the code ... there were no errors. And also ... saving the name of the enum into a separate string variable first, and then calling the if statement also fails to show the same errors. why is that? – dot Jul 26 '22 at 14:44
  • 3
    Again, a [mcve] is really quite necessary to answer this. – Jamiec Jul 26 '22 at 14:48
  • 1
    Here's an example which compiles with no issues for me (in VS rather than vscode, but I'd be surprised if that were the issue): https://gist.github.com/jskeet/bd68551fb00a9bd2e959339686a199b1 (I've turned off nullable reference types for simplicity.) The only way I can reproduce the issue is to remove `.Result`. – Jon Skeet Jul 26 '22 at 14:51
  • To rule vscode out, please try compiling from the console as well. If you can provide a [mcve] which demonstrates the problem with just "dotnet build" it will be much easier to help you. – Jon Skeet Jul 26 '22 at 14:56
  • So I'll post back/ update the question with more code in a few. I'm still navigating the code trying to understand what's what - not my code, but something I've inherited. – dot Jul 26 '22 at 14:57

0 Answers0