Last active
March 29, 2021 09:05
-
-
Save beachside-project/7caf04773013ded3b9da83510f0af20b to your computer and use it in GitHub Desktop.
OptionsPatternSamples - Validate 1
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.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>() | |
.Configure(options => Configuration.GetSection(nameof(AzureAdOptions)).Bind(options)) | |
// このメソッドが用意されてるので簡単に validate できる | |
.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