Asp.net core options pattern

์•ˆ๋…•ํ•˜์„ธ์š”.

ํ˜„์žฌ asp.net core๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ azure app service๋กœ ํ˜ธ์ŠคํŒ…ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

Startup.ConfigureServices ํ•จ์ˆ˜์—์„œ ์˜ต์…˜ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ appsetting ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์ ธ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
services.Configure(Configuration.GetSection(โ€œFooOptionsโ€));

๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Azure App Service ๊ตฌ์„ฑ ๋ฉ”๋‰ด์—์„œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ์„ฑ์„ FooOptions ๊ฐ’์„ ์žฌ์ •์˜ ํ•ด์„œ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  Controller์—์„œ FooOptions ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ ์žฌ์ •์˜๋œ ๊ฐ’์ด ์•„๋‹Œ appsettings.json์˜ ๊ฐ’์„ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.

Startup์—์„œ Configuration.GetSection(โ€œFooOptionsโ€).Value๋Š” appsettings.json ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ 
Controller์—์„œ ํ˜ธ์ถœํ•  ๋•Œ๋Š” Azure์—์„œ ์žฌ์ •์˜ํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ ์›ƒ๊ธด๊ฑด ConnectionStrings๋„ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ConnectionString์€ ์ •์ƒ์ ์œผ๋กœ ์žฌ์ •์˜๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์–ด๋–ค๊ฒŒ ์ž˜๋ชป๋์„๊นŒ์š”?

์ข‹์•„์š” 1

ํ˜น์‹œ appSettings๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€์ง€๊ณ  ๊ณ„์‹ ๊ฐ€์š”?
์ด๋ง์€ development, staging ๊ณผ ๊ฐ™์ด ํ™˜๊ฒฝ๋ณ„๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์ž…๋‹ˆ๋‹ค.

( ์ด๊ฒƒ์€ appsetting ํŒŒ์ผ์„ ์†”๋ฃจ์…˜ ํƒ์ƒ‰๊ธฐ์—์„œ ํŽผ์น˜๋ฉด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. )

Asp.net์€ ๋Ÿฐ์น˜ ์…‹ํŒ…์œผ๋กœ ๊ตฌ์„ฑ๋œ environment์— ํ•ด๋‹นํ•˜๋Š” appsetting๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ƒ์† ๊ฐœ๋…์ด ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
๊ทธ ๊ด€๊ณ„๋ฅผ ์‚ดํŽด๋ณด์‹œ๋Š”๊ฒŒ ์ข‹์„ ๊ฑธ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ข‹์•„์š” 1

apsettings.json๊ณผ apsettings.development.json ๋‘ ํ™˜๊ฒฝํŒŒ์ผ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ œ ์งˆ๋ฌธ์€ appsettings.json๊ณผ azure์— ๊ตฌ์„ฑ๋ฉ”๋‰ด์— ์ •์˜ํ•œ ํ‚ค๊ฐ’๋ชฉ๋ก์„ ๊ฐ™์€๋ฐ,

startup ์‹œ,
connection string๋งŒ azure์—์„œ ์žฌ์ •์˜ํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐ˜๋ฉด(Configuration.GetConnectionString)
Configuration.GetSection ์‚ฌ์šฉ ์‹œ, appsettings.json ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜ต๋‹ˆ๋‹ค.

์ดํ›„
์ปจํŠธ๋กค๋Ÿฌ์—์„œ ํ˜ธ์ถœ ์‹œ, Configuration.GetSection ์—์„œ๋Š” azure์—์„œ ์žฌ์ •์˜ํ•œ ๊ฐ’์„ ๊ฐ€์ง€๊ณ ์˜ต๋‹ˆ๋‹ค.

์ข‹์•„์š” 1