APIs with C# – Part 4 Options

NET provides support for passing options via environment variables, appsettings.json, and XML files. as well as command line arguments. In short, each higher level overrides the settings in lower levels.

To add options:

  1. Create a public class and name it (we’ll use funcOptions)
  2. Add a string property called returnValue (or name it whatever you like) and set the property to some value (e.g., MyOptions)
  3. We’re going to override that value in applications.json
  4. Add a property in appsettings.json (e.g., retVal) and set that property to a different value than we did in step 2

It’s time to set up the startup configuration. When you are done, your Program.cs should look like this:

var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices((context, services) =>
{
  services.AddApplicationInsightsTelemetryWorkerService()
  .ConfigureFunctionsApplicationInsights()
  .AddOptions<MyOptions>
     .BindConfiguration("");
})
.Build();
host.Run();

The next step is to inject IOptions<MyOptions> and in the constructor set a member variable.

The Run method will look like this:

[Function("Function1")]
public IActionResult Run(HttpTrigger(AuthorizationLevel
  .Anonymous, "get", "post")] HttpRequest req)
{
   return new OKObjectResult(options.value.retValue);
}

 

Unknown's avatar

About Jesse Liberty

** Note ** Jesse is currently looking for a new position. You can learn more about him at https://jesseliberty.bio Thank you. Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 13 year Microsoft MVP.
This entry was posted in APIs. Bookmark the permalink.

1,794 Responses to APIs with C# – Part 4 Options

  1. 摩臣APP's avatar 摩臣APP says:

    Just wish to say your article is as amazing. The clearness in your post is simply great and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the gratifying work.

  2. Excellent post. Keep writing such kind of info on your page. Im really impressed by it.

  3. slot gacor's avatar slot gacor says:

    Some really nice and useful info on this site, as well I conceive the layout has good features.

  4. At this time it looks like WordPress is the preferred blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  5. ratubet's avatar ratubet says:

    I cherished as much as you will obtain carried out right here. The cartoon is tasteful, your authored subject matter stylish. nonetheless, you command get bought an shakiness over that you want be delivering the following. ill undoubtedly come more beforehand once more since precisely the similar just about very continuously within case you shield this increase.

  6. Currently it seems like BlogEngine is the preferred blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog?

  7. At this time it sounds like Expression Engine is the best blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog?

  8. pestoto's avatar pestoto says:

    I am commenting to make you be aware of of the fantastic discovery my wife’s princess encountered studying your web site. She came to find too many things, which include how it is like to possess a very effective coaching nature to have many others without difficulty have an understanding of chosen specialized subject areas. You really did more than our own expectations. Many thanks for churning out those good, trustworthy, revealing and even cool tips about your topic to Kate.

  9. Currently it sounds like Drupal is the top blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

  10. What i don’t realize is in truth how you are not actually much more well-preferred than you may be right now. You’re so intelligent. You already know therefore considerably when it comes to this subject, produced me in my view believe it from numerous varied angles. Its like men and women don’t seem to be fascinated unless it’s something to do with Woman gaga! Your personal stuffs excellent. All the time handle it up!

  11. Richardkig's avatar Richardkig says:

    Viagra homme prix en pharmacie sans ordonnance: Viagra pas cher inde – Viagra vente libre pays

  12. Robertscals's avatar Robertscals says:

    Viagra gГ©nГ©rique sans ordonnance en pharmacie: Viagra Pfizer sans ordonnance – Viagra vente libre pays

  13. situs bola's avatar situs bola says:

    You really make it seem so easy with your presentation but I find this topic to be really one thing which I believe I would never understand. It sort of feels too complex and very huge for me. I’m looking forward on your next submit, I’ll try to get the cling of it!

  14. phdreamph's avatar phdreamph says:

    Been playing on phdreamph and I like the selection of games they have. Pretty straightforward site. Check out phdreamph if you’re looking for something new. phdreamph

  15. 25phdream's avatar 25phdream says:

    25phdream is my new go-to! Small bets, big wins! Hopefully! The site’s user-friendly too. Worth checking out 25phdream. 25phdream

  16. phdream33's avatar phdream33 says:

    Phdream33, kaya mo ba ‘ko panalunin? Haha! Jokes aside, it’s a fun site. Been having some good luck here. Try phdream33, baka ikaw rin swertehin! phdream33

  17. Just want to say your article is as surprising. The clarity in your post is just great and i can assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please continue the gratifying work.

  18. Hello there, You have done an incredible job. I’ll certainly digg it and in my opinion suggest to my friends. I’m sure they will be benefited from this site.

  19. PatrickBep's avatar PatrickBep says:

    gurkmeja apotek Svenska Pharma Svenska Pharma

  20. Williamisono's avatar Williamisono says:

    Viagra sans ordonnance livraison 24h: Viagra gГ©nГ©rique sans ordonnance en pharmacie – Viagra homme sans ordonnance belgique

  21. I very happy to find this website on bing, just what I was looking for : D besides bookmarked.

  22. Great series, Jesse! I’ve been following along, and the way you break down the Options pattern in Part 4 really clarified how to manage configuration in .NET Core. I especially appreciated the example with IOptionsSnapshot—it’s exactly what I needed for handling dynamic settings in my current project. Do you have any tips for testing these patterns in unit tests?

  23. Jeraldbasse's avatar Jeraldbasse says:

    https://sepharm.shop/# Svenska Pharma

  24. My brother recommended I might like this blog. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this information! Thanks!

  25. Hey! I’m at work surfing around your blog from my new iphone! Just wanted to say I love reading through your blog and look forward to all your posts! Carry on the superb work!

  26. jagoledak's avatar jagoledak says:

    Nice post. I learn something more challenging on different blogs everyday. It will always be stimulating to read content from other writers and practice a little something from their store. I’d prefer to use some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your web blog. Thanks for sharing.

  27. Jamesguism's avatar Jamesguism says:

    prescriptions from mexico: mexico medication – mexican medicine store

  28. pestoto's avatar pestoto says:

    Hiya, I’m really glad I have found this information. Nowadays bloggers publish just about gossips and net and this is really irritating. A good website with interesting content, that is what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Can not find it.

  29. Well I sincerely liked reading it. This post provided by you is very useful for proper planning.

  30. The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

  31. dondego's avatar dondego says:

    I’m not sure exactly why but this web site is loading very slow for me. Is anyone else having this problem or is it a issue on my end? I’ll check back later and see if the problem still exists.

  32. Great beat ! I wish to apprentice at the same time as you amend your website, how can i subscribe for a weblog website? The account helped me a applicable deal. I had been tiny bit acquainted of this your broadcast offered vibrant transparent concept

  33. rajawd's avatar rajawd says:

    I’ve learn several just right stuff here. Certainly price bookmarking for revisiting. I surprise how so much attempt you place to create this sort of fantastic informative website.

  34. ratubet's avatar ratubet says:

    After study a few of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website list and will be checking back soon. Pls check out my web site as well and let me know what you think.

  35. Simply wish to say your article is as amazing. The clearness in your post is just nice and i can assume you’re an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.

  36. OctavioAmoub's avatar OctavioAmoub says:

    MexiCare Direct: MexiCare Direct – MexiCare Direct

  37. I am glad to be a visitant of this pure weblog! , regards for this rare information! .

  38. OctavioAmoub's avatar OctavioAmoub says:

    reputable indian online pharmacy: Flexi Meds India – shop medicine online

  39. Randynek's avatar Randynek says:

    https://mexicaredirect.shop/# mexico drug store online

  40. JamesSog's avatar JamesSog says:

    mail order pharmacies: buying prescription drugs in mexico – reliable rx pharmacy

Comments are closed.