Settings Results in 4 milliseconds

How to Make Sure Your Application is Secure
Category: Computer Programming

This is a leading topic nowadays, Securi ...


Views: 0 Likes: 30
What is Computer Programming
Category: Computer Programming

<div class="group w-full text-gray-800 darktext-gray-100 border-b border-black/10 darkborder-gray- ...


Views: 0 Likes: 17
 How to use configuration with C# 9 top-level programs
How to use configuration with C# 9 top-level prog ...

I’ve been working with top-level programs in C# 9 quite a bit lately. When writing simple console apps in .NET 5, it allows you to remove the ceremony of a namespace and a Main(string[] args) method. It’s very beginner-friendly and allows developers to get going without worrying about learning about namespaces, arrays, arguments, and so on. While I’m not a beginner—although I feel like it some days—I enjoy using top-level programs to prototype things quickly.With top-level programs, you can work with normal functions, use async and await, access command-line arguments, use local functions, and more. For example, here’s me working with some arbitrary strings and getting a random quote from the Ron Swanson Quotes APIusing System; using System.Net.Http; var name = "Dave Brock"; var weekdayHobby = "code"; var weekendHobby = "play guitar"; var quote = await new HttpClient().GetStringAsync("https//ron-swanson-quotes.herokuapp.com/v2/quotes"); Console.WriteLine($"Hey, I'm {name}!"); Console.WriteLine($"During the week, I like to {weekdayHobby} and on the weekends I like to {weekendHobby}."); Console.WriteLine($"A quote to live by {quote}"); Add configuration to a top-level programCan we work with configuration with top-level programs? (Yes, should we is a different conversation, of course.)To be clear, there are many, many ways to work with configuration in .NET. If you’re used to it in ASP.NET Core, for example, you’ve most likely done it from constructor dependency injection, wiring up a ServiceCollection in your middleware, or using the Options pattern—so you may think you won’t be able to do it with top-level programs.Don’t overthink it. Using the ConfigurationBuilder, you can easily use configuration with top-level programs.Let’s create an appsettings.json file to replace our hard-coded values with configuration values.{ "Name" "Dave Brock", "Hobbies" { "Weekday" "code", "Weekend" "play guitar" }, "SwansonApiUri" "https//ron-swanson-quotes.herokuapp.com/v2/quotes" } Then, make sure your project file has the following packages installed, and that the appSettings.json file is being copied to the output directory <ItemGroup> <PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" /> </ItemGroup> <ItemGroup> <None Update="appsettings.json"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> </ItemGroup> In your top-level program, create a ConfigurationBuilder with the appropriate valuesvar config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); With a config instance, you’re ready to simply read in your valuesvar name = config["Name"]; var weekdayHobby = config.GetSection("HobbiesWeekday"); var weekendHobby = config.GetSection("HobbiesWeekend"); var quote = await new HttpClient().GetStringAsync(config["SwansonApiUri"]); And here’s the entire top-level program in actionusing Microsoft.Extensions.Configuration; using System; using System.IO; using System.Net.Http; var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var name = config["Name"]; var weekdayHobby = config.GetSection("HobbiesWeekdays"); var weekendHobby = config.GetSection("HobbiesWeekends"); var quote = await new HttpClient().GetStringAsync(config["SwansonApiUri"]); Console.WriteLine($"Hey, I'm {name}!"); Console.WriteLine($"During the week, I like to {weekdayHobby.Value}" + $" and on the weekends I like to {weekendHobby.Value}."); Console.WriteLine($"A quote to live by {quote}"); Review the generated codeWhen throwing this in the ILSpy decompilation tool, you can see there’s not a lot of magic here. The top-level program is merely wrapping the code in a Main(string[] args) method and replacing our implicit typingusing System; using System.IO; using System.Net.Http; using System.Runtime.CompilerServices; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; [CompilerGenerated] internal static class <Program>$ { private static async Task <Main>$(string[] args) { IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json").Build(); string name = config["Name"]; IConfigurationSection weekdayHobby = config.GetSection("HobbiesWeekday"); IConfigurationSection weekendHobby = config.GetSection("HobbiesWeekend"); string quote = await new HttpClient().GetStringAsync(config["SwansonApiUri"]); Console.WriteLine("Hey, I'm " + name + "!"); Console.WriteLine("During the week, I like to " + weekdayHobby.Value + " and on the weekends I like to " + weekendHobby.Value + "."); Console.WriteLine("A quote to live by " + quote); } } Wrap upIn this quick post, I showed you how to work with configuration in C# 9 top-level programs. We showed how to use a ConfigurationBuilder to read from an appsettings.json file, and we also reviewed the generated code.


Software Development Good Practices
Category: .Net 7

Knowledge Collected Over the Years of Developing Design your soft ...


Views: 231 Likes: 70
Asp.Net MVC Development Notes
Category: .Net 7

<a href="https//www.freecodecamp.org/news/an-awesome-guide-on-how-to-build-restful-apis-w ...


Views: 752 Likes: 79
CSS not rendering in Ruby on Rails [Development No ...
Category: Front-End

When Ruby on Rails does not render CSS and Images look disproportionate on Linux Ubuntu Destro (W ...


Views: 365 Likes: 118

Login to Continue, We will bring you back to this content 0



For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email Address[email protected]