Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Use ASP.NET Core Options with an instance

Use ASP.NET Core Options with an instance
June 10, 2022 3 min read BEN ABT
.NET Asptnetcore

This article was written in 2022. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.

ASP.NET Core provides a built-in mechanism for configuring an application’s options using the Options pattern. This pattern allows you to define a class to represent the options you want to configure and then bind those options to values from a variety of sources, such as configuration files, environment variables or command-line arguments.

One way to use the Options pattern is to configure an object instance using the Options.Create() method. This method creates an instance of a class and assigns its properties the values from the configuration sources.

Here’s an example of how to use Options.Create() to configure an object instance in an ASP.NET Core application:

Define a class to represent the options you want to configure. This class should have properties for each option, with default values if necessary.

C#
1public class MyOptions
2{
3    public string Option1 { get; set; } = "default value";
4    public int Option2 { get; set; } = 42;
5}

In your ASP.NET Core application’s Startup class, add the options class to the service collection using the services.Configure() method. This will allow the options to be injected into controllers and other services.

C#
1public void ConfigureServices(IServiceCollection services)
2{
3    services.Configure<MyOptions>(Configuration);
4}

Inject an instance of the options class into a controller or service using dependency injection.

C#
 1public class MyController : Controller
 2{
 3    private readonly MyOptions _options;
 4
 5    public MyController(IOptions<MyOptions> options)
 6    {
 7        _options = options.Value;
 8    }
 9
10    public IActionResult Index()
11    {
12        return View(_options);
13    }
14}

Use the Options.Create() method to create an instance of the options class and configure it with values from the configuration sources.

C#
IOptions<MyOptions> myOptionsInstance = Options.Create(new MyOptions());

This will create an instance of MyOptions and assign its properties the values from the configuration sources defined in the Startup class. You can also pass an anonymous object to the Options.Create() method to override the default values for specific options.

C#
1IOptions<MyOptions> myOptionsInstance = Options.Create(new MyOptions
2{
3    Option1 = "overridden value",
4});

This will create an instance of MyOptions with the Option1 property set to “overridden value” and the Option2 property set to its default value of 42.

Using the Options.Create() method is a convenient way to configure an object instance with values from the configuration sources in an ASP.NET Core application. It allows you to easily bind options to values from a variety of sources and inject those options into controllers and other services using dependency injection.

To use this in your dependency injection, you can just use a singleton - because that’s what services.Configure() under the hood does, too.

C#
1public void ConfigureServices(IServiceCollection services)
2{
3    IOptions<MyOptions> myOptionsInstance = Options.Create(new MyOptions());
4    services.AddSingleton(myOptionsInstance);
5}

In conclusion, the Options pattern in ASP.NET Core provides a flexible and convenient way to configure an application’s options using a variety of sources. The Options.Create() method allows you to create an instance of a class and bind its properties to values from the configuration sources, making it easy to inject those options into controllers and other services using dependency injection. Whether you need to configure a single object instance or multiple instances with different values, the Options pattern and the Options.Create() method provide a powerful and flexible solution.

Related articles

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026 - 11 min read

Containerizing .NET in 2026 Without a Dockerfile

Most .NET Dockerfiles repeat the same five steps: restore, publish, select the matching Microsoft runtime image, copy the output and start …

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 12 min read

Problem Details in ASP.NET Core and .NET 11

builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026 - 11 min read

Resilience in .NET with Microsoft.Extensions.Resilience

Retries are the most visible resilience strategy and the easiest one to misuse. Repeating a failing call can recover from a short network …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact