如何在 .Net Core 中使用 Feature Toggle Nuget 包启用功能开关?

4

以下是我在应用程序中所做的更改。在代码中添加了FeatureToggle包。并创建了一个新的打印类(仅作为示例类),扩展SimpleFeatureToggle。

using FeatureToggle;

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

appSettings.json添加了featureToggle关键字,并且设置为true。因此Printing类将读取它并启用特性切换。

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

在Startup.cs文件的ConfigureServices方法中注册了我的打印服务,并将配置文件(appSettings.json)传递给了打印类。

   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();
    }

// Mapper类中,检查功能开关是否启用并启用相关功能。

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
        }
    }
}

我在上面的代码中犯了哪些错误?为什么我的代码会出现System.FileIOException的错误?

1个回答

0
我建议您在寻求帮助时包含错误信息。我的猜测是,如果您刚创建了一个项目,那么 appsetting.json 文件很可能不在 bin 文件夹中。您可以调查一下,或者只需右键单击 appsettings.json 文件,选择属性,然后将“复制到输出目录”更改为“始终复制”。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接