1

I am newbiew in dotnet/c# and trying to learn. I am writing a server app that connects/init to different database. one is MongoDB. then I have a config microservice. how do I save the connection string that I got from my config-server? from these MongoDB example, I need to pass the connection string. but I don't want to get it from the config-server every time.

Read some tutorials from here about dependency injection, not sure if this is really what I need.

Here is my tryout code..

my configservice that I want to setup or act as getter/setter

public class Configctl : IDisposable
{
    public static string env;
    public Configctl()
    {
        env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        Console.WriteLine($"ASPNETCORE_ENVIRONMENT/env={env}");
    }

    public static void ConfigureMongodb()
    {
        Console.WriteLine($"configure mongodb..");
        // var config = await getConfig()
    }
}

MongdbContext that I want to fill the MongoUrl from the Configctl without getting from the Configctl each time.

public class MongdbContext<T> : IDisposable
{
    public IMongoDatabase _database { get; }
    public IMongoClient _client;
    public MongdbContext()
    {
        var settings = MongoClientSettings.FromUrl(GetMongoURL());
        _client = new MongoClient(settings);
        _database = _client.GetDatabase("testdb");
    }

    public void Dispose()
    {
        Console.WriteLine("mongodb disposed");
    }

    public static MongoUrl GetMongoURL()
    {
        return new MongoUrl("mongodb://user:pass@host:27017/admin");
    }
}

and for experiment, I tried calling Configctl from ConfigureServices. I maybe wrong..

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(new Configctl());

        services.AddMvc();
    }

1 Answer 1

2

If you don't need to read options from external service each time, then just read it on application start, bind to a POCO and register the POCO as singleton. Then pass the POCO as dependency to your MongdbContext.

Options reader example:

class OptionsReader
{
    public MyOptions GetMyOptions()
    {
         //call to external config microservice, db, etc.
    }
}

Services registration in Startup class:

public void ConfigureServices(IServiceCollection services)
{    
    services.AddTransient<OptionsReader>();
    services.AddSingletonFromService<OptionsReader, MyOptions>(x => x.GetMyOptions());
}

Useful extention method:

public static void AddSingletonFromService<TService, TOptions>(
    this IServiceCollection services,
    Func<TService, TOptions> getOptions)
    where TOptions : class
    where TService : class
{
    services.AddSingleton(provider =>
    {
        var service = provider.GetService<TService>();

        TOptions options = getOptions(service);

        return options;
    });
}

Options consumer:

class MongdbContext
{
    public MongdbContext(MyOptions options)
    {
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this looks fantastic and clean. I'm going to try this one now. thank you

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.