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

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. Liberty is a Senior AI Engineer at the University of Pittsburgh Medical Center, and 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 21 year Microsoft MVP.
This entry was posted in APIs. Bookmark the permalink.

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

  1. jackwin77's avatar jackwin77 says:

    Some really superb content on this website, regards for contribution. “When he has ceased to hear the many, he may discern the One – the inner sound which kills the outer.” by H Hahn Blavatsky.

  2. Excellent goods from you, man. I’ve understand your stuff previous to and you’re just extremely fantastic. I actually like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I cant wait to read much more from you. This is actually a wonderful web site.

  3. rajabandot's avatar rajabandot says:

    Pretty nice post. I simply stumbled upon your weblog and wanted to mention that I’ve truly enjoyed browsing your weblog posts. In any case I’ll be subscribing for your rss feed and I’m hoping you write once more very soon!

  4. Well I truly liked reading it. This subject offered by you is very helpful for accurate planning.

  5. When I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a remark is added I get 4 emails with the same comment. Is there any approach you’ll be able to remove me from that service? Thanks!

  6. bandarangka's avatar bandarangka says:

    Nice blog here! Also your website loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my site loaded up as quickly as yours lol

  7. Really nice design and style and superb subject matter, absolutely nothing else we want : D.

  8. You have brought up a very wonderful points, thankyou for the post.

  9. That is really attention-grabbing, You are a very professional blogger. I have joined your feed and sit up for seeking more of your excellent post. Additionally, I’ve shared your site in my social networks!

  10. Wow that was unusual. I just wrote an very long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say great blog!

  11. What i do not realize is in truth how you are no longer actually a lot more neatly-liked than you might be right now. You’re so intelligent. You know thus considerably with regards to this topic, made me individually believe it from numerous numerous angles. Its like men and women aren’t interested except it?¦s one thing to accomplish with Woman gaga! Your own stuffs nice. All the time maintain it up!

  12. Yeah bookmaking this wasn’t a risky conclusion outstanding post! .

  13. ADA ADAD's avatar ADA ADAD says:

    relx 電子菸以「穩定霧化、口感真實」著稱。其核心技術採用陶瓷霧化芯,能精準控制溫度,避免焦味與漏油問題。

  14. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your website? My blog is in the exact same area of interest as yours and my users would really benefit from some of the information you provide here. Please let me know if this ok with you. Many thanks!

  15. I simply desired to thank you so much once more. I’m not certain the things that I might have tried in the absence of the type of ideas shown by you directly on my theme. It has been an absolute difficult problem in my circumstances, nevertheless being able to see the well-written fashion you processed the issue forced me to weep over fulfillment. I am just happier for the service and pray you know what an amazing job you happen to be accomplishing educating other individuals thru a web site. Probably you’ve never come across all of us.

  16. Whats Going down i’m new to this, I stumbled upon this I’ve discovered It absolutely helpful and it has aided me out loads. I hope to contribute & help other users like its aided me. Great job.

  17. Hello.This post was extremely remarkable, particularly because I was searching for thoughts on this matter last Tuesday.

  18. Greetings! Very helpful advice on this article! It is the little changes that make the biggest changes. Thanks a lot for sharing!

  19. nagaempire's avatar nagaempire says:

    I have learn a few excellent stuff here. Definitely price bookmarking for revisiting. I wonder how so much effort you set to create such a fantastic informative site.

  20. sms808's avatar sms808 says:

    Very interesting info !Perfect just what I was looking for!

  21. waralaba's avatar waralaba says:

    Way cool, some valid points! I appreciate you making this article available, the rest of the site is also high quality. Have a fun.

  22. I respect your piece of work, thankyou for all the informative blog posts.

  23. Some genuinely howling work on behalf of the owner of this site, absolutely outstanding subject material.

  24. sakongtoto's avatar sakongtoto says:

    I want studying and I conceive this website got some genuinely utilitarian stuff on it! .

  25. I’d always want to be update on new posts on this internet site, saved to fav! .

  26. There are some interesting cut-off dates on this article but I don’t know if I see all of them middle to heart. There is some validity however I’ll take maintain opinion until I look into it further. Good article , thanks and we wish extra! Added to FeedBurner as effectively

  27. kids sofa's avatar kids sofa says:

    Howdy just wanted to give you a quick heads up. The words in your post seem to be running off the screen in Chrome. I’m not sure if this is a format issue or something to do with web browser compatibility but I figured I’d post to let you know. The layout look great though! Hope you get the problem resolved soon. Kudos

  28. You are my inspiration , I own few blogs and occasionally run out from to post .

  29. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across. It extremely helps make reading your blog significantly easier.

  30. Fantastic beat ! I would like to apprentice while you amend your web site, how can i subscribe for a weblog site? The account aided me a appropriate deal. I had been tiny bit familiar of this your broadcast offered brilliant clear idea

  31. Hi, i think that i saw you visited my blog so i came to “return the favor”.I’m attempting to find things to enhance my web site!I suppose its ok to use a few of your ideas!!

  32. Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Actually Fantastic. I am also a specialist in this topic so I can understand your effort.

  33. Would love to perpetually get updated great blog! .

  34. I just like the helpful information you supply for your articles. I’ll bookmark your blog and check again here regularly. I am quite sure I will be informed many new stuff proper here! Best of luck for the following!

  35. You are my intake, I own few web logs and often run out from to post .

  36. sakongtoto's avatar sakongtoto says:

    Everything is very open and very clear explanation of issues. was truly information. Your website is very useful. Thanks for sharing.

  37. Flash Burn is a revolutionary natural supplement that has been transforming the lives of thousands of people struggling with excess weight. Developed with a 100 natural and scientifically proven formula

  38. I just couldn’t depart your web site before suggesting that I actually enjoyed the standard information a person provide in your visitors? Is gonna be again often in order to inspect new posts

  39. Have you heard about ThyraFemme Balance? This natural supplement is becoming a sensation among women worldwide because it provides a real, safe, and natural solution for those struggling with hormonal imbalance, slow metabolism, weight gain, fatigue, and lack of energy.

  40. Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.

  41. hepatoburn's avatar hepatoburn says:

    Appreciate it for all your efforts that you have put in this. very interesting information.

  42. I¦ve learn a few excellent stuff here. Definitely price bookmarking for revisiting. I surprise how much effort you place to make such a wonderful informative site.

  43. Hi my loved one! I want to say that this article is awesome, nice written and come with almost all important infos. I would like to see more posts like this .

  44. fantastic post, very informative. I wonder why the other experts of this sector don’t notice this. You should continue your writing. I’m sure, you’ve a huge readers’ base already!

  45. sakong toto's avatar sakong toto says:

    Regards for all your efforts that you have put in this. very interesting info .

  46. Im no longer certain where you’re getting your info, but great topic. I must spend a while finding out much more or figuring out more. Thank you for fantastic info I used to be searching for this information for my mission.

  47. alexaslot138's avatar alexaslot138 says:

    I have recently started a website, the info you provide on this web site has helped me greatly. Thank you for all of your time & work.

  48. Very great post. I simply stumbled upon your blog and wished to mention that I’ve truly loved browsing your weblog posts. In any case I’ll be subscribing on your rss feed and I’m hoping you write again very soon!

  49. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how could we communicate?

Comments are closed.