代码之家  ›  专栏  ›  技术社区  ›  Souvik Ghosh

从不同的项目访问appsettings.json

  •  2
  • Souvik Ghosh  · 技术社区  · 8 年前

    我正在使用.NET核心2.1创建Web API。我的解决方案包含不同的项目,在主/启动项目中有一个appsettings.json文件。 我想从不同的项目中读取AppSettings。为此,我在一个公共项目中创建了一个类,该项目引用了其他所有项目。

    namespace Common
    {
        public sealed class ApplicationSettings
        {
            public ApplicationSettings(IConfiguration configuration)
            {
                InitSettings(configuration);
            }
    
            public string CloudStorageConnectionString { get; private set; }
    
            private void InitSettings(IConfiguration configuration)
            {
                CloudStorageConnectionString = configuration["CloudStorageAccountConnection"];
            }
        }
    }
    

    然后我尝试在启动项目的启动类中配置这个-

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    
    public IConfiguration Configuration { get; }
    
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
        //I can read the AppSettings values here via Configuration..
    
        var settings = new ApplicationSettings(Configuration);
        services.AddSingleton(settings);
    }
    

    最后使用它-

    public class ToolService : IToolService
    {
        private DataContext _dataContext = null;
        private readonly Common.ApplicationSettings _settings;
        public ToolService()
        {
            _dataContext = new DataContext();
        }
    
        public ToolService(Common.ApplicationSettings settings)
        {
            _settings = settings; //settings is null here
        }
    
        public ToolDetailModel ReadToolDetail(int toolDetailID)
        {
            ToolDetailModel toolDetailModel = null;
            try
            {
                var cloudConnection = _settings.CloudConnectionString; //settings is null here      
                //...
            }
            catch
            {
    
            }
            return toolDetailModel;
        }
    }
    

    这就是我如何在主启动项目中的API控制器中调用上述函数的方法。-

    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            IToolService _toolService = new ToolService();
            _toolService.ReadToolDetail(1);
            //...
            return new string[] { "value1", "value2" };
        }
    }
    

    这个 AppSettings 当我试图通过将它们作为参数传递(使用上面所示的依赖项注入)来在不同的项目中使用它们时,对象为空。

    我做错了吗?请告诉我是否可以添加更多细节。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Simply Ged Saeed    8 年前

    您已经为定义了两个构造函数 ToolService 而目前,您在另一个项目中调用了错误的构造函数。您应该修改它,使您只有一个构造函数:

    public ToolService(Common.ApplicationSettings settings)
    {
        _dataContext = new DataContext();
        _settings = settings; //settings is null here
    }
    

    如果希望类库使用依赖项注入,则需要使用 services 收藏,例如

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
        //I can read the AppSettings values here via Configuration..
    
        var settings = new ApplicationSettings(Configuration);
        services.AddSingleton(settings);
    
        // use whatever lifetime you prefer
        services.AddScoped<ToolService>();
    }
    

    现在当你试着打电话的时候 new ToolService() 它会抱怨说你需要提供 ApplicationSettings 参数。 问题是你不能用 新工具服务() 如果使用依赖注入。您需要请求依赖注入器为您创建类。对于ASP.NET核心,这是 IServiceProvider 接口。

    您不显示正在创建的位置 工具服务 类。如果它在注册的类中 ConfigureServices 然后你就可以通过 工具服务 作为构造函数参数,依赖注入器将为您解析引用,包括 应用程序设置 参数 工具服务 类需要,例如

    public class MyClass
    {
        public MyClass(ToolService toolService)
        {
            _toolService = toolService;
        }
    
        public void MyMethod()
        {
            _toolService.ReadToolDetail(1);
        }
    }