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. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post…

  2. mpo100 hoki's avatar mpo100 hoki says:

    Great article and straight to the point. I don’t know if this is truly the best place to ask but do you people have any ideea where to get some professional writers? Thanks 🙂

  3. you have a great blog here! would you like to make some invite posts on my blog?

  4. KermitBaike's avatar KermitBaike says:

    https://freshpharm24.com/ shop medicine online

  5. lgopro99's avatar lgopro99 says:

    I’m extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one these days..

  6. usahampo's avatar usahampo says:

    Hi there, I found your website via Google while searching for a related topic, your website came up, it looks good. I have bookmarked it in my google bookmarks.

  7. usahampo's avatar usahampo says:

    F*ckin’ remarkable issues here. I’m very glad to peer your post. Thanks a lot and i’m taking a look forward to contact you. Will you kindly drop me a e-mail?

  8. GARASISLOT's avatar GARASISLOT says:

    I genuinely enjoy looking at on this web site, it has got fantastic articles.

  9. Thanks for helping out, excellent information. “If you would convince a man that he does wrong, do right. Men will believe what they see.” by Henry David Thoreau.

  10. neuro salt's avatar neuro salt says:

    Interesting blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Kudos

  11. jelly tide's avatar jelly tide says:

    Normally I do not read article on blogs, but I wish to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, quite nice post.

  12. MarvinJarce's avatar MarvinJarce says:

    buy prescription drugs from canada cheap Easy Canada Meds safe reliable canadian pharmacy

  13. neuro sharp's avatar neuro sharp says:

    I haven’t checked in here for a while as I thought it was getting boring, but the last few posts are great quality so I guess I will add you back to my daily bloglist. You deserve it my friend 🙂

  14. RobertMen's avatar RobertMen says:

    http://easyindiameds.com/# indian pharmacy online

  15. ThomasMox's avatar ThomasMox says:

    Easy Canada Meds: Easy Canada Meds – Easy Canada Meds

  16. Great post. I am facing a couple of these problems.

  17. Alright, I came across this while looking for something completely different, but I ended up reading more than I expected. it explained things better than most sites I’ve seen and it wasn’t overwhelming. I’ll keep this in mind.

  18. prodentim's avatar prodentim says:

    The root of your writing whilst sounding agreeable initially, did not really work very well with me personally after some time. Somewhere throughout the sentences you were able to make me a believer but only for a while. I still have a problem with your leaps in assumptions and you would do well to fill in all those breaks. If you can accomplish that, I will undoubtedly be amazed.

  19. litoto's avatar litoto says:

    Very interesting subject, appreciate it for putting up.

  20. I have been browsing on-line greater than 3 hours lately, yet I by no means found any fascinating article like yours. It’s pretty price enough for me. In my opinion, if all site owners and bloggers made just right content material as you did, the net might be much more helpful than ever before.

  21. slot online's avatar slot online says:

    Hi, i believe that i saw you visited my weblog thus i got here to “go back the choose”.I am trying to to find things to enhance my site!I guess its adequate to make use of some of your ideas!!

  22. I’ve been exploring for a little for any high-quality articles or blog posts on this kind of area . Exploring in Yahoo I at last stumbled upon this site. Reading this information So i am happy to convey that I have an incredibly good uncanny feeling I discovered just what I needed. I most certainly will make sure to do not forget this website and give it a glance regularly.

  23. The next time I read a weblog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my option to learn, but I actually thought youd have one thing attention-grabbing to say. All I hear is a bunch of whining about one thing that you may repair for those who werent too busy on the lookout for attention.

  24. Henryplasy's avatar Henryplasy says:

    Order Viagra 50 mg online sildenafil online viagra canada

  25. WilliamCob's avatar WilliamCob says:

    Generic Tadalafil 20mg price Tadalafil price Generic Tadalafil 20mg price

  26. I know this if off topic but I’m looking into starting my own weblog and was wondering what all is required to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web savvy so I’m not 100 certain. Any recommendations or advice would be greatly appreciated. Thank you

  27. Please let me know if you’re looking for a writer for your weblog. You have some really good articles and I think I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some material for your blog in exchange for a link back to mine. Please blast me an email if interested. Regards!

  28. I have been exploring for a little for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this website. Reading this information So i’m happy to convey that I have a very good uncanny feeling I discovered just what I needed. I most certainly will make certain to do not forget this web site and give it a glance on a constant basis.

  29. fantastic put up, very informative. I ponder why the other experts of this sector do not realize this. You must continue your writing. I am confident, you have a great readers’ base already!

  30. I’ve read a few good stuff here. Definitely worth bookmarking for revisiting. I surprise how much effort you put to create such a wonderful informative web site.

  31. I got good info from your blog

  32. I take pleasure in, cause I found exactly what I used to be looking for. You’ve ended my 4 day long hunt! God Bless you man. Have a great day. Bye

  33. I have learn several good stuff here. Definitely worth bookmarking for revisiting. I surprise how so much attempt you set to create this sort of excellent informative site.

  34. Great tremendous things here. I’m very happy to look your post. Thank you so much and i’m looking forward to touch you. Will you kindly drop me a e-mail?

  35. Fascinating blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Thank you

  36. the forum consensus: a clean week beats a single lucky day.

  37. I enjoy your writing style truly enjoying this internet site.

  38. Youre so cool! I dont suppose Ive read something like this before. So good to find any person with some unique thoughts on this subject. realy thanks for beginning this up. this web site is something that’s wanted on the net, someone with slightly originality. helpful job for bringing something new to the web!

  39. excellent post, very informative. I wonder why the other specialists of this sector do not notice this. You must continue your writing. I am sure, you’ve a great readers’ base already!

  40. you are really a good webmaster. The web site loading speed is amazing. It seems that you are doing any unique trick. Also, The contents are masterpiece. you have done a magnificent job on this topic!

  41. My brother recommended I might like this website. He was totally right. This put up actually made my day. You cann’t consider simply how so much time I had spent for this info! Thanks!

  42. JamesIdosy's avatar JamesIdosy says:

    canadian pharmacy 24h com: Canadian Tabs – canadianpharmacyworld

  43. ROYAL138's avatar ROYAL138 says:

    I got what you intend, regards for posting.Woh I am lucky to find this website through google.

  44. Albertjed's avatar Albertjed says:

    india pharmacy mail order: Indian Meds Delivery – reliable online pharmacy

  45. Would you be eager about exchanging links?

  46. ROYAL138's avatar ROYAL138 says:

    Hi! Would you mind if I share your blog with my zynga group? There’s a lot of folks that I think would really appreciate your content. Please let me know. Thank you

  47. JamesIdosy's avatar JamesIdosy says:

    top 10 pharmacies in india: indian pharmacy paypal – secure medical online pharmacy

  48. ledak388's avatar ledak388 says:

    Hey, you used to write fantastic, but the last several posts have been kinda boringK I miss your great writings. Past few posts are just a bit out of track! come on!

Comments are closed.