Blazor

Blazor

Tags
C#
Frontend
Mobile
Published
Author

Blazor

๋‹จ์ผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ชจ๋ธ์—์„œ ์„œ๋ฒ„์ชฝ ๋ Œ๋”๋ง ๋ฐ ํด๋ผ์ด์–ธํŠธ ๋Œ€ํ™”ํ˜• ์ž‘์—…์„ ๋ชจ๋‘ ์ง€์›ํ•˜๋Š” .NET ์›น ํ”„๋ž˜์ž„์›Œํฌ
  • Razor ํด๋ž˜์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋˜๋Š” NuGet ํŒจํ‚ค์ง€๋กœ ๊ณต์œ  ๋ฐ ๋ฐฐํฌ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํด๋ž˜์Šค๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ .razor ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ Razor ํƒœ๊ทธ ํŽ˜์ด์ง€ ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ
  • Razor๋Š” HTML, CSS, C# ์ฝ”๋“œ๋ฅผ ๊ฒฐํ•ฉํ•œ ๊ตฌ๋ฌธ
<PageTitle>Counter</PageTitle> <h1>Counter</h1> <p role="status">Current count: @currentCount</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> @code { private int currentCount = 0; private void IncrementCount() { currentCount++; } }

Blazor Web Apps

๋‹จ์ผ ์†”๋ฃจ์…˜์—์„œ ์„œ๋ฒ„์ชฝ ๋ Œ๋”๋ง ๋ฐ ์ „์ฒด ํด๋ผ์ด์–ธํŠธ์ชฝ ๋Œ€ํ™”ํ˜• ์ž‘์—…์„ ํฌํ•จํ•˜๋Š” ๊ตฌ์„ฑ ์š”์†Œ ๊ธฐ๋ฐ˜ ์•„ํ‚คํ…์ฒ˜๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ์„œ๋ฒ„์ชฝ ํด๋ผ์ด์–ธํŠธ์ชฝ ๋ Œ๋”๋ง ๋ชจ๋“œ ๊ฐ„์— ์ „ํ™˜ํ•˜๊ณ  ๋™์ผํ•œ ํŽ˜์ด์ง€์—์„œ ํ˜ผํ•ฉํ•  ์ˆ˜๋„ ์žˆ์Œ
  • ์„œ๋ฒ„์—์„œ UI ๋ Œ๋”๋ง์ด ๋น ๋ฅด๊ฒŒ ์ˆ˜ํ–‰๋˜๋ฏ€๋กœ ํŽ˜์ด์ง€๊ฐ€ ๋น ๋ฅด๊ฒŒ ๋กœ๋“œ๋จ
  • ๋ธŒ๋ผ์šฐ์ €์™€์˜ ์‹ค์‹œ๊ฐ„ ์—ฐ๊ฒฐ์„ ํ†ตํ•ด ์„œ๋ฒ„์—์„œ UI ์ƒํ˜ธ ์ž‘์šฉ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋Œ€ํ™”ํ˜• SSR์„ ์ง€์›
  • ๋Œ€ํ™”ํ˜• SSR(Interactive SSR)์„ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ผ์ด์–ธํŠธ ์•ฑ์—์„œ ๊ธฐ๋Œ€ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ’๋ถ€ํ•œ ์‚ฌ์šฉ์ž ํ™˜๊ฒฝ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์„œ๋ฒ„ ๋ฆฌ์†Œ์Šค์— ์•ก์„ธ์Šคํ•˜๊ธฐ ์œ„ํ•ด API ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋Œ€ํ™”ํ˜• ํŽ˜์ด์ง€์˜ ํŽ˜์ด์ง€ ์ฝ˜ํ…์ธ ๋Š” ๋ฏธ๋ฆฌ ๋ Œ๋”๋ง๋จ

Blazor Hybrid

์›น, ๋ชจ๋ฐ”์ผ ๋ฐ ๋ฐ์Šคํฌํ†ฑ ํ”Œ๋žซํผ์— ๋Œ€ํ•œ ๋„ค์ดํ‹ฐ๋ธŒ ๋ฐ ์›น ๊ธฐ์ˆ ์ด ํ˜ผํ•ฉ๋œ ๋„ค์ดํ‹ฐ๋ธŒ ํด๋ผ์ด์–ธํŠธ ์•ฑ
  • .NET ํ”„๋กœ์„ธ์Šค์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹คํ–‰
  • ๋กœ์ปฌ interop ์ฑ„๋„์„ ์‚ฌ์šฉํ•˜์—ฌ ํฌํ•จ๋œ Web View ์ปจํŠธ๋กค์— ์›น UI๋ฅผ ๋ Œ๋”๋ง
  • C# ๋ฐ XAML์„ ์‚ฌ์šฉํ•˜์—ฌ ๋„ค์ดํ‹ฐ๋ธŒ ๋ชจ๋ฐ”์ผ ๋ฐ ๋ฐ์Šคํฌํ†ฑ ์•ฑ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ํ”Œ๋žซํผ ๊ฐ„ ํ”„๋ ˆ์ž„์›Œํฌ์ธ .NET MAUI๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋นŒ๋“œ๋จ

Blazor Project Contents

notion image

Program.cs

์„œ๋ฒ„๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ์•ฑ ์„œ๋น„์Šค์™€ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์•ฑ์˜ ์ง„์ž…์ 

Components

  • App.razor : ์•ฑ์˜ ๋ฃจํŠธ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ
  • Routes.razor : Blzor ๋ผ์šฐํ„ฐ๋ฅผ ๊ตฌ์„ฑ
  • Pages : ๋””๋ ‰ํ„ฐ๋ฆฌ์—๋Š” ์•ฑ์— ๋Œ€ํ•œ ๋ช‡ ๊ฐ€์ง€ ์˜ˆ์ œ ์›น ํŽ˜์ด์ง€๊ฐ€ ํฌํ•จ

Propertiesย ๋””๋ ‰ํ„ฐ๋ฆฌ ๋‚ด์˜ย launchSettings.jsonย ํŒŒ์ผ

๋กœ์ปฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ํ”„๋กœํ•„ ์„ค์ •์„ ์ •์˜
ํฌํŠธ ๋ฒˆํ˜ธ๋Š” ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ ์ž๋™์œผ๋กœ ํ• ๋‹น๋˜์–ด ์ด ํŒŒ์ผ์— ์ €์žฅ

๋Œ€ํ™”ํ˜• ์ž‘์—… ์‚ฌ์šฉ

๊ตฌ์„ฑ ์š”์†Œ์—์„œ UI ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋Œ€ํ™”ํ˜•(SSR)์ด์–ด์•ผํ•จ
  • ์„œ๋ฒ„์—์„œ ์ •์ ์œผ๋กœ ๋ Œ๋”๋ง
  • ์ง€์‹œ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€ํ™”ํ˜• ๋ Œ๋”๋ง ๋ชจ๋“œ๋ฅผ ์ ์šฉํ•˜์—ฌ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋Œ€ํ™”ํ˜•์œผ๋กœ ๋งŒ๋“ฌ @rendermode
@rendermode InteractiveServer <Counter @rendermode="InteractiveServer" />
๋Œ€ํ™”ํ˜• ์„œ๋ฒ„ ๋ Œ๋”๋ง์€ ๋ธŒ๋ผ์šฐ์ €์™€์˜ WebSocket ์—ฐ๊ฒฐ์„ ํ†ตํ•ด ์„œ๋ฒ„์˜ UI ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌ
notion image
  1. Blazor๋Š” ์•ฑ์˜ ๊ตฌ์„ฑ ์š”์†Œ์—์„œ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์—ฐ๊ฒฐ์„ ํ†ตํ•ด ์„œ๋ฒ„์— UI ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋ƒ„
  1. Blazor๋Š” ๋ Œ๋”๋ง๋œ ์—…๋ฐ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ € DOM ์—…๋ฐ์ดํŠธ๋ฅผ ์ฒ˜๋ฆฌ
๋˜๋Š”
  1. ๋ Œ๋”๋ง ๋ชจ๋“œ๋ฅผ InteractiveWebAssembly์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋Œ€ํ™”ํ˜•์œผ๋กœ ๋ Œ๋”๋ง
  1. ๊ตฌ์„ฑ ์š”์†Œ ์ฝ”๋“œ๊ฐ€ ๋‹ค์šด๋กœ๋“œ๋˜๊ณ  WebAssembly ๊ธฐ๋ฐ˜ .NET ๋Ÿฐํƒ€์ž„์„ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์ชฝ ์‹คํ–‰

ํ˜ธ์ŠคํŒ… ๋ชจ๋ธ

Blazor Server

ASP.NET Core ์•ฑ ๋‚ด์˜ ์›น ์„œ๋ฒ„์—์„œ ์‹คํ–‰
ํด๋ผ์ด์–ธํŠธ ์ชฝ์˜ UI์—…๋ฐ์ดํŠธ, ์ด๋ฒคํŠธ ๋ฐ JavaScript ํ˜ธ์ถœ์€ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ SignalR์—ฐ๊ฒฐ์„ ํ†ตํ•ด ์ „์†ก (์‹ค์‹œ๊ฐ„์— ์œ ์šฉ)
SignalR ๊ธฐ๋Šฅ
  1. ์—ฐ๊ฒฐ ๊ด€๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์ฒ˜๋ฆฌ
  1. ๋ชจ๋“  ์—ฐ๊ฒฐ๋œ ํด๋ผ์ด์–ธํŠธ์— ๋™์‹œ์— ๋ฉ”์‹œ์ง€ ์ „์†ก
  1. ํŠน์ • ํด๋ผ์ด์–ธํŠธ๋‚˜ ํด๋ผ์ด์–ธํŠธ ๊ทธ๋ฃน์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ „์†ก
  1. ๋Š˜์–ด๋‚œ ํŠธ๋ž˜ํ”ฝ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํฌ๊ธฐ๋ฅผ ์กฐ์ •

Blazor WebAssembly

Blazor ์•ฑ, ํ•ด๋‹น ์ข…์†์„ฑ ๋ฐ .NET ๋Ÿฐํƒ€์ž„์ด ๋‹ค์šด๋กœ๋“œ ๋˜์–ด ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰

.NET MAUI ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ ๋ฐ ์‹œ์ž‘์ด ํฌํ•จ๋œ Blazor ํ•˜์ด๋ธŒ๋ฆฌ๋“œ

Blazor ํ”„๋กœ์ ํŠธ ํŒŒ์ผ

  • Pages : Blazor ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ์„ธ ํŽ˜์ด์ง€๋ฅผ ์ •์˜
  • Shared : ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ ๋ฐ ํƒ์ƒ‰ ๋ฉ”๋‰ด๋ฅผ ๋น„๋กฏํ•œ ๊ณต์œ  Razor ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ํฌํ•จ
  • wwwroot : HTML,CSS,JavaScript ๋ฐ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ํฌํ•จํ•˜์—ฌ Blazor์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ •์  ์›น ์ž์‚ฐ์ด ํฌํ•จ
  • Main.razor : ์›น ๋ณด๊ธฐ ๋‚ด์—์„œ ํŽ˜์ด์ง€ ํƒ์ƒ‰์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก Blazor ๋ผ์šฐํ„ฐ๋ฅผ ์„ค์ •ํ•˜๋Š” ์•ฑ์˜ ๋ฃจํŠธ Razor ๊ตฌ์„ฑ ์š”์†Œ
  • _Imports.razor : ๊ฐ Razor ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์ •์˜

.NET MAUI ํ”„๋กœ์ ํŠธ ํŒŒ์ผ

  • App.xaml
    • ์•ฑ์ด XAML ๋ ˆ์ด์•„์›ƒ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฆฌ์†Œ์Šค๋ฅผ ์ •์˜
    • ๊ธฐ๋ณธ ๋ฆฌ์†Œ์Šค๋Š” Resources ํด๋”์— ์žˆ์œผ๋ฉฐ ์•ฑ ์ „์ฒด ์ƒ‰๊ณผ .NET MAUI์˜ ๋ชจ๋“  ๊ธฐ๋ณธ ์ œ๊ณต ์ปจํŠธ๋กค์˜ ๊ธฐ๋ณธ ์Šคํƒ€์ผ์„ ์ •์˜
  • App.xaml.cs
    • App.xaml ํŒŒ์ผ์˜ ์ฝ”๋“œ ์ˆจ๊น€
    • ์•ฑ ํด๋ž˜์Šค๋ฅผ ์ •์˜
    • ๋Ÿฐํƒ€์ž„์‹œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‚˜ํƒ€๋ƒ„
    • ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋Š” ์ดˆ๊ธฐ ์ฐฝ์„ ๋งŒ๋“ค๊ณ  MainPage ์†์„ฑ์— ํ• ๋‹น
      • ์ด ์†์„ฑ์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋  ๋•Œ ํ‘œ์‹œ๋˜๋Š” ํŽ˜์ด์ง€๋ฅผ ๊ฒฐ์ •
      • ์ผ๋ฐ˜์ ์ธ ํ”Œ๋žซํผ ์ค‘๋ฆฝ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ˆ˜๋ช… ์ฃผ๊ธฐ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ธฐ๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์Œ (OnStart, OnResume, Onsleep)
  • MainPage.xaml
    • ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜๊ฐ€ ํฌํ•จ
    • .NET MAUI Blazor ์•ฑ ํ…œํ”Œ๋ฆฟ์—์„œ ์ƒ์„ฑํ•˜๋Š” ์ƒ˜ํ”Œ ์•ฑ์€ CSS ์„ ํƒ๊ธฐ(#app)์—์„œ ์ง€์ •ํ•œ ์œ„์น˜์— ์ง€์ •๋œ ํ˜ธ์ŠคํŠธ HTML ํŽ˜์ด์ง€(wwwroot/index.html)์˜ Main ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋กœ๋“œํ•˜๋Š” BlazorWebView๋กœ ๊ตฌ์„ฑ
    • <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:BlazorHybridApp" x:Class="BlazorHybridApp.MainPage" BackgroundColor="{DynamicResource PageBackgroundColor}"> <BlazorWebView HostPage="wwwroot/index.html"> <BlazorWebView.RootComponents> <RootComponent Selector="#app" ComponentType="{x:Type local:Main}" /> </BlazorWebView.RootComponents> </BlazorWebView> </ContentPage>
  • MainPage.xaml.cs
    • ํŽ˜์ด์ง€์˜ ์ฝ”๋“œ ์ˆจ๊น€
    • ํŽ˜์ด์ง€ ํŠธ๋ฆฌ๊ฑฐ์—์„œ .NET MAUI๊ฐ€ ์ œ์–ดํ•˜๋Š” ๋‹ค์–‘ํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ๊ธฐ ๋ฐ ๊ธฐํƒ€ ์ž‘์—…์— ๋Œ€ํ•œ ๋…ผ๋ฆฌ๋ฅผ ์ •์˜
    • namespace BlazorHybridApp; public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); } }
  • MauiProgram.cs
    • ๋„ค์ดํ‹ฐ๋ธŒ ํ”Œ๋žซํผ๋งˆ๋‹ค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ณ  ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋‹ค๋ฅธ ์‹œ์ž‘์ 
    • ํ”„๋กœ์ ํŠธ์˜ Platforms ํด๋”์— ์žˆ์Œ
    • ์ •์  MauiProgram ํด๋ž˜์Šค์˜ CreateMauiApp ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ
    • CreateMauiApp ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ ์ž‘์„ฑ๊ธฐ ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑ
    • ์•ฑ ์ž‘์„ฑ๊ธฐ ๊ฐœ์ฒด์˜ UseMauiApp ์ œ๋„ค๋ฆญ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ˜•์‹ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํด๋ž˜์Šค๋ฅผ ์ง€์ •
      • ๊ธ€๊ผด ๋“ฑ๋ก, ์ข…์†์„ฑ ์ฃผ์ž…์— ์‚ฌ์šฉ๋˜๋Š” ์„œ๋น„์Šค ๊ตฌ์„ฑ, ์ปจํŠธ๋กค์˜ ์‚ฌ์šฉ์ž ์ง€์ • ์ฒ˜๋ฆฌ๊ธฐ ๋“ฑ๋ก ๋“ฑ๊ณผ ๊ฐ™์€ ์ž‘์—…์— ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต
      • ์•ฑ ์ž‘์„ฑ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ธ€๊ผด์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚ ์”จ ์„œ๋น„์Šค๋ฅผ ๋“ฑ๋กํ•˜๊ณ , AddMauiBlazorWebView ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Blazor ํ•˜์ด๋ธŒ๋ฆฌ๋“œ์— ๋Œ€ํ•œ ์ง€์›์„ ์ถ”๊ฐ€
      using Microsoft.AspNetCore.Components.WebView.Maui; using BlazorHybridApp.Data; namespace BlazorHybridApp; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); builder.Services.AddMauiBlazorWebView(); #if DEBUG builder.Services.AddBlazorWebViewDeveloperTools(); builder.Logging.AddDebug(); #endif builder.Services.AddSingleton<WeatherForecastService>(); return builder.Build(); } }

HttpClient ํด๋ž˜์Šค

  • ์•ฑ์ด REST ์›น ์„œ๋น„์Šค์— HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๊ณ  HTTP ์‘๋‹ต์„ ์ˆ˜์‹ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” .NET ํด๋ž˜์Šค
  • URI ์ง‘ํ•ฉ์€ ์›น ์„œ๋น„์Šค๊ฐ€ ๋…ธ์ถœํ•˜๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ์‹๋ณ„
  • URI๋Š” ์›น ์„œ๋น„์Šค์˜ ์ฃผ์†Œ๋ฅผ ํ•ด๋‹น ์ฃผ์†Œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์†Œ์Šค์˜ ์ด๋ฆ„๊ณผ ๊ฒฐํ•ฉ
using System.Net.Http; ... var client = new HttpClient();

HttpClient๋กœ ์ƒˆ ๋ฆฌ์†Œ์Šค ๋งŒ๋“ค๊ธฐ

HttpClient client = new HttpClient(); // ์›น ์„œ๋น„์Šค๋กœ ์ „์†ก๋˜๋Š” ์š”์ฒญ์„ ๋ชจ๋ธ๋ง, HTTP ๋™์‚ฌ, ์›น ์„œ๋น„์Šค์˜ URL์„ ์ง€์ • HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, url); // ์†์„ฑ์„ ํ†ตํ•ด ๋ณด๋‚ผ ํŽ˜์ด๋กœ๋“œ๋ฅผ ์ฑ„์›€ message.Content = JsonContent.Create<Part>(part); HttpResponseMessage response = await client.SendAsync(message);
  • ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ์‚ฌ์šฉํ•˜๋Š” client๋ผ๋Š” HttpClient์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ฌ
  • ๋ฉ”์‹œ์ง€๋ฅผ ๋ชจ๋ธ๋งํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” message๋ผ๋Š” HttpRequestMessage์˜ ์ธ์Šคํ„ฐ์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. message์—๋Š” HTTP ๋™์‚ฌ์™€ URL ์ด ์žˆ์Œ
  • JsonContent.Create ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HttpRequestMessage์˜ Content ์†์„ฑ์„ ์„ค์ •, ์ด ํ•จ์ˆ˜๋Š” part๋ณ€์ˆ˜๋ฅผ ์›น ์„œ๋น„์Šค๋กœ ๋ณด๋‚ด๊ธฐ์— ์ ํ•ฉํ•œ JSON์œผ๋กœ ์ž๋™์œผ๋กœ ์ง๋ ฌํ™”
  • HttpClient ๊ฐœ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒ…๋‹ˆ๋‹ค. HttpResponseMessage๋Š” ์ƒํƒœ ์ฝ”๋“œ ๋ฐ ์›น ์„œ๋น„์Šค์—์„œ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ •๋ณด์™€ ๊ฐ™์€ ์ •๋ณด๊ฐ€ ํฌํ•จ

HttpClient๋กœ ๋ฆฌ์†Œ์Šค ์ฝ๊ธฐ

HttpClient client = new HttpClient(); // GetStringAsync ๋ฉ”์„œ์Šค๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š” URI๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‘๋‹ต์„ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ string text = await client.GetStringAsync("https://..."); //ใ…”๋”๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ํ˜•์‹์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์›น ์„œ๋น„์Šค์— ์•Œ๋ ค์คŒ client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  • ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜๋˜๊ณ  ์‘๋‹ต ๋ฉ”์‹œ์ง€ ๋ณธ๋ฌธ๋งŒ ํฌํ•จ
  • ํ—ค๋”, ๋ณธ๋ฌธ ๋ฐ ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•œ ์ „์ฒด ์‘๋‹ต์„ ๊ฐ€์ ธ์˜ค๋ ค๋ฉด GetAsync ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ
  • ๋ฐ์ดํ„ฐ๊ฐ€ HttpResponseMessage ๊ฐœ์ฒด๋กœ ๋ฐ˜ํ™˜๋จ

HttpClient๋กœ ๋ฆฌ์†Œ์Šค ์—…๋ฐ์ดํŠธ

HttpClient client = new HttpClient(); HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Put, url); message.Content = JsonContent.Create<Part>(part); HttpResponseMessage response = await client.SendAsync(message);
๐Ÿ’ก
POST ์™€ PUT ์‚ฌ์ด์˜ ๊ทผ๋ณธ์ ์ธ ์ฐจ์ด๋Š” ๋ฉฑ๋“ฑ์„ฑ ๋™์ผํ•œ PUT ์š”์ฒญ์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉด ๋™์ผํ•œ ๋ฆฌ์†Œ์Šค๊ฐ€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ๋กœ ์—…๋ฐ์ดํŠธ๋˜๊ณ  ์š”์ฒญ์ด ํ•œ ๋ฒˆ๋งŒ ์ „์†ก๋œ ๊ฒฝ์šฐ์™€ ํšจ๊ณผ๊ฐ€ ๋™์ผ ๋™์ผํ•œ POST ์š”์ฒญ์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•˜๋ฉด REST ์„œ๋น„์Šค๊ฐ€ ๋ฆฌ์†Œ์Šค์˜ ์—ฌ๋Ÿฌ ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค๊ฒŒ ๋จ

HttpClient๋กœ ๋ฆฌ์†Œ์Šค ์‚ญ์ œ

HttpClient client = new HttpClient(); HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Delete, url); HttpResponseMessage response = await client.SendAsync(message);