How to enable Toggle function in .Net Core Using Toggle Nuget Package function?

Below are the changes I made to my application. Added FeatureToggle package in code. And created a new print class (class class only) Extension SimpleFeatureToggle.

using FeatureToggle;

namespace AspDotNetCoreExample.Models
{
    public class Printing : SimpleFeatureToggle {}
}

      

appSettings.json added featureToggle Key and it is set to true. So the Printing class will read it and enable the toggle function.

    {
  "FeatureToggle": {
    "Printing": "true"
  }
  }       

      

** Startup.cs Registered my print service in the ConfigureServices method. I passed the config file (appSettings.json) to the print class. In the meantime, there is no need to worry about it. ”

   public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // Set provider config so file is read from content root path
        var provider = new AppSettingsProvider { Configuration = Configuration };
        services.Configure<AppSettings(Configuration.GetSection("AppSettings"));
        services.AddSingleton(new Printing { ToggleValueProvider = provider });       
        services.AddTransient<IMapper, Mapper>();
        // Add framework services.
        services.AddMvc();
    }

      

// In the Mapper class to enable the toggle feature or not and to enable the features.

namespace STAR.Marketing.Portal.Web
{
    public class Mapper: Mapper
    {
        private readonly Printing _print;

        public Mapper(Printing print) //Dependency Injection to get Printing
        {
            _print = print;
        }
        public string Enabled()
        {
            return _print.FeatureEnabled;   // To check the feature is enabled but getting the Error System.FileIOException
        }
    }
}

      

What are the mistakes I made in the above code? Why am I getting this error in my code, System.FileIOException.?

+3


source to share


1 answer


I would suggest including the error the next time you try to get help. My guess is that if you just created the project, the appsetting.json file is probably not in the bin folder. You can investigate this, or just right click on the appsettings.json file, select properties and change "Copy to Output Directory" to "Always Copy".



0


source







All Articles