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. Admiring the time and energy you put into your site and in depth information you offer. It’s nice to come across a blog every once in a while that isn’t the same old rehashed material. Fantastic read! I’ve bookmarked your site and I’m including your RSS feeds to my Google account.

  2. Of course, what a magnificent website and revealing posts, I surely will bookmark your blog.Have an awsome day!

  3. It is the best time to make some plans for the future and it’s time to be happy. I have read this post and if I could I wish to suggest you few interesting things or advice. Perhaps you can write next articles referring to this article. I desire to read more things about it!

  4. Enjoyed studying this, very good stuff, thankyou.

  5. Lovely just what I was looking for.Thanks to the author for taking his time on this one.

  6. I would like to thank you for the efforts you’ve put in writing this web site. I am hoping the same high-grade web site post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Actually the blogging is spreading its wings rapidly. Your write up is a good example of it.

  7. This is a topic close to my heart cheers, where are your contact details though?

  8. I have to show appreciation to the writer for bailing me out of this scenario. Because of surfing through the the net and seeing solutions that were not beneficial, I figured my entire life was well over. Living without the strategies to the issues you’ve sorted out all through the review is a crucial case, and the ones which could have in a negative way affected my career if I hadn’t discovered your blog. Your skills and kindness in dealing with all the things was very useful. I’m not sure what I would’ve done if I hadn’t come upon such a point like this. I am able to now look forward to my future. Thanks so much for your high quality and amazing help. I will not think twice to propose the sites to any individual who desires assistance on this topic.

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

  10. Hello very cool website!! Guy .. Beautiful .. Amazing .. I’ll bookmark your blog and take the feeds additionally…I am happy to seek out numerous helpful information right here in the publish, we’d like work out more strategies on this regard, thanks for sharing. . . . . .

  11. Burnslim's avatar Burnslim says:

    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.

  12. situs byd108's avatar situs byd108 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.

  13. Very good written story. It will be supportive to anyone who usess it, as well as yours truly :). Keep up the good work – for sure i will check out more posts.

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

  15. I am no longer certain where you’re getting your info, however great topic. I needs to spend some time studying much more or figuring out more. Thank you for fantastic information I was looking for this info for my mission.

  16. Do you have a spam problem on this website; I also am a blogger, and I was wondering your situation; we have created some nice procedures and we are looking to swap strategies with others, please shoot me an e-mail if interested.

  17. I was studying some of your articles on this site and I believe this web site is very informative ! Continue putting up.

  18. This is a very good tips especially to those new to blogosphere, brief and accurate information… Thanks for sharing this one. A must read article.

  19. Thank you, I have just been looking for info approximately this subject for a while and yours is the best I’ve came upon till now. However, what about the conclusion? Are you positive about the supply?

  20. Thank you for some other wonderful article. The place else may just anybody get that kind of information in such a perfect means of writing? I’ve a presentation next week, and I am at the search for such information.

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

  22. suster slot's avatar suster slot says:

    Does your site have a contact page? I’m having trouble locating it but, I’d like to send you an e-mail. I’ve got some recommendations for your blog you might be interested in hearing. Either way, great blog and I look forward to seeing it improve over time.

  23. Hi, I think your site might be having browser compatibility issues. When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, awesome blog!

  24. I’ve been absent for some time, but now I remember why I used to love this blog. Thanks , I’ll try and check back more often. How frequently you update your site?

  25. I conceive this site contains some really excellent info for everyone :D.

  26. fitspresso's avatar fitspresso says:

    I appreciate, cause I found just what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a nice day. Bye

  27. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks a lot!

  28. You have observed very interesting details ! ps nice site.

  29. I have read several good stuff here. Certainly worth bookmarking for revisiting. I wonder how much effort you put to create such a excellent informative web site.

  30. you are really a good webmaster. The site loading speed is incredible. It seems that you are doing any unique trick. Moreover, The contents are masterwork. you have done a great job on this topic!

  31. Some truly nice stuff on this internet site, I like it.

  32. Along with every little thing that seems to be building inside this specific area, many of your points of view are generally very stimulating. Having said that, I beg your pardon, but I do not subscribe to your whole theory, all be it refreshing none the less. It would seem to us that your comments are not totally validated and in simple fact you are yourself not even completely confident of your argument. In any event I did take pleasure in looking at it.

  33. pacu togel's avatar pacu togel says:

    I love your blog.. very nice colors & theme. Did you create this website yourself? Plz reply back as I’m looking to create my own blog and would like to know wheere u got this from. thanks

  34. alexaslot138's avatar alexaslot138 says:

    I always was concerned in this topic and stock still am, thanks for putting up.

  35. hokibos898's avatar hokibos898 says:

    Enjoyed examining this, very good stuff, regards. “Talk sense to a fool and he calls you foolish.” by Euripides.

  36. Hey would you mind letting me know which webhost you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a fair price? Cheers, I appreciate it!

  37. Hello would you mind letting me know which web host you’re using? I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most. Can you recommend a good internet hosting provider at a fair price? Thanks a lot, I appreciate it!

  38. I like what you guys are up also. Such clever work and reporting! Carry on the excellent works guys I have incorporated you guys to my blogroll. I think it’ll improve the value of my website 🙂

  39. Some times its a pain in the ass to read what people wrote but this website is real user pleasant! .

  40. My brother suggested I might like this website. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  41. prodentim's avatar prodentim says:

    My spouse and i were now ecstatic when Chris managed to conclude his reports from your precious recommendations he acquired from your web pages. It’s not at all simplistic just to find yourself giving out information and facts which often a number of people might have been making money from. We take into account we’ve got the website owner to be grateful to because of that. All of the explanations you’ve made, the easy web site menu, the relationships you aid to promote – it’s got most astonishing, and it is facilitating our son and the family do think that subject is cool, and that’s truly serious. Many thanks for the whole thing!

  42. You really make it seem so easy along with your presentation however I in finding this matter to be really one thing that I think I would never understand. It seems too complex and very extensive for me. I am looking forward on your next put up, I will attempt to get the cling of it!

  43. As I web-site possessor I believe the content matter here is rattling excellent , appreciate it for your hard work. You should keep it up forever! Good Luck.

  44. After all, what a great site and informative posts, I will upload inbound link – bookmark this web site? Regards, Reader.

  45. Some really nice and utilitarian information on this website, too I conceive the layout has got good features.

  46. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  47. audifort's avatar audifort says:

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

  48. Some genuinely interesting information, well written and broadly speaking user genial.

  49. I liked as much as you will obtain carried out right here. The comic strip is tasteful, your authored material stylish. nonetheless, you command get bought an shakiness over that you would like be handing over the following. ill undoubtedly come further in the past again since precisely the same just about very ceaselessly within case you protect this increase.

Comments are closed.