Program.cs
์๋ฏธ์ ์ญํ
์ดํ๋ฆฌ์ผ์ด์ ์์ ์ฝ๋๊ฐ ์จ์๋ ํ์ผ
C/C++์์์ main ํจ์ ํ์ผ ์ญํ ์ ํจ
์ฑ์์ ์๊ตฌํ๋ ์๋น์ค๋ฅผ ์ค์ ํจ
์ฑ์ ์์ฒญ์ ํธ๋ค๋งํ ์ ์๊ฒ ๋ฏธ๋ค์จ์ด๋ค์ ๊ตฌ์ฑํ๊ณ ํ์ดํ๋ผ์ด๋ ํจ
๊ตฌ์ฑ
ํ๋ก๊ทธ๋จ ์์์ต์ ๋ฐ DI ์๋น์ค๋ฅผ ์ง์ ํ๋ ๊ฒ์ Startup.cs ํ์ผ์์ ํ์ผ๋, ํ์ฌ๋ program.cs ํ์ผ๋ก ํตํฉ๋จ
main ํด๋์ค๋ฅผ ๋ง๋ค์ด ์คํ ๋ฉ์๋๋ฅผ ๋ง๋ค์ด์ ์คํํ์ง๋ง, ์ต์์ ๋ฌธ(Top-level statements)์ผ๋ก๋ ์ฌ์ฉํ ์ ์์. VS 2022 ๊ธฐ์ค์ผ๋ก ํ๋ก์ ์ค ์์ฑ์ ์ต์์ ๋ฌธ ์ธํ ์ด ๋ํดํธ๋ก ์ค์ ๋ผ์์ (C# 9 ๋ถํฐ)
์ฝ๋ ์คํ ํ๋ฆ
๊ธฐ๋ณธ์ ์ธ ํ๋ฆ์ ๋ค์๊ณผ ๊ฐ์
using namespace
builder์ ์ธ์คํด์ค๋ฅผ ๋ง๋ฆ (class WebApplicationBuilder)
์ข ์์ฑ ์ฃผ์ (Dependency Injection) : ๋น๋ ์ธ์คํด์ค์ ์๋น์ค๋ฅผ ๋ฑ๋กํจ (์ปจํธ๋กค๋ฌ ๋ฑ)
๋น๋๋ก ์ฑ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ฆ (class WebApplication)
๋ฏธ๋ค์จ์ด ์ค์ : ์ฑ์์ ์ฌ์ฉํ๊ณ ์ ํ๋ ๊ฒ๋ค์ ์ง์ ํจ
์ฑ์ ์คํํจ
program.cs ์ฝ๋
// 1. using namespace
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
// 2. builder์ ์ธ์คํด์ค๋ฅผ ๋ง๋ฆ : ๋ฏธ๋ฆฌ ๊ตฌ์ฑ๋ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ์ฌ WebApplication์ ์ธ์คํด์ค builder๋ฅผ ์์ฑํจ
var builder = WebApplication.CreateBuilder(args);
// 3. ์ข
์์ฑ ์ฃผ์
(Dependency Injection)
// Add services to the container.
builder.Services.AddControllers();
// ํ์์ ์๋น์ค๋ฅผ ๋น๋ ์ปจํ
์ด๋์ ๋ ์ถ๊ฐํ ์ ์์
// 4. ๋น๋๋ก ์ฑ์ ๊ฐ์ฒด๋ฅผ ๋ง๋ฆ
var app = builder.Build();
// 5. ๋ฏธ๋ค์จ์ด ์ค์
// Configure the HTTP request pipeline.
// Devleop ๋ฒ์ ์ด ์๋ ๋ Exception page๋ฅผ ์ฌ์ฉํจ
if (!app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// Routing์ ์ฌ์ฉํจ
app.UseRouting();
// Endpoint ์ง์
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
// 6. ์ฑ์ ์คํํจ
// ์ค์ ์ด ๋ค ๋๋ฌ์ผ๋ฉด ์ฑ์ ์คํํจ
app.Run();
์ฐธ๊ณ ๋ฌธํ
Last updated