Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beachside-project/2469585db45c7f9196b7c7d351632597 to your computer and use it in GitHub Desktop.
Save beachside-project/2469585db45c7f9196b7c7d351632597 to your computer and use it in GitHub Desktop.
OptionsPatternSamples - Validate 2
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