0

I have created a simple .net core application connecting to mongoDb as described here https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-7.0&tabs=visual-studio

Database connection information is configured in appsettings.json file.

  "DataSource": {
    "ConnectionString": "mongodb://localhost:27017",
    "DatabaseName": "Root",
    "CollectionName": "ApiLog"
  },

I have a matching class

public class DatabaseSettings
{
    public string ConnectionString { get; set; } = null!;

    public string DatabaseName { get; set; } = null!;

    public string CollectionName { get; set; } = null!;
}

I dont want to store db info in a file so how can I configure connection information in a production environment?

I expect something like

dotnet app.dll -DataSource.ConnectionString = mongodb://localhost:27017

in Program.cs i have;

builder.Services.Configure<DatabaseSettings>(
    builder.Configuration.GetSection("DataSource"))

I want to access this configuration whenever i need it via dependency injection

public class LogService
{
    private readonly IMongoCollection<ElekseLog> _collection;
    public LogService(
        IOptions<DatabaseSettings> databaseSettings)
    {
        var mongoClient = new MongoClient(
            databaseSettings.Value.ConnectionString);

        var mongoDatabase = mongoClient.GetDatabase(
            databaseSettings.Value.DatabaseName);

        _collection = mongoDatabase.GetCollection<ElekseLog>(
            databaseSettings.Value.CollectionName);
    }
}

But I couldn't make it work...

3
  • stackoverflow.com/questions/56108617/… | I think this link can help. Commented Jan 18, 2023 at 22:35
  • @AdemCatamak passing arguments is a great idea but can i do it using configuration? like var connectionInfo = builder.Configuration.GetSection("DataSource"); Commented Jan 18, 2023 at 22:52
  • 1
    You want to keep the value in the config for the local and test environment in the json file. If I understand correctly, you want to change this value when the release will be made for the prod environment. You can use EnvironmentVariable values. Or you can search config vault usage by platform that you are using (azure, aws etc). Or if you are using CI pipeline you can modify the json file there. Commented Jan 18, 2023 at 23:21

1 Answer 1

0

You can use this syntax:

dotnet app.dll --DataSource:ConnectionString=mongodb://localhost:27017

And access the value using:

var connectionString = builder.Configuration["DataSource:ConnectionString"];

Check documentation

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

1 Comment

this is working: dotnet F:\Elekse\c\loc\LogApplication\bin\Debug\net6.0\LogApplication.dll --DataSource.ConnectionString = "mongodb://localhost:27017" but I want to access it like builder.Services.Configure<ConnectionSettings>( builder.Configuration.GetSection("DataSource")); where ConnectionString is a field inside ConnectionSettings class. What is the syntax for this?

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.