.Net Core How to Access Configuration Anywhere in application - asp.net-core

I have read through the documentation on the different ways to setup and access configuration in .Net Core 2.1 and also the options pattern that seems to be recommended (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.1). However, I can't seem to get what I want working:
I have done the following:
AppSettings:
{
"ConnectionStrings": {
"DefaultConnStr": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true",
"AW2012ConnStr": "Server=localhost;Database=AW2012;Trusted_Connection=True;MultipleActiveResultSets=true;Integrated Security=true"
}
}
MyConfig:
public class MyConfig
{
public string AWConnStr { get; }
public string DefaultConnStr { get; }
}
Startup:
public class Startup
{
public IConfiguration _config { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
_config = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
//add config to services for dependency injection
//services.AddTransient<IMyConfig, MyConfig>();
//services.AddScoped<IMyConfig, MyConfig>();
var section = _config.GetSection("ConnectionStrings");
services.Configure<MyConfig>(section);
}
private static void HandleGetData(IApplicationBuilder app)
{
//DataHelper dataHelper = new DataHelper(_dataHelper);
var _dataHelper = app.ApplicationServices.GetService<DataHelper>();
app.Run(async context =>
{
//await context.Response.WriteAsync("<b>Get Data</b>");
//await context.Response.WriteAsync(dataHelper.GetCompetitions(context.Request.QueryString.ToString()));
await context.Response.WriteAsync(_dataHelper.GetCompetitions(context.Request.QueryString.ToString()));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Map("/Route1", HandleRoute1);
app.Map("/Route2", HandleRoute2);
app.Map("/GetData", HandleGetData);
app.Run(async (context) =>
{
await context.Response.WriteAsync("Non Mapped Default");
});
}
}
I would like to then access the configuration in any class anywhere in my code. So for example I have the following class where I would like to just read the configuration information:
public interface IDataHelper
{
string GetCompetitions(string val);
}
public class DataHelper : IDataHelper
{
private readonly MyConfig _settings;
public DataHelper(IOptions<MyConfig> options)
{
_settings = options.Value;
}
public string GetCompetitions( string queryStringVals)
{
return _settings.AWConnStr;
}
}
As shown above in my Startup class I then want to access/call something in the HandleGetData function in my startup, so that when I browse to the following route: http://localhost:xxxxx/getdata I get back the response from the Something.GetData function.
Is this correct? The problem I'm having is that when I create an instance of class Something, it is requiring me to pass in the configuration object, but doesn't that defeat the purpose of injecting it. How should I be setting this up to work similar to how DBContext gets the context injected with the configuration options. And what's the difference between services.AddTransient and services.AddScoped? I've seen both as a way to register the service.

I would say that in .Net Core application you shouldn't pass instance of IConfiguration to your controllers or other classes. You should use strongly typed settings injected through IOtions<T> instead. Applying it to your case, modify MyConfig class (also property names should match names in config, so you have to rename either config (DefaultConnection->DefaultConnStr, AW2012ConnStr->AWConnStr or properies vice versa):
public class MyConfig
{
public string AWConnStr { get; set; }
public string DefaultConnStr { get; set; }
}
Register it:
public void ConfigureServices(IServiceCollection services)
{
// in case config properties specified at root level of config file
// services.Configure<MyConfig>(Configuration);
// in case there are in some section (seems to be your case)
var section = Configuration.GetSection("ConnectionStrings");
services.Configure<MyConfig>(section);
}
Inject it to required service:
public class MyService
{
private readonly MyConfig _settings;
public MyService(IOptions<MyConfig> options)
{
_settings = options.Value;
}
}
And what's the difference between services.AddTransient and
services.AddScoped? I've seen both as a way to register the service.
Transient lifetime services are created each time they're requested.
Scoped lifetime services are created once per request.

You have to do the same thing for the Something as you did for MyConfig like:
public interface ISomething
{
string GetSomeData();
}
Then:
public class Something : ISomething
{
public IConfiguration _config { get; set; }
public Something(IConfiguration configuration)
{
_config = configuration;
}
public string GetSomeData()
{
return _config["DefaultConnStr"];
}
}
Then in the ConfigureService method of the Startup class as follows:
services.AddScoped<ISomething,Something>();
Then call the GetSomeData() as follows:
public class CallerClass
{
public ISomething _something { get; set; }
public CallerClass(ISomething something)
{
_something = something;
}
public string CallerMethod()
{
return _something.GetSomeData();
}
}
Then:
And what's the difference between services.AddTransient and services.AddScoped? I've seen both as a way to register the service.
Here is the details about this from microsoft:
Service Lifetime details in ASP.NET Core

Related

ASP.NET Core - Create a singleton class to supply connection string across the application

I'm creating a ASP.NET Core Web API using ADO.NET (without Entity Framework). I need a singleton class to supply connection string to all the controllers. I have done the following.
Defined a class DBUtils to have just one public property DBConnectionString.
Try to register the class as a singleton in startup.cs.
Use the class through DI in each controller to access the connection string.
public class DBUtils
{
public string DBConnectionString { get; set; }
public DBUtils(string connectionString)
{
this.DBConnectionString = connectionString;
}
}
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.AddSingleton<DBUtils>();
services.AddControllers();
}
}
public class CommonController : ControllerBase
{
private string conStr;
public CommonController(DBUtils utils)
{
conStr = utils.DBConnectionString;
}
public IActionResult GetData() {
SqlConnection connection = new SqlConnection(conStr);
//Get dat from the database
return null;
}
}
Now the problem is I'm not able to pass the connection string to the DBUtils constructor. I read from other posts that we should not use parameters to Singleton classes. But my class will only have one parameter and it will never change during execution. It gets the connection string from config file.
please help how to I pass connection string to my controllers.
I don't want to use IConfiguration as DI in the controller class directly.
UPDATE: I realised that Singleton is not the approach for my requirement and as #Ceemah Four suggested we should use Options Pattern.
Thanks
This scenario has already been catered for in dotnet core.
You do not need to create the DBUtils class. Neither do you need to set up the Singleton DI etc.
Assuming this is your appsettings.json
"ConnectionStrings": {
"SqlDatabase": "connection string here"
}
There are two potential approaches:
Inject IConfiguration in Controller constructor - you can simply access the connection string value from the injected Configuration.
public class CommonController : ControllerBase
{
private readonly IConfiguration _config;
private string conStr;
public CommonController(IConfiguration config)
{
_config = config;
}
public IActionResult GetData()
{
SqlConnection connection = new SqlConnection(_config.GetConnectionString("SqlDatabase"));
//Get data from the database
return null;
}
}
Create a Settings class, bind the settings class in Startup and inject the Settings class in the controller constructor. This uses the IOPtions pattern * and is a cleaner and recommended approach*
public class ConnectionSettings
{
public string SqlDatabase { get; set; }
}
In your startup.cs:
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.Configure<ConnectionSettings>(Configuration.GetSection("ConnectionStrings"));
services.AddControllers();
}
}
Then in your controller:
public class CommonController : ControllerBase
{
private readonly IOptions<ConnectionSettings> _connectionSettings;
public CommonController(IOptions<ConnectionSettings> connectionSettings)
{
_connectionSettings = connectionSettings;
}
public IActionResult GetData()
{
SqlConnection connection = new SqlConnection(_connectionSettings.Value.SqlDatabase));
//Get data from the database
return null;
}
}

How to get AppSetting values in startup by using services in asp.net core?

I want to get value of appsetting inside StartUp and also using services for saving them.
I create a static IServiceCollection method for AddTransient my custom service.
I define a readonly variable for keep the appsetting values. My problem is that, this service creates new instance for readonly variable, for all calling.how can I prevent this?
and I have a question that other extensions like AddOpenIdConnect, how to work with their configs, I mean how to save and use them?
this is startup:
public void ConfigureServices(IServiceCollection services){
...
services.AddMyIntegration(conf =>
{
conf.ConnectionString = Configuration.GetConnectionString("Integration");
conf.AgentApiAddress = Configuration["AgentApiAddress"];
});
}
....
public static class MyExtension
{
public static IServiceCollection AddMyIntegration(this IServiceCollection services, Action<MyConstantsProvider> myConstantsProvider)
{
services.AddTransient((t) =>
{
return new MyService(myConstantsProvider);
});
return services;
}
}
this is my service:
public class MyService
{
public readonly MyConstantsProvider Provider;
public MyService(Action<MyConstantsProvider> configure)
{
Provider = new MyConstantsProvider();
configure(Provider);
}
}
public class MyConstantsProvider
{
public string ConnectionString { get; set; }
public string AgentApiAddress { get; set; }
}
Update my question:
Finally I fixed my issue by add MyConstantsProvider as singletone instead of MyService so this creates new instance of variable at the first time in extension class:
public static class MyExtension
{
public static IServiceCollection AddMyIntegration(this IServiceCollection services, Action<MyConstantsProvider> myConstantsProvider)
{
var provider = new MyConstantsProvider();
myConstantsProvider(provider);
services.AddSingleton(provider);
services.AddTransient<MyService>();
return services;
}
}
this is MyService class:
public class MyService
{
public readonly MyConstantsProvider Provider;
public MyService(MyConstantsProvider provider)
{
Provider = provider;
}
}
I wonder why we make it so complicated ? I just saw we're trying to read appsettings later in the application somewhere, and for this, the framework have default implementation to back us up.
Our app settings might look like
{
"Catalog": {
"ConnectionString": "SomeConnection",
"AgentApiAddress": "http://somewhere.dev"
}
}
Then our class could be
public class MySetting
{
public string ConnectionString { get; set; }
public string AgentApiAddress{ get; set; }
}
Config register it in startup (or somewhere we like in .net 6)
services.Configure<MySetting>(configuration.GetSection("Catalog"));
Retrive it later in the app via DI
public class SomeService
{
private readonly MySetting _setting;
public SomeService(IOptions<MySetting> config)
{
_setting = config.Value;
}
}
For setting that can be change dynamically, take a look at IOptionsMonitor
Or that might be some special case that I miss ?

inject Database Context into Custom Attribute .NET Core

I'm creating ASP.NET Core 3.1 app, using SPA for front end. So I decided to create custom Authentication & Authorization. So I created custom attributes to give out and verify JWTs.
Lets say it looks like this:
[AttributeUsage(AttributeTargets.Method)]
public class AuthLoginAttribute : Attribute, IAuthorizationFilter
{
public async void OnAuthorization(AuthorizationFilterContext filterContext)
{
//Checking Headers..
using (var EF = new DatabaseContext)
{
user = EF.User.Where(p => (p.Email == username)).FirstOrDefault();
}
filterContext.HttpContext.Response.Headers.Add(
"AccessToken",
AccessToken.CreateAccessToken(user));
}
}
Everything was Okay, but my DatabaseContext, looked like this:
public class DatabaseContext : DbContext
{
public DbSet<User> User { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySQL("ConnectionString");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//....
}
}
I wanted to take Connection string from Appsettings.json and maybe use Dependency injection. I
Changed Startup.cs to look like this:
//...
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<DatabaseContext>(
options => options.UseMySQL(Configuration["ConnectionStrings:ConnectionString"]));
services.Add(new ServiceDescriptor(
typeof(HMACSHA256_Algo), new HMACSHA256_Algo(Configuration)));
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
//...
Changed Database Context class to this:
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { }
public DbSet<User> User { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
///..
}
}
In Controllers I injected DB context and everything works. It looks like this:
[ApiController]
[Route("API")]
public class APIController : ControllerBase
{
private DatabaseContext EF;
public WeatherForecastController(DatabaseContext ef)
{
EF = ef;
}
[HttpGet]
[Route("/API/GetSomething")]
public async Task<IEnumerable<Something>> GetSomething()
{
using(EF){
//.. this works
}
}
}
But my custom Attribute doesn't work no more. I can't declare new Database context, because it needs DatabaseContextOptions<DatabaseContext> object to declare, so how do I inject DBContext to Attribute as I did to Controller?
This doesn't work:
public class AuthLoginAttribute : Attribute, IAuthorizationFilter
{
private DatabaseContext EF;
public AuthLoginAttribute(DatabaseContext ef)
{
EF = ef;
}
public async void OnAuthorization(AuthorizationFilterContext filterContext)
{
using(EF){
}
}
}
this works with controller, but with attribute complains about there not being constructor with 0 arguments.
What you can do is utilize the RequestServices:
[AttributeUsage(AttributeTargets.Method)]
public class AuthLoginAttribute : Attribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext context)
{
var dbContext = context.HttpContext
.RequestServices
.GetService(typeof(DatabaseContext)) as DatabaseContext;
// your code
}
}
If you allow me to add two comments to your code:
Try not to use async void because in the event of an exception you will be very confused what is going on.
There is no need to wrap injected DbContext in a using statement like this using(EF) { .. }. You will dispose it early and this will lead to bugs later in the request. The DI container is managing the lifetime for you, trust it.

IConfiguration object is null.

I'm trying to read connection strings from appsettings.json and I'm using:
services.AddSingleton(Configuration);
This line from startup throws null. I'm pretty new to core2.0. Can someone tell what I'm missing?
My startup:
public class Startup
{
public static string ConnectionString { get; private set; }
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton(Configuration);
services.AddSingleton<IConfiguration>(Configuration);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
My controller:
public class CreateController : Controller
{
public IConfiguration _ConnectionString;
public CreateController(IConfiguration configuration)
{
_ConnectionString = configuration;
}
public IEnumerable<string> Get()
{
Markets();
}
public string Markets()
{
using(SqlConnection con = new SqlConnection(_ConnectionString.GetSection("Data").GetSection("ConnectionString").Value))
{
return con.Database;
}
}
}
I've noticed your Startup is missing a constructor. In ASP.NET Core 2, when it calls startup (based on a typical WebHost.CreateDefaultBuilder(args).UseStartup<Startup>() inside a vanilla Program.BuildWebHost) will automatically pass the configuration into the Startup constructor:
public class Startup
{
public IConfiguration Configuration { get; }
// This configuration is automatic, if WebHost.CreateDefaultBuilder(args) is used in Program.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
Just adding that will solve your IConfiguration is null issue.
Now, you should not have to add that Configuration into DI because with the defaults it should already be in there and you can add it as-is to your controller constructor. However, there's no harm in doing so.
Lastly, to join the chorus, using IConfiguration directly in you controllers is not a good idea. Rather look into strongly typed configuration settings. There are tutorials out there that can help - here's the first link I found - but the gist is your controller will end up looking sort of like this:
public class CreateController : Controller
{
public ConnectionStrings _ConnectionStrings;
public CreateController(IOptions<ConnectionStrings> connectionStrings)
{
_ConnectionStrings = connectionStrings.Value;
...
You shouldn't be calling services.AddSingleton(Configuration) in ConfigureServices. It is already in the DI container by default.
You simply need to reference it within your Controler:
public class CreateController : Controller
{
public IConfiguration _configuration;
public CreateController(IConfiguration configuration)
{
_configuration = configuration;
}
public IEnumerable<string> Get()
{
Markets();
}
public string Markets()
{
var connectionString = _configuration.GetConnectionString("ConnectionStringName");
using( SqlConnection con = new SqlConnection(connectionString) )
{
return con.Database;
}
}
}
It is null because it hasn't been set. You need to build your configuration first which is best done in the constructor. As others have pointed out it is not recommended to do this.
Example
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
builder.AddUserSecrets<Startup>();
}
Configuration = builder.Build();
}
private IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Config);
}
}

.Net Core passing connection string to DBContext class

Just started using .Net Core and facing passing connection string information to Context console project.
I have 4 projects, created using .Net Core.
MVC
Service Layer
Domain Layer
Data Layer
In MVC project, I have Startup.cs file where i am reading appsettings.json file
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
// Add appsettings
services.Configure<AppSettingsConfig>(Configuration.GetSection("AppSettings"));
}
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
}
In my 4th project (Data Layer), which Console Project and having following DBContext class. This project doesn't have Startup.cs as i MVC project having. Not created by default by VS 2015.
public class MyDWContext : DbContext
{
public MyDWContext() : base ()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Data Source=localhost;Initial Catalog=MyDW; Persist Security Info = False; User ID = TempUser; Password = Temp123");
}
public DbSet<User> Users { get; set; }
public DbSet<Class> Classs { get; set; }
}
I have been to other post as well but i believe its created using older version or RC version. So some time i cannot find correct object or .Net classes.
As i have connection string is in MVC project, how can i use connection string during my MVC call to Data layer.
I have Web.API (Core) project as well and that having own connection string (different user configuration in connection string which having only read access). How can i use Web2.API connection string when i am making call from Web2.API project.
Instead of passing connection string to DbContext, configuring DbContext in Startup.cs(if possible) is better way. See official docs to understand how to configure DbContext and use it via Dependency Injection.
EDIT : Below code is not good way
However, if you want to pass connection string to DbContext you can use options pattern.
Here is an example how to pass connection string with options pattern:
First you need an options class which accessible from Data Layer and MVC layer
public class ConnectionStringOption
{
public string ConStr { get ; set; }
}
Then set option value
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<ConnectionStringOption>(options=>
{
// set connection string from configuration
options.ConStr = Configuration.GetConnectionString("Default");
});
}
appsetting.json
{
"ConnectionStrings": {
"Default": "<your connection string>"
}
}
Finally DbContext
private readonly IOptions<ConnectionStringOption> _conStrOptions;
protected YourDbContext()
{
}
public YourDbContext(IOptions<ConnectionStringOption> conStrOptions, DbContextOptions options)
: base(options)
{
_conStrOptions= conStrOptions;
}
Edit for another way
Using Static Service Locator may be a solution:
Create a DependencyResolver in Data Layer
public static class DependencyResolver
{
private static IServiceProvider _provider;
public static IServiceProvider ServiceProvider
{
get
{
return _provider;
}
set
{
if(_provider == null)
{
_provider = value;
}
}
}
}
In ConfigureServices method
public void ConfigureServices(IServiceCollection services)
{
// other stuff
services.AddOptions();
services.Configure<ConnectionStringOption>(options=>
{
// set connection string from configuration
options.ConStr = Configuration.GetConnectionString("Default");
});
DependencyResolver.ServiceProvider = services.BuildServiceProvider();
}
And finally get option:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var conStr = DependencyResolver.ServiceLocator.GetService<IOptions<ConnectionStringOption>>().Value.ConStr;
optionsBuilder.UseSqlServer();
}
Final Edit for previous stupid way
public static class ConnectionStringGetter
{
public static string ConStr{get;set;}
}
public Startup(IHostingEnvironment env)
{
//...
Configuration = builder.Build();
ConnectionStringGetter.ConStr = Configuration.GetConnectionString("Default");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConnectionStringGetter.ConStr);
}