■ 대용량 데이터를 스트리밍 방식으로 전달받는 방법을 보여준다. (PostgreSQL 연동)
[TestClient 프로젝트]
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using Newtonsoft.Json; namespace TestClient { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> static void Main() { string url = "https://localhost:44352/api/data/inquirylist"; DataSearchConditionModel searchCondition = new DataSearchConditionModel(); searchCondition.MENU_ID = "A007A6D5-35E6-46A2-A3BD-1B84843BB76C"; searchCondition.MEMBER_GRADE = "99"; searchCondition.LIMIT_AGE = "99"; searchCondition.WRITE_DATE1 = new DateTime(2021, 7 , 1 ); searchCondition.WRITE_DATE2 = new DateTime(2021, 7 , 25); searchCondition.CREATE_TIME1 = new DateTime(1 , 1 , 1 ); searchCondition.CREATE_TIME2 = new DateTime(9999, 12, 31); int i = 0; var result = GetPOSTData1<DataModel>(url, searchCondition); foreach(DataModel photo in result) { Console.WriteLine("{0} {1} {2}", i++, photo.ID, photo.SUBJECT); } } #endregion #region POST 스트림 구하기 - POSTGetStream(serverURL, contentType, encoding, argument, timeOut) /// <summary> /// POST 스트림 구하기 /// </summary> /// <param name="serverURL">서버 URL</param> /// <param name="contentType">컨텐츠 타입</param> /// <param name="encoding">인코딩</param> /// <param name="argument">인자 문자열</param> /// <param name="timeOut">타임 아웃</param> /// <returns>POST 스트림</returns> private static Stream POSTGetStream(string serverURL, string contentType, Encoding encoding, string argument, int? timeOut = null) { byte[] argumentArray = encoding.GetBytes(argument); HttpWebRequest request = WebRequest.Create(serverURL) as HttpWebRequest; request.ProtocolVersion = HttpVersion.Version11; request.AllowAutoRedirect = true; request.AllowWriteStreamBuffering = true; request.Method = WebRequestMethods.Http.Post; request.ContentType = contentType; request.ContentLength = argumentArray.Length; if(timeOut.HasValue) { request.Timeout = timeOut.Value; } using(Stream requestStream = request.GetRequestStream()) { requestStream.Write(argumentArray, 0, argumentArray.Length); } HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream responseStream = response.GetResponseStream(); return responseStream; } #endregion #region POST 데이터 구하기 1 - GetPOSTData1<TResultItem>(url, source) /// <summary> /// POST 데이터 구하기 1 /// </summary> /// <typeparam name="TResultItem">결과 항목 타입</typeparam> /// <param name="url">URL</param> /// <param name="source">소스 객체</param> /// <returns>결과 항목 타입 열거 가능형</returns> private static IEnumerable<TResultItem> GetPOSTData1<TResultItem>(string url, object source) { string sourceJSON = JsonConvert.SerializeObject(source); HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, url); requestMessage.Content = new StringContent(sourceJSON, Encoding.UTF8, "application/json"); requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpClient client = new HttpClient(); HttpResponseMessage responseMessage = client.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead).Result; responseMessage.EnsureSuccessStatusCode(); using(Stream stream = responseMessage.Content.ReadAsStreamAsync().Result) { using(StreamReader streamReader = new StreamReader(stream)) { JsonSerializer serializer = new JsonSerializer(); using(JsonTextReader jsonTextReader = new JsonTextReader(streamReader)) { while(jsonTextReader.Read()) { if(jsonTextReader.TokenType != JsonToken.StartArray && jsonTextReader.TokenType != JsonToken.EndArray) { yield return serializer.Deserialize<TResultItem>(jsonTextReader); } } } } } } #endregion #region POST 데이터 구하기 2 - GetPOSTData2<TResultItem>(url, source) /// <summary> /// POST 데이터 구하기 2 /// </summary> /// <typeparam name="TResultItem">결과 항목 타입</typeparam> /// <param name="url">URL</param> /// <param name="source">소스 객체</param> /// <returns>결과 항목 타입 열거 가능형</returns> private static IEnumerable<TResultItem> GetPOSTData2<TResultItem>(string url, object source) { string sourceJSON = JsonConvert.SerializeObject(source); using(Stream stream = POSTGetStream(url, "application/json; charset=utf-8", Encoding.UTF8, sourceJSON)) { using(StreamReader streamReader = new StreamReader(stream)) { using(JsonTextReader jsonTextReader = new JsonTextReader(streamReader)) { JsonSerializer serializer = new JsonSerializer(); while(jsonTextReader.Read()) { if(jsonTextReader.TokenType != JsonToken.StartArray && jsonTextReader.TokenType != JsonToken.EndArray) { yield return serializer.Deserialize<TResultItem>(jsonTextReader); } } } } } } #endregion } } |
[TestServer 프로젝트]
▶ launchSettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
{ "$schema" : "http://json.schemastore.org/launchsettings.json", "iisSettings" : { "windowsAuthentication" : false, "anonymousAuthentication" : true, "iisExpress" : { "applicationUrl" : "http://localhost:35043", "sslPort" : 44352 } }, "profiles" : { "IIS Express" : { "commandName" : "IISExpress", "launchBrowser" : true, "launchUrl" : "swagger", "environmentVariables" : { "ASPNETCORE_ENVIRONMENT" : "Development" } }, "TestServer" : { "commandName" : "Project", "dotnetRunMessages" : "true", "launchBrowser" : true, "launchUrl" : "swagger", "applicationUrl" : "https://localhost:5001;http://localhost:5000", "environmentVariables" : { "ASPNETCORE_ENVIRONMENT" : "Development" } } } } |
▶ appsettings.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "Logging" : { "LogLevel" : { "Default" : "Information", "Microsoft" : "Warning", "Microsoft.Hosting.Lifetime" : "Information" } }, "AllowedHosts" : "*", "ConnectionStrings" : { "DefaultConnection" : "Server=localhost;Port=5432;Database=arca;User Id=admin;Password=1234;" } } |
▶ DataModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
using System; namespace TestServer.Models { /// <summary> /// 자료 모델 /// </summary> public class DataModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID (NOT NULL) /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 메뉴 ID - MENU_ID (NOT NULL) /// <summary> /// 메뉴 ID /// </summary> public string MENU_ID { get; set; } #endregion #region 제목 - SUBJECT (NOT NULL) /// <summary> /// 제목 /// </summary> public string SUBJECT { get; set; } #endregion #region 작성일 - WRITE_DATE (NOT NULL) /// <summary> /// 작성일 /// </summary> public DateTime WRITE_DATE { get; set; } #endregion #region 순번 - SEQUENCE (NOT NULL) /// <summary> /// 순번 /// </summary> public string SEQUENCE { get; set; } #endregion #region 회원 등급 - MEMBER_GRADE (NOT NULL) /// <summary> /// 회원 등급 /// </summary> public string MEMBER_GRADE { get; set; } #endregion #region 연령 제한 - LIMIT_AGE (NOT NULL) /// <summary> /// 연령 제한 /// </summary> public string LIMIT_AGE { get; set; } #endregion #region 자료 타입 - DATA_TYPE (NOT NULL) /// <summary> /// 자료 타입 /// </summary> public string DATA_TYPE { get; set; } #endregion #region 분류 1 - CATEGORY1 /// <summary> /// 분류 1 /// </summary> public string CATEGORY1 { get; set; } #endregion #region 분류 2 - CATEGORY2 /// <summary> /// 분류 2 /// </summary> public string CATEGORY2 { get; set; } #endregion #region 분류 3 - CATEGORY3 /// <summary> /// 분류 3 /// </summary> public string CATEGORY3 { get; set; } #endregion #region 분류 4 - CATEGORY4 /// <summary> /// 분류 4 /// </summary> public string CATEGORY4 { get; set; } #endregion #region 분류 5 - CATEGORY5 /// <summary> /// 분류 5 /// </summary> public string CATEGORY5 { get; set; } #endregion #region 분류 6 - CATEGORY6 /// <summary> /// 분류 6 /// </summary> public string CATEGORY6 { get; set; } #endregion #region 분류 7 - CATEGORY7 /// <summary> /// 분류 7 /// </summary> public string CATEGORY7 { get; set; } #endregion #region 분류 8 - CATEGORY8 /// <summary> /// 분류 8 /// </summary> public string CATEGORY8 { get; set; } #endregion #region 저장소 ID - STORAGE_ID (NOT NULL) /// <summary> /// 저장소 ID /// </summary> public string STORAGE_ID { get; set; } #endregion #region 저장소명 - STORAGE_NAME (JOIN FIELD) /// <summary> /// 저장소명 /// </summary> public string STORAGE_NAME { get; set; } #endregion #region 생성자 ID - CREATE_ID (NOT NULL) /// <summary> /// 생성자 ID /// </summary> public string CREATE_ID { get; set; } #endregion #region 생성자 사용자 ID - CREATE_USER_ID (JOIN FIELD) /// <summary> /// 생성자 사용자 ID /// </summary> public string CREATE_USER_ID { get; set; } #endregion #region 생성 시간 - CREATE_TIME (NOT NULL) /// <summary> /// 생성 시간 /// </summary> public DateTime CREATE_TIME { get; set; } #endregion #region 수정자 ID - UPDATE_ID (NOT NULL) /// <summary> /// 수정자 ID /// </summary> public string UPDATE_ID { get; set; } #endregion #region 수정자 사용자 ID - UPDATE_USER_ID (JOIN FIELD) /// <summary> /// 수정자 사용자 ID /// </summary> public string UPDATE_USER_ID { get; set; } #endregion #region 수정 시간 - UPDATE_TIME (NOT NULL) /// <summary> /// 수정 시간 /// </summary> public DateTime UPDATE_TIME { get; set; } #endregion } } |
▶ DataSearchConditionModel.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
using System; namespace TestServer.Models { /// <summary> /// 자료 조회 조건 모델 /// </summary> public class DataSearchConditionModel { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메뉴 ID - MENU_ID /// <summary> /// 메뉴 ID /// </summary> public string MENU_ID { get; set; } #endregion #region ID - ID /// <summary> /// ID /// </summary> public string ID { get; set; } #endregion #region 제목 - SUBJECT /// <summary> /// 제목 /// </summary> public string SUBJECT { get; set; } #endregion #region 순번 - SEQUENCE /// <summary> /// 순번 /// </summary> public string SEQUENCE { get; set; } #endregion #region 회원 등급 - MEMBER_GRADE /// <summary> /// 회원 등급 /// </summary> public string MEMBER_GRADE { get; set; } #endregion #region 연령 제한 - LIMIT_AGE /// <summary> /// 연령 제한 /// </summary> public string LIMIT_AGE { get; set; } #endregion #region 작성일 1 - WRITE_DATE1 /// <summary> /// 작성일 1 /// </summary> public DateTime WRITE_DATE1 { get; set; } #endregion #region 작성일 2 - WRITE_DATE2 /// <summary> /// 작성일 2 /// </summary> public DateTime WRITE_DATE2 { get; set; } #endregion #region 자료 타입 - DATA_TYPE /// <summary> /// 자료 타입 /// </summary> public string DATA_TYPE { get; set; } #endregion #region 분류 1 - CATEGORY1 /// <summary> /// 분류 1 /// </summary> public string CATEGORY1 { get; set; } #endregion #region 분류 2 - CATEGORY2 /// <summary> /// 분류 2 /// </summary> public string CATEGORY2 { get; set; } #endregion #region 분류 3 - CATEGORY3 /// <summary> /// 분류 3 /// </summary> public string CATEGORY3 { get; set; } #endregion #region 분류 4 - CATEGORY4 /// <summary> /// 분류 4 /// </summary> public string CATEGORY4 { get; set; } #endregion #region 분류 5 - CATEGORY5 /// <summary> /// 분류 5 /// </summary> public string CATEGORY5 { get; set; } #endregion #region 분류 6 - CATEGORY6 /// <summary> /// 분류 6 /// </summary> public string CATEGORY6 { get; set; } #endregion #region 분류 7 - CATEGORY7 /// <summary> /// 분류 7 /// </summary> public string CATEGORY7 { get; set; } #endregion #region 분류 8 - CATEGORY8 /// <summary> /// 분류 8 /// </summary> public string CATEGORY8 { get; set; } #endregion #region 저장소명 - STORAGE_NAME /// <summary> /// 저장소명 /// </summary> public string STORAGE_NAME { get; set; } #endregion #region 생성 시간 1 - CREATE_TIME1 /// <summary> /// 생성 시간 1 /// </summary> public DateTime CREATE_TIME1 { get; set; } #endregion #region 생성 시간 2 - CREATE_TIME2 /// <summary> /// 생성 시간 2 /// </summary> public DateTime CREATE_TIME2 { get; set; } #endregion } } |
▶ DataController.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System.Collections.Generic; using System.Data; using Npgsql; using Dapper; using TestServer.Models; namespace TestServer.Controllers { [ApiController] [Route("api/[controller]")] public class DataController : ControllerBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region INQUIRY_LIST_SQL_POSTGRESQL /// <summary> /// INQUIRY_LIST_SQL (POSTGRESQL용) /// </summary> private static string INQUIRY_LIST_SQL_POSTGRESQL = @" SELECT A.ID AS ID ,A.MENU_ID AS MENU_ID ,A.SUBJECT AS SUBJECT ,A.WRITE_DATE AS WRITE_DATE ,A.SEQUENCE AS SEQUENCE ,A.MEMBER_GRADE AS MEMBER_GRADE ,A.LIMIT_AGE AS LIMIT_AGE ,A.DATA_TYPE AS DATA_TYPE ,A.CATEGORY1 AS CATEGORY1 ,A.CATEGORY2 AS CATEGORY2 ,A.CATEGORY3 AS CATEGORY3 ,A.CATEGORY4 AS CATEGORY4 ,A.CATEGORY5 AS CATEGORY5 ,A.CATEGORY6 AS CATEGORY6 ,A.CATEGORY7 AS CATEGORY7 ,A.CATEGORY8 AS CATEGORY8 ,A.STORAGE_ID AS STORAGE_ID ,B.NAME AS STORAGE_NAME ,A.CREATE_ID AS CREATE_ID ,C.USER_ID AS CREATE_USER_ID ,A.CREATE_TIME AS CREATE_TIME ,A.UPDATE_ID AS UPDATE_ID ,D.USER_ID AS UPDATE_USER_ID ,A.UPDATE_TIME AS UPDATE_TIME FROM LIBRARY_DATA A LEFT JOIN COM_STORAGE B ON B.ID = A.STORAGE_ID LEFT JOIN COM_USER C ON C.ID = A.CREATE_ID LEFT JOIN COM_USER D ON D.ID = A.UPDATE_ID WHERE A.MENU_ID = @MENU_ID AND (@ID = '' OR @ID IS NULL OR A.ID ILIKE @ID || '%') AND (@SUBJECT = '' OR @SUBJECT IS NULL OR A.SUBJECT ILIKE @SUBJECT || '%') AND (@SEQUENCE = '' OR @SEQUENCE IS NULL OR A.SEQUENCE ILIKE @SEQUENCE || '%') AND A.MEMBER_GRADE <= @MEMBER_GRADE AND A.LIMIT_AGE <= @LIMIT_AGE AND A.WRITE_DATE BETWEEN @WRITE_DATE1 AND @WRITE_DATE2 AND (@DATA_TYPE = '' OR @DATA_TYPE IS NULL OR A.DATA_TYPE = @DATA_TYPE ) AND (@CATEGORY1 = '' OR @CATEGORY1 IS NULL OR A.CATEGORY1 ILIKE @CATEGORY1 || '%') AND (@CATEGORY2 = '' OR @CATEGORY2 IS NULL OR A.CATEGORY2 ILIKE @CATEGORY2 || '%') AND (@CATEGORY3 = '' OR @CATEGORY3 IS NULL OR A.CATEGORY3 ILIKE @CATEGORY3 || '%') AND (@CATEGORY4 = '' OR @CATEGORY4 IS NULL OR A.CATEGORY4 ILIKE @CATEGORY4 || '%') AND (@CATEGORY5 = '' OR @CATEGORY5 IS NULL OR A.CATEGORY5 ILIKE @CATEGORY5 || '%') AND (@CATEGORY6 = '' OR @CATEGORY6 IS NULL OR A.CATEGORY6 ILIKE @CATEGORY6 || '%') AND (@CATEGORY7 = '' OR @CATEGORY7 IS NULL OR A.CATEGORY7 ILIKE @CATEGORY7 || '%') AND (@CATEGORY8 = '' OR @CATEGORY8 IS NULL OR A.CATEGORY8 ILIKE @CATEGORY8 || '%') AND (@STORAGE_NAME = '' OR @STORAGE_NAME IS NULL OR B.NAME ILIKE @STORAGE_NAME || '%') AND A.CREATE_TIME BETWEEN @CREATE_TIME1 AND @CREATE_TIME2 ORDER BY A.SEQUENCE DESC ,A.WRITE_DATE DESC ,A.CREATE_TIME DESC ,A.SUBJECT ASC "; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private readonly IConfiguration configuration; /// <summary> /// 로거 /// </summary> private readonly ILogger<DataController> logger; /// <summary> /// 연결 문자열 /// </summary> private readonly string connectionString; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - DataController(configuration, logger) /// <summary> /// 생성자 /// </summary> /// <param name="configuration">구성</param> /// <param name="logger">로거</param> public DataController(IConfiguration configuration, ILogger<DataController> logger) { this.configuration = configuration; this.logger = logger; this.connectionString = configuration.GetConnectionString("DefaultConnection"); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 리스트 조회하기 - InquiryList(searchCondition, transaction) /// <summary> /// 리스트 조회하기 /// </summary> /// <param name="searchCondition">조회 조건</param> /// <param name="transaction">트랜잭션</param> /// <returns>조회 결과</returns> [HttpPost] [Route("InquiryList")] public IEnumerable<DataModel> InquiryList([FromBody] DataSearchConditionModel searchCondition) { using(IDbConnection connection = CreateConnection()) { using(IDataReader reader = connection.ExecuteReader(INQUIRY_LIST_SQL_POSTGRESQL, searchCondition)) { while(reader.Read()) { var rowParser = reader.GetRowParser<DataModel>(typeof(DataModel)); yield return rowParser(reader); } } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 연결 생성하기 - CreateConnection() /// <summary> /// 연결 생성하기 /// </summary> /// <returns>연결</returns> private IDbConnection CreateConnection() { return new NpgsqlConnection(this.connectionString); } #endregion } } |
▶ Startup.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; namespace TestServer { /// <summary> /// 시작 /// </summary> public class Startup { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 구성 - Configuration /// <summary> /// 구성 /// </summary> public IConfiguration Configuration { get; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Startup(configuration) /// <summary> /// 생성자 /// </summary> /// <param name="configuration">구성</param> public Startup(IConfiguration configuration) { Configuration = configuration; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서비스 구성하기 - ConfigureServices(IServiceCollection serviceCollection) /// <summary> /// 서비스 구성하기 /// </summary> /// <param name="serviceCollection">서비스 컬렉션</param> public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddControllers(); serviceCollection.AddSwaggerGen ( options => { options.SwaggerDoc("v1", new OpenApiInfo { Title = "TestServer", Version = "v1" }); } ); } #endregion #region 구성하기 - Configure(builder, environment) /// <summary> /// 구성하기 /// </summary> /// <param name="builder">빌더</param> /// <param name="environment">환경</param> public void Configure(IApplicationBuilder builder, IWebHostEnvironment environment) { if(environment.IsDevelopment()) { builder.UseDeveloperExceptionPage(); builder.UseSwagger(); builder.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestServer v1")); } builder.UseHttpsRedirection(); builder.UseRouting(); builder.UseAuthorization(); builder.UseEndpoints ( endpoints => { endpoints.MapControllers(); } ); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; namespace TestServer { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 호스트 빌더 생성하기 - CreateHostBuilder(argumentList) /// <summary> /// 호스트 빌더 생성하기 /// </summary> /// <param name="argumentList">인자 리스트</param> /// <returns>호스트 빌더</returns> public static IHostBuilder CreateHostBuilder(string[] argumentList) => Host.CreateDefaultBuilder(argumentList) .ConfigureWebHostDefaults ( webHostBuilder => { webHostBuilder.UseStartup<Startup>(); } ); #endregion #region 프로그램 시작하기 - Main(argumentList) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentList">인자 리스트</param> public static void Main(string[] argumentList) { CreateHostBuilder(argumentList).Build().Run(); } #endregion } } |