Implicit Configuration in .NET
最后更新 November 2, 2023

Hello friends,

In our last post, we demonstrated the process of registering Vonage clients within the .NET IoC container. This was a valuable step in simplifying interactions with our SDK.

Now, let's take a moment to ponder: What if we were to take this a step further?

Where Did We Leave Off?

Just a refresher: here's how we used to register those clients:

// Initialize credentials
Credentials credentials = ...
// Register our clients with a 'Transient' lifetime...
builder.Services.AddVonageClientTransient(credentials);
// Or a 'Scoped' lifetime
builder.Services.AddVonageClientScoped(credentials);

You had to initialize a Credentials instance, typically from an ApiKey/ApiSecret pair or an ApplicationId/PrivateKey pair. These values often came from your configuration file or were plucked from our Configuration instance.

You see where I'm going, don't you?

Implicit Configuration Loading

No more fussing about that Credentials instance anymore. We've taken it further and tweaked the method to accept an IConfiguration instance immediately.

// Register our clients with a 'Transient' lifetime...
builder.Services.AddVonageClientTransient(builder.Configuration);
// Or a 'Scoped' lifetime
builder.Services.AddVonageClientScoped(builder.Configuration);

Now, we handle all the heavy lifting as we'll grab all the necessary data straight from your configuration file.

Less manual work for you!

Extending With Environment-Specific Configuration

Until v6.9.0, we could only load configuration data from settings.json or appsettings.json. It could be problematic if you wanted to use environment-specific values, for example, with a staging environment deployment.

You might have noticed that we are discussing loading data based on your local configuration. What's the scoop there?

We're fetching all those crucial configuration values from sources you've loaded into your configuration builder. For example, we can retrieve values from appsettings.{environment}.json, the default naming convention on a new .NET application.

Signing Off

Stay tuned for more features and quality-of-life improvements.

As always, we value your feedback! So, feel free to hit up our GitHub repository to report issues, suggest improvements, or even contribute your pull requests. If you have questions, join us on the Vonage Developer Slack, and we will get back to you.

Happy coding, and see you soon!

Guillaume FaasSenior .Net Developer Advocate

Guillaume is a Senior .Net Developer Advocate for Vonage. He has been working in .Net for almost 15 years while focusing on advocating Software Craftsmanship in the last few years. His favorite topics include code quality, test automation, mobbing, and code katas. Outside work, he enjoys spending time with his wife & daughter, working out, or gaming.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.