.NET Core WebAPI(C#)를 IIS에 배포(VisualStudioCode)

15349 단어 WebAPIIIS.NETCoreC#

환경


  • Windows10 Pro
  • Visual Studio Code (VSCodeUserSetup-x64-1.40.2.exe) ... 고기능 텍스트 편집기
  • .NET Core (dotnet-sdk-3.1.100-win-x64.exe)・・・API 만들기 위한 것
  • .NET Core 호스팅 번들 (dotnet-hosting-3.1.1-win.exe)...
  • ※v3.1.0은 환경에 따라서는 500 Internal Server Errort가 발생한다.


  • 전제



  • Visual Studio Code 설치됨

  • .NET Core 3.1 SDK 다운로드됨

  • ASP.NET Core 3.1 Runtime (v3.1.1) - Windows Hosting Bundle 다운로드 받음
  • ※v3.1.0은 환경에 따라서는 500 Internal Server Errort가 발생한다.


  • 흐름


  • .NETCore 설치
  • WebAPI 생성
  • Visual Studio Code에서 디버깅 실행
  • WebAPI 앱 게시 (배포물 생성)
  • IIS 활성화
  • .NET Core 호스팅 번들 설치
  • IIS에 배포

  • .NETCore 설치



    dotnet-sdk-3.1.100-win-x64.exe를 실행합니다.




    WebAPI 작성



    웹 프로젝트 만들기



    이하의 커멘드 실행으로 프로젝트의 세트 파일이 만들어져 샘플 API의 「WeatherForecastController.cs」도 포함된다.
    PS D:\git\cshaptest> dotnet new webapi -o HelloApi
    The template "ASP.NET Core Web API" was created successfully.
    
    Processing post-creation actions...
    Running 'dotnet restore' on HelloApi\HelloApi.csproj...
      D:\git\cshaptest\HelloApi\HelloApi.csproj の復元が 135.98 ms で完了しました。
    
    Restore succeeded.
    
    HelloApi
    │  appsettings.Development.json
    │  appsettings.json
    │  HelloApi.csproj
    │  Program.cs
    │  Startup.cs
    │  WeatherForecast.cs
    │
    ├─Controllers
    │      WeatherForecastController.cs
    │
    ├─obj
    │      HelloApi.csproj.nuget.cache
    │      HelloApi.csproj.nuget.dgspec.json
    │      HelloApi.csproj.nuget.g.props
    │      HelloApi.csproj.nuget.g.targets
    │      project.assets.json
    │
    └─Properties
            launchSettings.json
    

    빌드



    빌드하면 bin 폴더가 생겨 빌드 결과의 exe 등이 생성된다.
    cd HelloApi
    PS D:\git\cshaptest\HelloApi> dotnet build
    .NET Core 向け Microsoft (R) Build Engine バージョン 16.4.0+e901037fe
    Copyright (C) Microsoft Corporation.All rights reserved.
    
      D:\git\cshaptest\HelloApi\HelloApi.csproj の復元が 26.62 ms で完了しました。
      HelloApi -> D:\git\cshaptest\HelloApi\bin\Debug\netcoreapp3.1\HelloApi.dll
    
    ビルドに成功しました。
        0 個の警告
        0 エラー
    
    経過時間 00:00:07.66
    
    HelloApi
    │  appsettings.Development.json
    │  appsettings.json
    │  HelloApi.csproj
    │  Program.cs
    │  Startup.cs
    │  WeatherForecast.cs
    │
    ├─bin
    │  └─Debug
    │      └─netcoreapp3.1
    │          │  appsettings.Development.json
    │          │  appsettings.json
    │          │  HelloApi.deps.json
    │          │  HelloApi.dll
    │          │  HelloApi.exe
    │          │  HelloApi.pdb
    │          │  HelloApi.runtimeconfig.dev.json
    │          │  HelloApi.runtimeconfig.json
    │          │
    │          └─Properties
    │                  launchSettings.json
    │
    ├─Controllers
    │      WeatherForecastController.cs
    │
    ├─obj
    │  │  HelloApi.csproj.nuget.cache
    │  │  HelloApi.csproj.nuget.dgspec.json
    │  │  HelloApi.csproj.nuget.g.props
    │  │  HelloApi.csproj.nuget.g.targets
    │  │  project.assets.json
    │  │
    │  └─Debug
    │      └─netcoreapp3.1
    │          │  HelloApi.AssemblyInfo.cs
    │          │  HelloApi.AssemblyInfoInputs.cache
    │          │  HelloApi.assets.cache
    │          │  HelloApi.csproj.FileListAbsolute.txt
    │          │  HelloApi.csprojAssemblyReference.cache
    │          │  HelloApi.dll
    │          │  HelloApi.exe
    │          │  HelloApi.MvcApplicationPartsAssemblyInfo.cache
    │          │  HelloApi.pdb
    │          │  HelloApi.RazorTargetAssemblyInfo.cache
    │          │
    │          └─staticwebassets
    │                  HelloApi.StaticWebAssets.Manifest.cache
    │                  HelloApi.StaticWebAssets.xml
    │
    └─Properties
            launchSettings.json
    

    실행


    PS D:\git\cshaptest\HelloApi> dotnet run
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: https://localhost:5001
    info: Microsoft.Hosting.Lifetime[0]
          Now listening on: http://localhost:5000
    info: Microsoft.Hosting.Lifetime[0]
          Application started. Press Ctrl+C to shut down.
    info: Microsoft.Hosting.Lifetime[0]
          Hosting environment: Development
    info: Microsoft.Hosting.Lifetime[0]
          Content root path: D:\git\cshaptest\HelloApi
    

    https://localhost:5001/WeatherForecast


    정지



    Ctrl+C
    info: Microsoft.Hosting.Lifetime[0]
          Application is shutting down...
    

    Visual Studio Code에서 디버깅 실행



    단순한 텍스트 에디터 + 커멘드라도 좋지만, Visual Studio Code를 사용하는 것으로 디버그 실행이 할 수 있어 브레이크 포인트라든지 둘 수 있어 편리.

    프로젝트 열기



    아까 커맨드로 만든 프로젝트의 폴더를 지정해 연다.




    새로운 API 만들기



    Controllers 폴더를 마우스 오른쪽 버튼으로 클릭하여 "HelloController.cs"를 만듭니다.




    작성한 「HelloController.cs」코드는 이쪽.
    using System;
    using Microsoft.AspNetCore.Mvc;
    
    namespace HelloWorld.Controllers
    {
        [ApiController]
        [Route("api/{controller}")]
        public class HelloController : ControllerBase
        {
            // GET: Hello
            [HttpGet(Name = "hello")]
            public String Hello()
            {
                return "Hello!";
            }
        }
    }
    

    디버그 실행



    "Start Debugging"을 실행.

    Environment는 「.NET Core」를 선택.

    Visual Studio Code는 터미널에서 표준으로 나와 살아난다.




    WebAPI 앱 게시(배포물 생성)


    PS D:\git\cshaptest\HelloApi> dotnet publish --configuration Release
    .NET Core 向け Microsoft (R) Build Engine バージョン 16.4.0+e901037fe
    Copyright (C) Microsoft Corporation.All rights reserved.
    
      D:\git\cshaptest\HelloApi\HelloApi.csproj の復元が 32.83 ms で完了しました。
      HelloApi -> D:\git\cshaptest\HelloApi\bin\Release\netcoreapp3.1\HelloApi.dll
      HelloApi -> D:\git\cshaptest\HelloApi\bin\Release\netcoreapp3.1\publish\
    
    HelloApi
    │  appsettings.Development.json
    │  appsettings.json
    │  HelloApi.csproj
    │  Program.cs
    │  Startup.cs
    │  WeatherForecast.cs
    │
    ├─.vscode
    │      launch.json
    │      tasks.json
    │
    ├─bin
    │  ├─Debug
    │  │  └─netcoreapp3.1
    │  │      │  appsettings.Development.json
    │  │      │  appsettings.json
    │  │      │  HelloApi.deps.json
    │  │      │  HelloApi.dll
    │  │      │  HelloApi.exe
    │  │      │  HelloApi.pdb
    │  │      │  HelloApi.runtimeconfig.dev.json
    │  │      │  HelloApi.runtimeconfig.json
    │  │      │
    │  │      └─Properties
    │  │              launchSettings.json
    │  │
    │  └─Release
    │      └─netcoreapp3.1
    │          │  appsettings.Development.json
    │          │  appsettings.json
    │          │  HelloApi.deps.json
    │          │  HelloApi.dll
    │          │  HelloApi.exe
    │          │  HelloApi.pdb
    │          │  HelloApi.runtimeconfig.dev.json
    │          │  HelloApi.runtimeconfig.json
    │          │
    │          ├─Properties
    │          │      launchSettings.json
    │          │
    │          └─publish
    │                  appsettings.Development.json
    │                  appsettings.json
    │                  HelloApi.deps.json
    │                  HelloApi.dll
    │                  HelloApi.exe
    │                  HelloApi.pdb
    │                  HelloApi.runtimeconfig.json
    │                  web.config
    │
    ├─Controllers
    │      HelloController.cs
    │      WeatherForecastController.cs
    │
    ├─obj
    │  │  HelloApi.csproj.nuget.cache
    │  │  HelloApi.csproj.nuget.dgspec.json
    │  │  HelloApi.csproj.nuget.g.props
    │  │  HelloApi.csproj.nuget.g.targets
    │  │  project.assets.json
    │  │
    │  ├─Debug
    │  │  └─netcoreapp3.1
    │  │      │  HelloApi.AssemblyInfo.cs
    │  │      │  HelloApi.AssemblyInfoInputs.cache
    │  │      │  HelloApi.assets.cache
    │  │      │  HelloApi.csproj.FileListAbsolute.txt
    │  │      │  HelloApi.csprojAssemblyReference.cache
    │  │      │  HelloApi.dll
    │  │      │  HelloApi.exe
    │  │      │  HelloApi.MvcApplicationPartsAssemblyInfo.cache
    │  │      │  HelloApi.pdb
    │  │      │  HelloApi.RazorTargetAssemblyInfo.cache
    │  │      │  project.razor.json
    │  │      │
    │  │      └─staticwebassets
    │  │              HelloApi.StaticWebAssets.Manifest.cache
    │  │              HelloApi.StaticWebAssets.xml
    │  │
    │  └─Release
    │      └─netcoreapp3.1
    │          │  HelloApi.AssemblyInfo.cs
    │          │  HelloApi.AssemblyInfoInputs.cache
    │          │  HelloApi.assets.cache
    │          │  HelloApi.csproj.FileListAbsolute.txt
    │          │  HelloApi.csprojAssemblyReference.cache
    │          │  HelloApi.dll
    │          │  HelloApi.exe
    │          │  HelloApi.MvcApplicationPartsAssemblyInfo.cache
    │          │  HelloApi.pdb
    │          │  HelloApi.RazorTargetAssemblyInfo.cache
    │          │
    │          └─staticwebassets
    │                  HelloApi.StaticWebAssets.Manifest.cache
    │                  HelloApi.StaticWebAssets.xml
    │
    └─Properties
            launchSettings.json
    

    IIS 활성화



    Windows 기능을 엽니다.



    인터넷 정보 서비스를 선택합니다.



    .NET Core 호스팅 번들 설치



    dotnet-hosting-3.1.1-win.exe를 실행합니다.




    ※dotnet-hosting-3.1.1-win.exe 인스톨 후에는 IIS 자체를 재기동하는 것

    IIS에 배포



    IIS 시작.


    웹사이트 추가



    '사이트'를 마우스 오른쪽 버튼으로 클릭하고 '웹 사이트 추가'



    실제 경로는 dotnet publish 때 publish 폴더를 지정합니다.




    주의


  • IIS에 배포했지만 액세스할 수 없는 경우 IIS 자체를 다시 시작하는 것을 잊지 않았는지 확인합니다.

  • 참고



    htps : // / cs. 미 c 로소 ft. 이 m / 자 jp / 아 sp 네 t / 이것 / 쓰리 아 ls / 후 rst - 으 b 아피?ゔ ぃ w = 아 sp 네 t 이것 3.1 & bs = ゔ ぃ あ ぢ
    htps : // / cs. mic로소 ft. 이 m / 자 jp / 아 sp 네 t / 이것 / 쓰리 아 ls / 뿌 b ぃ sh - 좋은 s?ゔ ぃ w = 아 sp.
    htps : // 이 m / 등 wr / ms / 85, bf5f646, 20, 3438, a1
    htps : // 이 m/피에세오 f를 하고 r/있어 ms/d091744193로 86682712
    htps : // 이 m/피에세오 f를 하고 r/있어 ms/d091744193로 86682712

    좋은 웹페이지 즐겨찾기