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. Your style is so unique compared to many other people. Thank you for publishing when you have the opportunity,Guess I will just make this bookmarked.2

  2. Merely wanna say that this is extremely helpful, Thanks for taking your time to write this.

  3. Hello, Neat post. There’s a problem together with your site in web explorer, would test thisK IE nonetheless is the marketplace leader and a huge component to other folks will leave out your wonderful writing because of this problem.

  4. kupitoto's avatar kupitoto says:

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

  5. obviously like your website however you have to test the spelling on quite a few of your posts. A number of them are rife with spelling problems and I find it very troublesome to tell the truth then again I’ll certainly come back again.

  6. I’ve learn a few just right stuff here. Certainly price bookmarking for revisiting. I wonder how much effort you put to make this type of great informative site.

  7. Thank you for sharing superb informations. Your website is so cool. I’m impressed by the details that you have on this blog. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for more articles. You, my friend, ROCK! I found just the information I already searched everywhere and simply couldn’t come across. What a perfect site.

  8. I like the helpful information you provide for your articles. I’ll bookmark your weblog and take a look at once more right here regularly. I’m moderately certain I’ll be informed plenty of new stuff right here! Best of luck for the next!

  9. slot pg's avatar slot pg says:

    Some really interesting points you have written.Assisted me a lot, just what I was searching for : D.

  10. bhai88's avatar bhai88 says:

    Great ?V I should definitely pronounce, impressed with your site. I had no trouble navigating through all tabs as well as related info ended up being truly simple to do to access. I recently found what I hoped for before you know it in the least. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your customer to communicate. Nice task..

  11. slot 4d's avatar slot 4d says:

    I’ve recently started a website, the information you provide on this web site has helped me greatly. Thanks for all of your time & work. “The achievements of an organization are the results of the combined effort of each individual.” by Vince Lombardi.

  12. Some truly nice and useful information on this site, also I believe the layout holds excellent features.

  13. An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

  14. wonderful points altogether, you simply won a emblem new reader. What could you suggest about your publish that you simply made a few days ago? Any positive?

  15. I’ve learn a few good stuff here. Certainly worth bookmarking for revisiting. I wonder how so much attempt you set to create the sort of wonderful informative website.

  16. I’m impressed by how smoothly you conveyed the information, making everything clear and organized.

    https://www.geturcollege.com/rvit/

  17. Thanks for another fantastic post. Where else could anybody get that kind of information in such a perfect way of writing? I have a presentation next week, and I’m on the look for such information.

  18. Hiya, I am really glad I’ve found this info. Nowadays bloggers publish just about gossips and net and this is really annoying. A good website with exciting content, this is what I need. Thank you for keeping this web-site, I will be visiting it. Do you do newsletters? Can not find it.

  19. I have been absent for some time, but now I remember why I used to love this blog. Thank you, I will try and check back more frequently. How frequently you update your site?

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

  21. qjoker123's avatar qjoker123 says:

    Hi would you mind letting me know which webhost you’re using? 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 suggest a good internet hosting provider at a fair price? Many thanks, I appreciate it!

  22. dewa100's avatar dewa100 says:

    hello!,I really like your writing very a lot! percentage we be in contact more about your article on AOL? I require an expert in this space to unravel my problem. Maybe that is you! Taking a look forward to peer you.

  23. slot gacor's avatar slot gacor says:

    It is in point of fact a nice and helpful piece of info. I am glad that you just shared this helpful information with us. Please keep us up to date like this. Thank you for sharing.

  24. slot gacor's avatar slot gacor says:

    I gotta favorite this internet site it seems very beneficial invaluable

  25. bokep's avatar bokep says:

    Absolutely written content, Really enjoyed reading through.

  26. Hey very nice web site!! Man .. Beautiful .. Amazing .. I’ll bookmark your web site and take the feeds also…I’m happy to find numerous useful info here in the post, we need develop more techniques in this regard, thanks for sharing. . . . . .

  27. Hello there, just became alert to your blog through Google, and found that it is truly informative. I am going to watch out for brussels. I will appreciate if you continue this in future. A lot of people will be benefited from your writing. Cheers!

  28. Excellent site. A lot of useful info here. I am sending it to some pals ans additionally sharing in delicious. And of course, thanks to your effort!

  29. slotbuzz's avatar slotbuzz says:

    I’m still learning from you, as I’m making my way to the top as well. I absolutely love reading all that is written on your site.Keep the aarticles coming. I loved it!

  30. I think you have noted some very interesting details, thankyou for the post.

  31. You should take part in a contest for one of the best blogs on the web. I will recommend this site!

  32. Keep up the superb work, I read few blog posts on this site and I conceive that your blog is real interesting and contains circles of wonderful information.

  33. olxtoto's avatar olxtoto says:

    You made several nice points there. I did a search on the theme and found the majority of folks will go along with with your blog.

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

  35. Simply desire to say your article is as surprising. The clarity for your post is just nice and i can suppose you are knowledgeable in this subject. Fine with your permission let me to seize your RSS feed to keep up to date with approaching post. Thanks one million and please continue the rewarding work.

  36. qjoker123's avatar qjoker123 says:

    Great wordpress blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you! take care

  37. You are my intake, I possess few web logs and infrequently run out from to brand : (.

  38. vigorjp's avatar vigorjp says:

    Your place is valueble for me. Thanks!…

  39. tuan kuda's avatar tuan kuda says:

    Whats up this is kind of of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience. Any help would be enormously appreciated!

  40. I have recently started a site, the information you offer on this site has helped me tremendously. Thanks for all of your time & work.

  41. Heya! I just wanted to ask if you ever have any trouble with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no data backup. Do you have any methods to stop hackers?

  42. slot online's avatar slot online says:

    hello!,I really like your writing very so much! percentage we keep in touch more approximately your post on AOL? I need a specialist in this area to unravel my problem. Maybe that is you! Looking ahead to look you.

  43. Excellent blog you have here but I was curious if you knew of any message boards that cover the same topics talked about here? I’d really like to be a part of online community where I can get feed-back from other knowledgeable individuals that share the same interest. If you have any suggestions, please let me know. Cheers!

  44. kilat333's avatar kilat333 says:

    Good day! This is my first comment here so I just wanted to give a quick shout out and say I genuinely enjoy reading your posts. Can you recommend any other blogs/websites/forums that deal with the same topics? Thank you so much!

  45. Hi there, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can recommend? I get so much lately it’s driving me mad so any assistance is very much appreciated.

  46. brewhouse's avatar brewhouse says:

    I like what you guys are up too. Such smart work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my web site :).

  47. Neurocept's avatar Neurocept says:

    I conceive you have remarked some very interesting details , thankyou for the post.

  48. alfutuh's avatar alfutuh says:

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

  49. Brokerage's avatar Brokerage says:

    Respect to op, some wonderful selective information.

  50. Nervital's avatar Nervital says:

    Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any feed-back would be greatly appreciated.

Comments are closed.