Reading Lists From Appsettings.json - BitScry

Reading Lists from appsettings.json

Published by Shinigami on 14 November 2017

About this site

This is a blog to help me remember some of the coding challenges I’ve faced and hopefully help others out if they’re suffering the same problems.

Recent Posts
  • Installing TrueNAS on TerraMaster F4-425
  • Proxmox LXC Containers Fail to Start After Reboot
  • Adding a Sonoff Zigbee Dongle Plus P Router to Zigbee2MQTT
  • Configuring Raspberry Pi 5 with NVME Hat
  • Pass Through an iGPU to Frigate from Proxmox
Tag Cloud

.NET .NET Core AAD API appsettings.json ASP.NET Core Authentication Authorization Azure Azure Active Directory Azure Function Azure Storage Blob Bootstrap C# Cloud Storage Console Application CSS CSV DataTable Dependency Injection Dictionary Docker Entity Framework Enum Extension HTML Image JavaScript jQuery JSON List MVC Newtonsoft Raspberry Pi RestSharp Serilog SQL Stored Procedure Stream Table Table Storage Web App Web Application WordPress

SearchSearch
Copyright
Creative Commons License Any code within Reading Lists from appsettings.json by Shinigami is licensed under a Creative Commons Attribution 4.0 International License.

I’ve been using the SendGrid API for a project and I was trying to be lazy and read multiple email addresses to send to from a single parameter in appsettings.json with the email addresses separated by a semicolon. Strangely this only sends to the first email in the string so I needed to do a little more work and store the emails in an array in appsettings.json and to read this into a list and then loop through it adding each address individually.

{ "Email": { "ToEmails": [ "[email protected]", "[email protected]", "[email protected]" ] } } // Build configuration IConfigurationRoot _config = new ConfigurationBuilder() .SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName) .AddJsonFile("appsettings.json", false) .Build(); List<string> emailTo = _config.GetSection("Email:ToEmails").Get<List<string>>(); foreach(string email in emailTo) { sendGridMessage.AddTo(newEmailAddress(email)); }

If you are aiming to use this in a console application then you’ll need to install the following NuGet packages.

Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration.Binder Categories: Programming Tags: appsettings.jsonArrayListSendGridSettings

1 Comment

Nayan · 21 June 2022 at 07:44

Can you help me to mock this “_config.GetSection(“Email:ToEmails”).Get<List>();” with moq for unit testing? I am having hard time to figure this out

Reply

Leave a Reply Cancel reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

Name * Email * Website What's on your mind?

Save my name, email, and website in this browser for the next time I comment.

Δ

Related Posts

Programming

Proxmox LXC Containers Fail to Start After Reboot

After a routine reboot of my Proxmox host (along with the network switch and router), I found that all of my VMs started fine, but none of my LXC containers would come up. Trying to Read more

Programming

Pass Through an iGPU to Frigate from Proxmox

I am running Proxmox on a Dell Optiplex 7050 Mini with an i7-6700T. The i7-6700T has an integrated Intel 530 GPU and in order to use this GPU in Frigate for hardware acceleration it needs Read more

Programming

Creating a Frigate VM on Proxmox

Although there’s a community script for creating a Frigate LXC in Proxmox I’ve found it easier get working with external devices like hard drives and a Coral TPU by running as a VM rather than Read more

Tag » Appsettings.json Guid