简体   繁体   English

ASP.NET Core:从解决方案中的另一个项目访问appsettings

[英]ASP.NET Core: Accessing appsettings from another project in solution

In my Startup.cs class I have the following config build which initializes the db context: 在我的Startup.cs类中,我有以下配置构建,它初始化db上下文:

var builder = new ConfigurationBuilder()
                    .SetBasePath(env.ContentRootPath)
                    .AddJsonFile("appsettings.json", true, true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json",true)
                    .AddEnvironmentVariables();

Configuration = builder.Build();

NHibernateUnitOfWork.Init(Configuration["ConnectionStrings:OracleConnection"]);

NHibernateUnitOfWork is located in a different project as a class library project. NHibernateUnitOfWork作为类库项目位于不同的项目中。 I want to be able to inject (or somehow pass the settings) instead of using the NHibernateUnitOfWork.Init in my Startup.cs class. 我希望能够注入(或以某种方式传递设置),而不是在我的Startup.cs类中使用NHibernateUnitOfWork.Init

I can build the appsettings in my class library but trying to avoid that. 我可以在我的类库中构建appsettings但是试图避免这种情况。

What is a sane way to do this ? 什么是理智的方式来做到这一点?

You can inject it. 你可以注射它。

In your Startup.cs class, you can add the following to your ConfigureServices(IServiceCollection services) method: Startup.cs类中,可以将以下内容添加到ConfigureServices(IServiceCollection services)方法中:

services.AddSingleton(Configuration);

From there, you can use constructor injection and consume it downstream the same way you normally would: 从那里,您可以使用构造函数注入并以与通常相同的方式向下游使用它:

public class SomeClass
{
    public SomeClass(IConfigurationRoot configuration) 
    {
        NHibernateUnitOfWork.Init(Configuration["ConnectionStrings:OracleConnection"]);
    }
}

NOTE: This assumes Configuration is defined in your Startup.cs class: 注意:这假定ConfigurationStartup.cs类中定义:

public static IConfigurationRoot Configuration;

See this: Read appsettings json values in .NET Core Test Project 请参阅: 在.NET Core Test Project中读取appsettings json值

Essentially this is an integration solution using the TestServer. 基本上这是使用TestServer的集成解决方案。 But if you're having to use the configuration, then it should probably be an integration test anyway. 但是如果你不得不使用配置,那么无论如何它应该是一个集成测试。

The key to using the config in your main project inside your test project is the following: 在测试项目中的主项目中使用配置的关键如下:

"buildOptions": {
  "copyToOutput": {
    "include": [ "appsettings.Development.json" ]
  }
}

which needs to go inside your main project's project.json 需要进入主项目的project.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET CORE 1.0,来自另一个程序集的AppSettings - ASP.NET CORE 1.0, AppSettings from another assembly 是否可以在另一个解决方案中从 ASP.NET Webforms 项目中引用 ASP.NET Core 2.0 类库 dll? - Possible to reference an ASP.NET Core 2.0 class library dll from an ASP.NET Webforms project in another solution? 如何在另一个项目的上下文中阅读 appsettings.json? ASP.NET 核心 - How to read appsettings.json in context in another project? Asp.Net Core 如何使用 Src asp.net 内核从同一解决方案中的另一个项目获取图像? - how to get image from another project in the same solution with Src asp.net core? Asp.Net Core v1.1从同一解决方案中的另一个项目获取继承的类名 - Asp.Net Core v1.1 Get Inherited Class Names From another project in same solution 将测试项目 appSettings 附加到 ASP.NET Core 集成测试 - Append test project appSettings to ASP.NET Core integration tests 从另一个项目/程序集访问asp.net核心中的预编译视图 - Accessing pre-compiled views in asp.net core from another project/assembly ASP.NET Core 应用程序不会从输出目录中读取 appsettings.json,而是从项目一中读取 - ASP.NET Core app does not read appsettings.json from output directory, but from the project one 发布时从另一个ASP.NET Core 2项目引用ASP.NET Core 2项目失败 - Referencing ASP.NET Core 2 project from another ASP.NET Core 2 project fails when publishing 从 Asp.Net Core AppSettings 获取值 - Get values from Asp.Net Core AppSettings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM