0

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?
    }
}

1 Answer 1

0

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
}

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);

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The top example, which is preferrable to me since the appsettings.json will probably me modified often for new development worked with the following modification: in ConfigureServices method of Startup.cs I had to add: services.AddTransient<EBisUserAuthResourceFilter>();
Yes. You need to do that for the filter to be available

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.