[OSCA] AzureFunctions OpenAPI with .NET 맛보기

귀귀재재·2023년 7월 20일
0

과제 최종 목표


  • Azure Function App - dotnet(isolated process)로 만들어보기
  • 닷넷 버전은 7.0으로 설정하기(완료)
  • 새로운 HttpTrigger를 만들어서 아래 영상의 결과물과 동일하게 만들어보기
  • 만든 결과를 자신의 Github 계정에 새 레포를 하나 만들어서 업로드 후 "2023-07-18 제출용" 스레드로 제출(링크)
    .NET ubuntu 20.04 설치

Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc을 실행시켜보자!


cd ./samples/Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc
func start

Swagger 확인


hostip:7071/api/swagger/ui 로 접속해서 API 스웨거 확인해 볼 수 있습니다.

good


local.settings.json 파일을 수정하고 다시 빌드해보기


before

after

dotnet build && func start

패키지 빌드를 이렇게 하면 번거롭다.
Node 의 npm 있고,
Java 의 gradle 이있다면,
.NET 에게는 nuget이 있다.


빈 애저펑션 프로젝트를 만들어보자!

새로운 폴더를 만들고 initializing을 해보았다.

그리고 우리는 애저펑션의 아주 기초 프로젝트를 만들어본 것이다.

dotnet restore && dotnet build

근데 에러가 발생했습니다.

메타데이터 생성에 문제가 있어 해당 오류나는 파일로 접근을 해보니... 안나온다.

하지만, 트러블 슈팅 짬바 7년차

이전에 잘돌아가는 project 메타데이터를 확인해보니

sdk 4.1.3으로 표시되어있으나
내 메타데이터는 4.1.1을 가리키고 있었다.

아무래도 애저펑션의 오류로 인해서 Init을 했을때 작성되는 파일이 dynamic 하지 않고 static 하게 적용된것 같다.

그래서 버전수정을 해주고 나니 잘 돌아갔다.


HttpTrigger 추가해보기



OcaTrigger라는 이름으로 추가해보았습니다.

다음과같이 파일이 생성이 되네요.
그리고 원하는 api 로 라우트를 "greetings"로 해주니 엔드 포인트가 생성된걸 확인 할 수 있었습니다.

쿼리파라미터가 잘 먹은걸 볼 수 있습니다. ㅎㅎ

OpenAPI 익스텐션 프로젝트에 설치하기


메타데이터에 다음과 같은 문구 추가하기

	<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.OpenApi" Version="1.5.1" />

그리고 다시 닷넷 리스토어와 빌드를 해주면 됩니다.


Swagger에 Operation 추가해주기


문구 추가

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;



[OpenApiOperation(operationId: "Run", tags: new[] { "name" }, Summary = "The name of the person to use in the greeting.", Description = "This HTTP triggered function returns a person's name.", Visibility = OpenApiVisibilityType.Important)]

Operation에 파라미터 추가해주기


using Microsoft.OpenApi.Models;

[OpenApiParameter(name: "name", In = ParameterLocation.Query, Required = true, Type = typeof(string), Summary = "The name of the person to use in the greeting.", Description = "The name of the person to use in the greeting.", Visibility = OpenApiVisibilityType.Important)]

Operation에 Response 응답 형태 정보 넣어주기



[OpenApiResponseWithBody(statusCode: System.Net.HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(string), Summary = "The response", Description = "This returns the response")]
[OpenApiResponseWithBody(statusCode: System.Net.HttpStatusCode.InternalServerError, contentType: "text/plain", bodyType: typeof(string), Summary = "The response", Description = "This returns the response")]

수고많았습니다.

profile
오늘 뭐하지///?

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

좋은 글 잘 읽었습니다, 감사합니다.

답글 달기