0

I am using Options Pattern within Azure Function. I created custom classes to represent the config data.

Below is my settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
   
    "ServiceBusConfigOptions:EventTypeTopic": "",
    "ServiceBusConfigOptions:EventTypeTopicSubscription": "",
    "ServiceBusConfigOptions:ConnectionString": "",
    "ServiceBusConfigOptions:SendEmailTopic": "",
    "ServiceBusConfigOptions:NotificationMonitoringTopic": "",

    "EmailConfigOptions:FromAddress": "",
    "EmailConfigOptions:Subject": "",
    "EmailConfigOptions:MailingServiceAccount": "",
    "EmailConfigOptions:EmailTemplateDefaultValue": "",
   
  }
}

My EmailConfiOptions class:

public class EmailConfigOptions
    {
        public string FromAddress { get; set; }  
        public string Subject { get; set; }
        public string MailingServiceAccount { get; set; }
        public string EmailTemplateDefaultValue { get; set; }
    }

ServiceBusConfigOptions class:

 public class ServiceBusConfigOptions
    {
        public string ConnectionString { get; set; }
        public string EventTypeTopic { get; set; }
        public string EventTypeTopicSubscription { get; set; }
        public string SendEmailTopic { get; set; }
        public string NotificationMonitoringTopic { get; set; }
    }

In the Startup.cs class I can register with both methods AddOptions and Configure(as shown in the below).

So basically which method should we use? AddOptions or Configure.

public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddLogging();
       
            builder.Services.AddOptions<ServiceBusConfigOptions>()
                .Bind(builder.GetContext().Configuration.GetSection(nameof(ServiceBusConfigOptions)));

            builder.Services.Configure<EmailConfigOptions>(builder.GetContext().Configuration.GetSection(nameof(EmailConfigOptions)));

            builder.Services.AddSingleton<IValidateOptions<EmailConfigOptions>, EmailConfiOptionsValidator>();
            builder.Services.AddSingleton<IValidateOptions<ServiceBusConfigOptions>, ServiceBusConfigOptionsValidator>();

            
        }
    }
Rakesh Kumar
  • 2,701
  • 9
  • 38
  • 66
  • Internally, `Configure` calls `AddOptions`. [source code](https://source.dot.net/#Microsoft.Extensions.Options.ConfigurationExtensions/OptionsConfigurationServiceCollectionExtensions.cs,62) – Nkosi Jul 11 '22 at 17:19
  • @Nkosi Then, should we use Configure? Or it doesn't matter – Rakesh Kumar Jul 11 '22 at 17:20
  • 1
    Doesn't matter. Both shown code work the same. – Nkosi Jul 11 '22 at 17:22
  • 3
    This could help if you time to read: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-6.0 – Thomas Jul 11 '22 at 20:13

1 Answers1

1

The ConfigurationBuilder Class and the extension methods used to configure various files in your code.

In a ConfigurationBuilder we use Configure property to inject the settings of our custom or predefined services.

which method should we use? AddOptions or Configure.

We can use both Configure or AddOptions to inject the IOptions. But the proper way which follows to avoid conflicts.

If we are trying to inject the Custom IOptions in our Application, we need to use the Services.AddOptions(). to retrieve the exact IOptions value we need to use the Services.Configure< IOptions>()

public override void Configure(IFunctionsHostBuilder builder)
{
    # Inject Options
    builder.services.AddOptions();
    # it inject the Connection String value of ServiceBusConfigOptions 
    builder.services.Configure<ServiceBusConfigOptions>(Configuration.GetSection("ConnectionString"));
    ...
}

Reference

  • Usage of IOptions in Configure Link 1 & 2
  • Usage of IOptions in AddOptions Link 1 & 2
Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15