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. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty is a Senior SW Engineer for CNH and he was 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 Microsoft MVP.
This entry was posted in APIs. Bookmark the permalink.

963 Responses to APIs with C# – Part 4 Options

  1. vigorjp's avatar vigorjp says:

    Your place is valueble for me. Thanks!…

  2. 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!

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

  4. 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?

  5. 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.

  6. 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!

  7. 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!

  8. 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.

  9. 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 :).

  10. Neurocept's avatar Neurocept says:

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

  11. alfutuh's avatar alfutuh says:

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

  12. Brokerage's avatar Brokerage says:

    Respect to op, some wonderful selective information.

  13. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *