如何从ASP.NET Core读取.NET标准类库项目中的连接字符串 [英] How to read connection string inside .NET Standard Class library project from ASP.NET Core

查看:266
本文介绍了如何从ASP.NET Core读取.NET标准类库项目中的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的解决方案中,我有一个ASP.NET Core Web项目和一个.NET Standard类库项目.类库项目是数据访问层,我想从我的数据访问层中的appsettings.json(ASP.NET Core项目)中读取连接字符串.

In my solution, I have a ASP.NET Core web project and a .NET Standard class library project. Class library project is the data access layer and I want to read the connection string from my appsettings.json (ASP.NET Core project) in my data access layer.

我发现了一些答案,例如 Andrii Litvinov 看起来很容易实现,但是他也提到了通过依赖注入实现.我不想选择简单的快捷方式,而是要寻找依赖注入实现?

I have found few answers such as by Andrii Litvinov which looks like quite straight forward to implement but he also mentioned about implementing through Dependency Injection. I don't want to choose the easy shortcut way but looking for the dependency injection implementation?

我不确定在我的类库中是否有appsettings.json然后通过IConfigurationRoot注册它是更好的选择(如

I am not sure if having appsettings.json in my class library and then registering it through IConfigurationRoot is the better option (as explained here by JRB) but in my scenario, the connection string is in the appsettings.json file of the web project and I wan't constructor dependency injection implementation in my class library project to consume the connection string.

推荐答案

您可以注入实现IConfiguration的类的实例 在此处查看
假设在您的.net核心应用中,您有一个类似以下内容的配置文件:

You can inject an instance of a class that implements IConfiguration See Here
Let's assume in your .net core app, you have a configuration file that looks something like this:

{
  "App": {
    "Connection": {
      "Value": "connectionstring"
    }
  }
}

在数据访问层(类库)中,您可以依赖于IConfiguration

In your data access layer (class library) you can take a dependency on IConfiguration

public class DataAccess : IDataAccess
{
    private IConfiguration _config;

    public DataAccess(IConfiguration config)
    {
        _config = config;
    }

    public void Method()
    {
        var connectionString = _config.GetValue<string>("App:Connection:Value"); //notice the structure of this string
        //do whatever with connection string
    }
}

现在,在ASP.net Core Web项目中,您需要连接"依赖项. 在Startup.cs中,我正在使用它(来自默认样板模板)

Now, in your ASP.net Core web project, you need to 'wire up' your dependency. In Startup.cs, I'm using this (from the default boilerplate template)

public class Startup
{
    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();
        services.AddSingleton<IConfiguration>(Configuration); //add Configuration to our services collection
        services.AddTransient<IDataAccess, DataAccess>(); // register our IDataAccess class (from class library)
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

现在,当您执行类库中的代码时,ctor将获得您在网络应用中设置的IConfiguration实例

Now, when your code in your class library gets executed, the ctor gets handed the instance of IConfiguration you have set up in your web app

注意: 如果愿意,可以创建强类型的设置类,

Note: You can create strongly typed settings class if you'd prefer, see here for more information

这篇关于如何从ASP.NET Core读取.NET标准类库项目中的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆