加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

如何使用Feature Toggle Nuget Package在.Net Core中启用功能切

发布时间:2020-12-16 09:32:55 所属栏目:asp.Net 来源:网络整理
导读:以下是我在申请中所做的更改.在代码中添加了FeatureToggle包.并创建了新的打印类(仅用于示例类)扩展SimpleFeatureToggle. using FeatureToggle;namespace AspDotNetCoreExample.Models{ public class Printing : SimpleFeatureToggle {}} appSettings.json添
以下是我在申请中所做的更改.在代码中添加了FeatureToggle包.并创建了新的打印类(仅用于示例类)扩展SimpleFeatureToggle.

using FeatureToggle;

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

appSettings.json添加了featureToggle Key,并将其设置为true.因此,Printing类将读取它并启用功能切换.

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

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

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中收到此错误.?

解决方法

我建议你在下次尝试获得帮助时加入错误.我的猜测是,如果您刚刚创建了一个项目,那么appsetting.json文件可能不在bin文件夹中.您可以对此进行调查,也可以右键单击appsettings.json文件,选择属性,然后将“复制到输出目录”更改为“始终复制”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读