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. each time i used to read smaller articles or reviews that also clear their motive, and that is also happening with this post which I am reading now.

  2. Clora Wescom's avatar Clora Wescom says:

    May I just say what a comfort to discover a person that actually understands what they’re talking about on the net. You actually realize how to bring an issue to light and make it important. A lot more people have to look at this and understand this side of the story. I can’t believe you’re not more popular since you definitely possess the gift.

  3. I have not checked in here for some time because I thought it was getting boring, but the last few posts are good quality so I guess I’ll add you back to my daily bloglist. You deserve it my friend 🙂

  4. Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such magnificent info being shared freely out there.

  5. Great series, Jesse. The section on using `IOptionsSnapshot` for scoped configuration was particularly helpful—I’ve been wrestling with reloading config in my own microservices project. Do you find any performance trade-offs with `IOptionsMonitor` in high-traffic scenarios compared to the snapshot approach? Thanks for breaking it down so clearly.

  6. Blair Ivey's avatar Blair Ivey says:

    At this time it sounds like Expression Engine is the best blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog?

  7. Greate article. Keep posting such kind of information on your site. Im really impressed by your blog.

  8. May I simply just say what a relief to find a person that actually knows what they’re discussing on the net. You definitely realize how to bring a problem to light and make it important. More people really need to read this and understand this side of your story. I can’t believe you aren’t more popular given that you most certainly possess the gift.

  9. May I simply say what a comfort to uncover someone who really understands what they are talking about on the web. You actually understand how to bring an issue to light and make it important. More people must look at this and understand this side of the story. It’s surprising you’re not more popular since you certainly have the gift.

  10. I like this web site its a master peace ! Glad I found this on google .

  11. sync swap never choked during volume

  12. At this time it appears like BlogEngine is the best blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  13. memocept's avatar memocept says:

    I’ve been exploring for a little bit for any high-quality articles or blog posts in this sort of house . Exploring in Yahoo I at last stumbled upon this website. Studying this information So i’m glad to show that I’ve an incredibly excellent uncanny feeling I found out exactly what I needed. I so much surely will make sure to don’t forget this web site and give it a look regularly.

  14. Hi there just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading properly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.

  15. At this time it sounds like BlogEngine is the preferred blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  16. Weston Kozma's avatar Weston Kozma says:

    Aw, this was a really good post. Spending some time and actual effort to produce a great article… but what can I say… I hesitate a lot and never seem to get anything done.

  17. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this. Please let me know if you run into anything. I truly enjoy reading your blog and I look forward to your new updates.

  18. defi news's avatar defi news says:

    sharing some decent crypto links here
    [url]https://quickswap-page.github.io/[/url]
    [url]https://polygon-bridge-fees.github.io/[/url]
    [url]https://retro-bridge-app.github.io/[/url]
    [url]https://rhino-bridge-site.github.io/[/url]
    [url]https://polygon-bridge-app.github.io/[/url]
    [url]https://renbridge-protocol.github.io/[/url]
    [url]https://polymarket-airdrop.github.io/[/url]
    [url]https://rango-exchange-page.github.io/[/url]
    [url]https://polygon-staking-app.github.io/[/url]
    [url]https://polygon-staking-calculator.github.io/[/url]
    [url]https://pulsechain-bridge.github.io/[/url]
    [url]https://polymarket-site.github.io/[/url]
    [url]https://poocoin-app.github.io/[/url]
    [url]https://polygonbridge-v1.github.io/[/url]
    [url]https://ren-bridge.github.io/[/url]

  19. judi bola's avatar judi bola says:

    Hi there, I found your website via Google while searching for a related topic, your site came up, it looks great. I have bookmarked it in my google bookmarks.

  20. Magnificent beat ! I would like to apprentice while you amend your website, how can i subscribe for a blog website? The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast offered bright clear concept

  21. Kathey's avatar Kathey says:

    Greate post. Keep writing such kind of info on your page. Im really impressed by your blog.

  22. bos138 slot's avatar bos138 slot says:

    I was very pleased to find this web-site.I wanted to thanks for your time for this wonderful read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you blog post.

  23. I have learn some good stuff here. Definitely value bookmarking for revisiting. I wonder how a lot effort you set to create the sort of magnificent informative web site.

  24. Francesca's avatar Francesca says:

    Right now it looks like WordPress is the preferred blogging platform out there right now. (from what I’ve read) Is that what you are using on your blog?

  25. Greetings from Florida! I’m bored to tears at work so I decided to browse your site on my iphone during lunch break. I love the information you provide here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my cell phone .. I’m not even using WIFI, just 3G .. Anyways, wonderful blog!

  26. block daily's avatar block daily says:

    some solid crypto projects worth a look
    [url]https://bridge-weth.github.io/[/url]
    [url]https://bridge-to-polygon.github.io/[/url]
    [url]https://bridge-mantle.github.io/[/url]
    [url]https://basebridge.github.io/[/url]
    [url]https://bridge-blast.github.io/[/url]
    [url]https://bridge-crypto.github.io/[/url]
    [url]https://best-crypto-app.github.io/[/url]
    [url]https://best-crypto-trading-bot.github.io/[/url]
    [url]https://bridge-wbtc.github.io/[/url]
    [url]https://bridge-optimism.github.io/[/url]
    [url]https://babylon-staking.github.io/[/url]
    [url]https://bridge-ethereum.github.io/[/url]
    [url]https://bridge-base.github.io/[/url]
    [url]https://best-crypto-trading-site.github.io/[/url]
    [url]https://bridge-scroll.github.io/[/url]

  27. Janyce's avatar Janyce says:

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

  28. hoki138's avatar hoki138 says:

    certainly like your web site but you need to check the spelling on quite a few of your posts. Several of them are rife with spelling issues and I find it very bothersome to tell the truth nevertheless I’ll surely come back again.

  29. Clyde's avatar Clyde says:

    Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyways, just wanted to say wonderful blog!

  30. asterdex fan's avatar asterdex fan says:

    asterdex handled my longs smoothly

  31. Jeannette's avatar Jeannette says:

    Currently it appears like WordPress is the best blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?

  32. deposit 5k's avatar deposit 5k says:

    Superb blog! Do you have any tips for aspiring writers? I’m hoping to start my own site soon but I’m a little lost on everything. Would you recommend starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m completely confused .. Any tips? Many thanks!

  33. I like this site its a master peace ! Glad I discovered this on google .

  34. Delta 10 THC's avatar Delta 10 THC 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.

  35. of course like your website but you have to check the spelling on several of your posts. Several of them are rife with spelling problems and I find it very troublesome to tell the truth nevertheless I’ll certainly come back again.

  36. olxtoto link's avatar olxtoto link says:

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your site? My blog site is in the exact same niche as yours and my users would certainly benefit from a lot of the information you provide here. Please let me know if this alright with you. Appreciate it!

  37. Greate article. Keep posting such kind of information on your blog. Im really impressed by your site.

  38. olxtoto's avatar olxtoto says:

    It’s really a cool and useful piece of information. I am satisfied that you simply shared this useful information with us. Please stay us informed like this. Thanks for sharing.

Comments are closed.