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. 中出现此错误?

4

1 回答 1

0

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

于 2017-12-20T15:20:33.787 回答