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. Thanks for every other fantastic article. Where else may anyone get that type of info in such a perfect manner of writing? I’ve a presentation next week, and I am at the look for such information.

  2. Excellent website. A lot of useful info here. I’m sending it to several pals ans also sharing in delicious. And certainly, thanks in your effort!

  3. 88dewi's avatar 88dewi says:

    I’d perpetually want to be update on new blog posts on this web site, saved to my bookmarks! .

  4. Sweet web site, super style and design, really clean and apply genial.

  5. dewi288's avatar dewi288 says:

    Very interesting topic, regards for putting up.

  6. Its such as you learn my mind! You seem to know a lot about this, such as you wrote the e-book in it or something. I think that you just can do with a few to pressure the message house a bit, but other than that, that is fantastic blog. An excellent read. I will definitely be back.

  7. dwg288's avatar dwg288 says:

    I’ve recently started a blog, the information you provide on this web site has helped me greatly. Thank you for all of your time & work.

  8. dewi288's avatar dewi288 says:

    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!

  9. dewi188's avatar dewi188 says:

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

  10. Normally I don’t read article on blogs, but I wish to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, quite nice article.

  11. ledak788's avatar ledak788 says:

    Thanks for another informative web site. Where else could I get that kind of info written in such an ideal way? I have a project that I’m just now working on, and I have been on the look out for such info.

  12. Andrewwem's avatar Andrewwem says:

    india pharmacy: Global India Pharmacy – online shopping pharmacy india

  13. dewi11's avatar dewi11 says:

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You obviously know what youre talking about, why throw away your intelligence on just posting videos to your blog when you could be giving us something enlightening to read?

  14. dewi11's avatar dewi11 says:

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

  15. ovalpool's avatar ovalpool says:

    Hola! I’ve been reading your weblog for some time now and finally got the courage to go ahead and give you a shout out from Lubbock Tx! Just wanted to mention keep up the good job!

  16. Gabrielhax's avatar Gabrielhax says:

    https://globalindiapharmacy.shop/# top 10 pharmacies in india

  17. ledak388's avatar ledak388 says:

    I cling on to listening to the news bulletin speak about receiving free online grant applications so I have been looking around for the best site to get one. Could you advise me please, where could i get some?

  18. It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  19. syair macau's avatar syair macau says:

    Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, 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, fantastic blog!

  20. Magnificent web site. Plenty of useful information here. I am sending it to several friends ans also sharing in delicious. And obviously, thanks for your sweat!

  21. link togel's avatar link togel says:

    Would you be desirous about exchanging links?

  22. bandar toto's avatar bandar toto says:

    Hello my family member! I wish to say that this post is amazing, nice written and come with approximately all vital infos. I’d like to peer extra posts like this .

  23. syair macau's avatar syair macau says:

    I’ve read some good stuff here. Certainly value bookmarking for revisiting. I surprise how so much attempt you put to make any such great informative website.

  24. slot resmi's avatar slot resmi says:

    Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch since I found it for him smile So let me rephrase that: Thanks for lunch! “There are places and moments in which one is so completely alone that one sees the world entire.” by Jules Renard.

  25. link togel's avatar link togel says:

    Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, 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, fantastic blog!

  26. toto macau's avatar toto macau says:

    naturally like your web-site but you need to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I in finding it very troublesome to inform the reality then again I will surely come again again.

  27. slot resmi's avatar slot resmi says:

    Those are yours alright! . We at least need to get these people stealing images to start blogging! They probably just did a image search and grabbed them. They look good though!

  28. As I web site possessor I believe the content material here is rattling fantastic , appreciate it for your efforts. You should keep it up forever! Best of luck.

  29. macau's avatar macau says:

    This site is mostly a stroll-via for all the info you wished about this and didn’t know who to ask. Glimpse here, and you’ll positively discover it.

  30. rajabandot's avatar rajabandot says:

    Hey very nice site!! Guy .. Beautiful .. Wonderful .. I’ll bookmark your website and take the feeds also…I am glad to find a lot of helpful info right here in the publish, we’d like develop extra strategies in this regard, thank you for sharing.

  31. senangslot's avatar senangslot says:

    hello!,I like your writing very much! share we communicate more about your post on AOL? I need an expert on this area to solve my problem. May be that’s you! Looking forward to see you.

  32. You really make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complicated and very broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  33. I conceive other website proprietors should take this website as an model, very clean and wonderful user friendly style.

  34. You are my inhalation, I have few blogs and infrequently run out from brand :). “Follow your inclinations with due regard to the policeman round the corner.” by W. Somerset Maugham.

  35. Hi, Neat post. There’s a problem with your site in internet explorer, would check this… IE still is the market leader and a big portion of people will miss your excellent writing due to this problem.

  36. You have remarked very interesting points! ps nice site. “I hate music, especially when it’s played.” by Jimmy Durante.

  37. Rattling instructive and fantastic complex body part of subject matter, now that’s user pleasant (:.

  38. BrianLYDAY's avatar BrianLYDAY says:

    prednisone cost canada: prednisone for allergies and asthma – prednisone 5 mg tablet

  39. dwg288's avatar dwg288 says:

    This website is mostly a walk-by for all of the information you wished about this and didn’t know who to ask. Glimpse here, and you’ll undoubtedly uncover it.

  40. 3z download's avatar 3z download says:

    Hi there! I just wish to give an enormous thumbs up for the nice data you’ve right here on this post. I shall be coming again to your blog for more soon.

  41. gt108's avatar gt108 says:

    With havin so much written content do you ever run into any issues of plagorism or copyright infringement? My site has a lot of exclusive content I’ve either written myself or outsourced but it seems a lot of it is popping it up all over the web without my agreement. Do you know any solutions to help protect against content from being ripped off? I’d really appreciate it.

  42. serieday's avatar serieday says:

    I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!

  43. slot mahjong's avatar slot mahjong says:

    Some really fantastic articles on this site, regards for contribution. “A man with a new idea is a crank — until the idea succeeds.” by Mark Twain.

  44. F*ckin’ amazing things here. I am very satisfied to see your post. Thanks so much and i am looking ahead to contact you. Will you kindly drop me a mail?

  45. togel online's avatar togel online says:

    Its fantastic as your other blog posts : D, appreciate it for putting up.

  46. bulansabit's avatar bulansabit says:

    Saya mengunjungi banyak website tetapi saya percaya yang satu ini punya sesuatu yang ekstra di dalamnya

  47. Hello my friend! I wish to say that this post is awesome, nice written and include almost all vital infos. I’d like to see more posts like this.

  48. dwg288's avatar dwg288 says:

    It is truly a nice and helpful piece of info. I’m happy that you shared this useful info with us. Please keep us informed like this. Thank you for sharing.

Comments are closed.