Created
March 29, 2021 09:06
-
-
Save beachside-project/2469585db45c7f9196b7c7d351632597 to your computer and use it in GitHub Desktop.
OptionsPatternSamples - Validate 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using OptionsPatternSamples.Options; | |
using System; | |
namespace OptionsPatternSamples | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddOptions<AzureAdOptions>() | |
// options + IConfiguration のパターン | |
.Configure<IConfiguration>((options, configuration) => configuration.GetSection(nameof(AzureAdOptions)).Bind(options)) | |
.Validate(options => | |
{ | |
// 地道に validation: | |
if (string.IsNullOrEmpty(options.TenantId)) throw new ArgumentNullException(nameof(options.TenantId)); | |
if (string.IsNullOrEmpty(options.ClientId)) throw new ArgumentNullException(nameof(options.ClientId)); | |
if (string.IsNullOrEmpty(options.ClientSecret)) throw new ArgumentNullException(nameof(options.ClientSecret)); | |
return true; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment