如何从.net核心中的自定义过滤器中的appsettings.json获取连接字符串 [英] How to get connection string from appsettings.json inside custom filter in .net core

查看:87
本文介绍了如何从.net核心中的自定义过滤器中的appsettings.json获取连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器EBisUserController,它包含一个通过依赖注入从appsettings.json获得的公共属性ConnectionString.控制器具有属性过滤器"EBisUserAuthResourceFilter",要求使用在控制器中找到的属性ConnectionString.访问ConnectionString的最高效的方法是什么.我有一个想要的工作示例,但是知道这不是正确的方法,因为它必须打开并读取每个事务的文件.

I have a controller EBisUserController which contains a public property ConnectionString obtained from appsettings.json through dependency injection. The controller has an attribute filter 'EBisUserAuthResourceFilter' requiring use of the property ConnectionString found in the controller. What is the most performant method to access ConnectionString. I have a working example of what I want, but know this is not the correct way of doing this as it must open and read the file for each transaction.

public class EBisUserAuthResourceFilter : Attribute, IResourceFilter { 

    private string _connectionString;

    public EBisUserAuthResourceFilter() {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json");
        _connectionString= builder.Build().GetValue<string>("Data:DefaultConnection:ConnectionString"); //this property exists as property of controller through DI, how can we access it?
    }
}

推荐答案

在过滤器中也可以进行依赖注入.

Dependency injection is possible in filters as well.

这是获取连接字符串的简单方法

Here is a simple way to get connection string

public class EBisUserAuthResourceFilter : Attribute, IResourceFilter
{
    private readonly string connectionString;

    public EBisUserAuthResourceFilter(IConfiguration configuration)
    {
        this.connectionString = configuration
                   .GetSection("ConnectionStrings:DefaultConnection").Value;
    }
    public void OnResourceExecuted(ResourceExecutedContext context)
    {
        // use this.connectionString
    }

    public void OnResourceExecuting(ResourceExecutingContext context)
    {
        // use this.connectionString
    }
}

现在您可以使用此过滤器

Now you can use this filter

[ServiceFilter(typeof(EBisUserAuthResourceFilter))]
public class HomeController : Controller
{  }

您还需要将此过滤器添加到服务集合

You also need to add this Filter to the service collection

public void ConfigureServices(IServiceCollection services)
{
   services.AddScoped<EBisUserAuthResourceFilter>();

   // your existing code to add other services
}

另一种解决方案是让一个类表示AppSettings.json文件或子节内容的结构,并将其加载到您的Startup类的ConfigureServices方法

Another solution is to have a class representing the structure of the content of AppSettings.json file or a sub section and load that in your Startup classes' ConfigureServices method

services.Configure<SiteSettings>(Configuration);

,现在您可以注入IOptions<SiteSettings>并使用所需的属性值.我更喜欢这样做,因为它在我的代码中更少的魔术弦.

and now you can inject IOptions<SiteSettings> and use the needed property values. I prefer this as it is less magic strings in my code.

这篇关于如何从.net核心中的自定义过滤器中的appsettings.json获取连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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