Wanted to read from appsetting values:
"CosmosConfig": {
"EndPointUri": "1",
"AuthorizationKey": "2",
"DatabaseName": "3",
"DatabaseContainer": "4"
},
In repository class i want to retrieve the data, but receiving null object reference:
public abstract class Repository<TEntity> : IRepository<TEntity>, IDisposable where TEntity : Entity
{
private readonly CosmosConfig _appSettings;
private readonly Container _container;
private readonly CosmosClient _cosmosClient;
public abstract string DatabaseId { get; }
public abstract string ContainerId { get; }
public Repository(
IOptions<CosmosConfig> appSettings,
CosmosClient cosmosClient)
{
_appSettings = appSettings.Value;
_cosmosClient = cosmosClient;
_container = _cosmosClient.GetContainer(_appSettings.DatabaseId, _appSettings.DatabaseContainer);
}
Program.cs where i try to read values:
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.ConfigureServices((hostContext, services) =>
{
services.Configure<CosmosConfig>(hostContext.Configuration.GetSection("CosmosConfig"));
})
.Build();
var serviceProvider = host.Services;
var mySettings = serviceProvider.GetRequiredService<IOptions<CosmosConfig>>().Value;