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. BryceTew's avatar BryceTew says:

    how much semaglutide for weight loss glp-1 agonists

  2. Rodneybox's avatar Rodneybox says:

    generic liraglutide glp-1 agonists

  3. TimothySet's avatar TimothySet says:

    semaglutide georgia us obesity science

  4. Great article, Jesse. The way you break down the different options for configuring APIs in C# really clarifies when to use each approach. I’ve been experimenting with the Options pattern in my own project, and your explanation of named options in particular helped me avoid some confusion.

  5. I was reading some of your articles on this website and I believe this website is very informative ! Keep on posting.

  6. I do not even understand how I ended up right here, but I believed this put up was great. I do not know who you might be but certainly you’re going to a well-known blogger in case you aren’t already 😉 Cheers!

  7. Woah! I’m really enjoying the template/theme of this blog. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and visual appeal. I must say you have done a very good job with this. In addition, the blog loads very quick for me on Chrome. Excellent Blog!

  8. constantly i used to read smaller articles or reviews that also clear their motive, and that is also happening with this paragraph which I am reading at this time.

  9. Hey There. I discovered your blog the usage of msn. This is an extremely smartly written article. I will be sure to bookmark it and come back to read extra of your helpful information. Thank you for the post. I will certainly return.

  10. situs gacor's avatar situs gacor says:

    Hey, you used to write excellent, but the last few posts have been kinda boring?K I miss your super writings. Past few posts are just a bit out of track! come on!

  11. Its like you learn my thoughts! You seem to grasp a lot approximately this, such as you wrote the e book in it or something. I feel that you can do with some p.c. to drive the message home a bit, however other than that, this is wonderful blog. An excellent read. I will definitely be back.

  12. I appreciate, cause I found exactly what I was looking for. You’ve ended my 4 day long hunt! God Bless you man. Have a great day. Bye

  13. I am impressed with this website , really I am a big fan .

  14. BrianTed's avatar BrianTed says:

    us obesity science https://usobesityscience.org/glp-1-agonists/ side effects from semaglutide

  15. gatotkac's avatar gatotkac says:

    I’m typically to running a blog and i really admire your content. The article has actually peaks my interest. I am going to bookmark your site and maintain checking for new information.

  16. Some really excellent content on this web site, thanks for contribution.

  17. Thankyou for helping out, superb info .

  18. link mpo100's avatar link mpo100 says:

    I got what you intend,saved to fav, very decent web site.

  19. Keep working ,great job!

  20. Thank you for another informative blog. Where else could I get that kind of info written in such an ideal way? I’ve a project that I am just now working on, and I’ve been on the look out for such info.

  21. Discover how SBLC and Bank Guarantee monetization works in trade finance, including liquidity access, structured funding, risk management, and global trade financing solutions.

  22. senang4d's avatar senang4d says:

    Thanks a lot for sharing this with all of us you really know what you’re talking about! Bookmarked. Please also visit my web site =). We could have a link exchange contract between us!

  23. Saved as a favorite, I really like your blog!

  24. Good post. I learn something more challenging on totally different blogs everyday. It is going to at all times be stimulating to learn content from other writers and apply slightly something from their store. I’d want to make use of some with the content on my blog whether you don’t mind. Natually I’ll give you a hyperlink in your web blog. Thanks for sharing.

  25. I’m curious to find out what blog system you happen to be using? I’m experiencing some small security issues with my latest website and I’d like to find something more risk-free. Do you have any solutions?

  26. ClaytonRedge's avatar ClaytonRedge says:

    Cialis 20mg price http://mensrxguide.org/# Buy Cialis online

  27. olxtoto slot's avatar olxtoto slot says:

    Usually I do not learn post on blogs, but I wish to say that this write-up very forced me to take a look at and do it! Your writing style has been amazed me. Thanks, very great post.

  28. olxtoto slot's avatar olxtoto slot says:

    Very interesting points you have mentioned, regards for putting up. “I’ve made a couple of mistakes I’d like to do over.” by Jerry Coleman.

  29. ClintSig's avatar ClintSig says:

    Cheap Sildenafil 100mg viagra canada – Viagra Tablet price

  30. I am so happy to read this. This is the type of manual that needs to be given and not the accidental misinformation that’s at the other blogs. Appreciate your sharing this greatest doc.

  31. Donaldtix's avatar Donaldtix says:

    semaglutide semaglutide glp1 cheap semaglutide

  32. Jessegaf's avatar Jessegaf says:

    https://liraglutideglp1.com/# liraglutide pharmacy

  33. Merely wanna input on few general things, The website design and style is perfect, the articles is very fantastic : D.

  34. Whats up very nice website!! Man .. Beautiful .. Wonderful .. I will bookmark your site and take the feeds additionallyKI am glad to search out a lot of helpful info right here within the post, we need develop extra strategies in this regard, thank you for sharing. . . . . .

  35. Hello World's avatar Hello World says:

    Greetings! I know this is kinda off topic but I was wondering which blog platform are you using for this site? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be awesome if you could point me in the direction of a good platform.

  36. RogerEmbow's avatar RogerEmbow says:

    Viagra Tablet price buy viagra here generic sildenafil

  37. Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!

  38. I’ve been exploring for a bit for any high-quality articles or weblog posts in this kind of house . Exploring in Yahoo I at last stumbled upon this website. Studying this information So i am satisfied to convey that I’ve an incredibly just right uncanny feeling I came upon exactly what I needed. I such a lot unquestionably will make sure to don’t fail to remember this site and provides it a glance regularly.

  39. comprar pbn's avatar comprar pbn says:

    Thanks , I have just been looking for info approximately this topic for a long time and yours is the greatest I’ve found out so far. But, what in regards to the bottom line? Are you positive in regards to the source?

  40. hoki365's avatar hoki365 says:

    I couldn’t resist commenting

  41. ElmerPhich's avatar ElmerPhich says:

    https://stromectolvip.com/# ivermectin 10 ml

  42. madu138's avatar madu138 says:

    Hey! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I’m getting tired of WordPress because I’ve had problems with hackers and I’m looking at options for another platform. I would be great if you could point me in the direction of a good platform.

  43. KermitBaike's avatar KermitBaike says:

    https://freshpharm24.com/ medicine online order

Comments are closed.