Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 379. Azure Web App 확장 예제 제작 [링크 복사], [링크+제목 복사]
조회: 12503
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 4개 있습니다.)

Azure Web App 확장 예제 제작

사실 지난번에 소개한 글만 봐도,

Azure Web App 확장 예제 - Simple WebSite Extension
; https://www.sysnet.pe.kr/2/0/11505

확장 프로그램을 만들 기본이 마련됩니다. 그래도 다시 정리하는 의미에서 바닥부터 만들어 보겠습니다.

우선 웹 사이트 하나를 만듭니다. (여기서는 ASP.NET WebForms 프로젝트로 시작합니다.)

유형: ASP.NET Web Application - Empty - Web Forms
이름: MyAppExtension

그다음 default.aspx를 추가하고 다음과 같이 환경 변수를 출력하는 코드만 넣어두겠습니다. (따라서, 이 확장이 설치되면 App Service가 운영 중인 가상 머신의 환경 변수를 모두 확인할 수 있습니다. 참고로, AppService의 환경 변수는 Kudu에서 기본 출력해 줍니다.)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyAppExtension.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    Ver 1.0<br />
    <form id="form1" runat="server">
        <div>
            <table>
            <%
    foreach (System.Collections.DictionaryEntry entry in Environment.GetEnvironmentVariables())
    {
            %>
        <tr>
                <td><%=entry.Key.ToString()%></td>
                <td><%=entry.Value.ToString()%></td>
        </tr>
            <%
    }
             %>
                </table>
        </div>
    </form>
</body>
</html>

그다음, 웹 사이트 프로젝트의 루트에 applicationHost.xdt 파일을 다음의 내용으로 생성합니다.

<?xml version="1.0" encoding="utf-8" ?> 
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.applicationHost>
    <sites>
      <site name="%XDT_SCMSITENAME%" xdt:Locator="Match(name)">
          <application path="/MyAppExtension" xdt:Locator="Match(path)" xdt:Transform="Remove" />
          <application path="/MyAppExtension" applicationPool="%XDT_APPPOOLNAME%" xdt:Transform="Insert">
          <virtualDirectory path="/" physicalPath="%XDT_EXTENSIONPATH%" />
        </application>
      </site>
    </sites>
  </system.applicationHost>
</configuration>

(위의 path 부분은 여러분의 상황에 맞는 이름으로 바꿔도 됩니다.)

이제 비주얼 스튜디오의 솔루션 탐색기에서 프로젝트 노드를 우 클릭해 "Publish" 메뉴로 "Folder"에 배포합니다. 이렇게 만들어진 확장을 배포하기 위해 폴더의 내용을 하나의 .zip 파일로 압축하고 이 파일을 (Windows 10 1803 빌드부터 기본 포함되어 있기도 한) curl.exe를 이용해 Azure Portal의 "Deployment Trigger Url(배포 트리거 URL)"로 배포하면 됩니다.

여기서 한 가지, 고려 사항이 있는데요. zip 파일은 반드시 내부에 "/SiteExtensions/...[extension_name]..."의 디렉터리 구조를 포함해야 합니다. 즉, 해당 zip 파일을 압축을 풀었을 때 다음과 같은 구조로 나와야 합니다.

/SiteExtensions
            /MyAppExtension
                    /app.config
                    /...[이하 생략]...

따라서 Publish 메뉴에서 Folder 설정을 "bin\Release\Publish"에서 "bin\Release\Publish\SiteExtensions\MyAppExtension"으로 바꿔야 하고 zip 파일 생성은 ""bin\Release\Publish" 폴더를 기준으로 압축해야 합니다. 참고로, 다음의 글에 따라 FolderProfile.pubxml 파일을 변경하면 비주얼 스튜디오에서 배포(Publish) 시 압축 파일까지 함께 생성할 수 있습니다.

MSBuild를 이용해 프로젝트 배포 후 결과물을 zip 파일로 압축하는 방법
; https://www.sysnet.pe.kr/2/0/11508

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        ...[생략]...
    </PropertyGroup>

    <PropertyGroup>
        <MyZipFilePath>$(publishUrl)\..\..\..\$(TargetName).zip</MyZipFilePath>
        <MyPublishFolderPath>$(publishUrl)\..\..</MyPublishFolderPath>
    </PropertyGroup>

    <Target Name="MyPublish" AfterTargets="GatherAllFilesToPublish">
        <Delete Files="$(MyZipFilePath)" />
        <Exec Command="powershell.exe -nologo -noprofile -command &quot;&amp; { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('$(MyPublishFolderPath)', '$(MyZipFilePath)'); }&quot;" />
    </Target>
</Project>

생성된 (이 글에서는 MyWebAppExtension.zip) zip 파일은 (Windows 10 1803 빌드부터 기본 포함되어 있기도 한) curl.exe를 이용해 Azure Portal의 "Deployment Trigger Url(배포 트리거 URL)"로 배포할 수 있습니다.

curl.exe -k -v -T "E:\...\MyAppExtension.zip" "https://....scm.azurewebsites.net/zip"

정상적으로 배포가 되었으면 다음의 URL을 통해 웹앱 확장이 잘 동작하는지 확인할 수 있습니다.

https://[app_service_name].scm.azurewebsites.net/MyAppExtension/

(일부 변경은 Azure Portal이나 Kudu Site Extensions 화면을 통해 재시작해줘야 반영됩니다.)




다음은 AppService 웹 사이트가 Azure VM 내 위치하는 폴더의 경로입니다.

D:\home\site\wwwroot\

그리고 그 AppService의 확장들은 다음의 폴더에 배포됩니다.

D:\home\SiteExtensions\...[your_extension]...

확장들이 포함한 applicationHost.xdt 파일은 대충 다음과 같은 식의 경로에 있는 applicationHost.config 파일과 병합됩니다.

C:\DWASFiles\Sites\...app_service...\Temp\applicationhost.config

위의 applicationHost.config 파일에 각각의 Extensions들에 의해 어떤 변경이 이뤄졌는지에 대한 로그를 다음의 경로에서 확인할 수 있습니다.

D:\home\LogFiles
                \*_SCM.log (예: RD0004FFE3003C_2018-05-28_06-53-21_SCM.log)

참고로, 위와 같은 동작을 하는데 꼭 확장을 만들 필요는 없습니다. 아래의 글에 보는 바와 같이,

Making changes to the applicationHost.config on Azure App Service
; https://www.thebestcsharpprogrammerintheworld.com/2015/01/19/making-changes-to-the-applicationhost-config-on-azure-app-service/

d:/home/site 폴더에만 있으면 xdt 파일이 적용됩니다.




개발 팁을 하나 소개하면, App Service 확장은 로컬에서 개발하고 Azure로 올릴 텐데요, 이때 환경에 따른 코드 수행을 제어할 필요가 있습니다. 이에 대해 다음의 글에도 나오지만,

Writing a Site Extension for Azure Websites
; https://azure.microsoft.com/en-us/blog/writing-a-site-extension-for-azure-websites/

그럴 때 "home" 환경 변수 여부를 이용하면 됩니다.

if (Environment.GetEnvironmentVariable("home") != null)
{
    // in Azure
}
else
{
    // Allows us to run locally.
}

물론, 여러분의 로컬 PC에 'home' 환경 변수를 정의해 두었다면 다음의 변수들이 후보가 될 수 있습니다.

WEBSITE_HOME_STAMPNAME
WEBSITE_PROACTIVE_AUTOHEAL_ENABLED
ScmType
APPSETTING_WEBSITE_AUTH_ENABLED
WEBSITE_SCM_ALWAYS_ON_ENABLED
REGION_NAME
WEBSITE_SCM_SEPARATE_STATUS
WEBSITE_SITE_NAME
WEBSITE_AUTH_AUTO_AAD
WEBSITE_CURRENT_STAMPNAME
APPSETTING_ScmType
WEBSITE_COMPUTE_MODE
WEBSITE_HOSTNAME
WEBSITE_IIS_SITE_NAME
HOME_EXPANDED
WEBSITE_RESOURCE_GROUP
WEBSITE_AUTH_LOGOUT_PATH
WEBSITE_LOCALCACHE_ENABLED
WEBSITE_INSTANCE_ID
WEBSITE_SKU
WEBSITE_OWNER_NAME
WEBSITE_RELAYS




curl.exe로 배포 시 다음과 같은 오류가 발생한다면?

{"Message":"An error has occurred.","ExceptionMessage":"Access to the path 'D:\\home\\app.config' is denied.","ExceptionType":"System.UnauthorizedAccessException","StackTrace":" at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\r\n at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)\r\n at System.IO.FileInfo.Open(FileMode mode, FileAccess access)\r\n at System.IO.Abstractions.FileInfoWrapper.Open(FileMode mode, FileAccess access)\r\n at Kudu.Core.Infrastructure.ZipArchiveExtensions.Extract(ZipArchive archive, String directoryName) in C:\\Kudu Files\\Private\\src\\master\\Kudu.Core\\Infrastructure\\ZipArchiveExtensions.cs:line 130\r\n
...[생략]...at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}* Connection #0 to host jpublic.scm.azurewebsites.net left intact


확장 모듈을 담은 .zip 파일에 "/SiteExtensions/...[extension name]..."과 같은 디렉터리 구조를 담지 않고 있기 때문입니다. 따라서 그 디렉터리를 고려해 다시 압축한 후 업로드하면 정상적으로 설치가 됩니다.




이 글을 실습하면서 zip 파일 압축 단계에서 다음과 같은 오류가 발생할 수 있습니다.

2>Transformed Web.config using E:\TestWebApp\Web.Release.config into obj\Release\TransformWebConfig\transformed\Web.config.
2>Copying all files to temporary location below for package/publish:
2>obj\Release\Package\PackageTmp.
2>7zipping files...
2>powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('...', '...'); }"
2>Exception calling "CreateFromDirectory" with "2" argument(s): "The process cannot access the file 'E:\TestWebApp\bin\Release\Publish\TestWebApp.zip' because it is being used by another process."
2>At line:1 char:53
2>+ ... ileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('E:\...
2>+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2>    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
2>    + FullyQualifiedErrorId : IOException
2> 
2>Publishing folder /...
2>Publishing folder bin...
2>Publishing folder bin/roslyn...
2>Publishing folder Content...
2>Publishing folder fonts...
2>Publishing folder Scripts...
2>Publishing folder Views...
2>Publishing folder Views/Home...
2>Publishing folder Views/Shared...
2>Web App was published successfully file:///E:/TestWebApp/bin/Release/Publish/SiteExtensions/TestWebApp
2>

이건 zip 파일이 생성되는 폴더가 압축을 하려는 폴더와 같은 경우에 발생합니다. 즉, 다음과 같은 식으로 설정하고 빌드하면,

<PropertyGroup>
    <MyZipFilePath>$(ProjectDir)$(PublishUrl)\..\..\$(TargetName).zip</MyZipFilePath>
    <MyPublishFolderPath>$(ProjectDir)$(PublishUrl)\..\..</MyPublishFolderPath>
</PropertyGroup>

<Target Name="MyPublish" AfterTargets="GatherAllFilesToPublish">
    
    <Delete Files="$(MyZipFilePath)" />
      
    <Exec Command="powershell.exe -nologo -noprofile -command &quot;&amp; { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('$(MyPublishFolderPath)', '$(MyZipFilePath)'); }&quot;"/>

</Target>

압축 대상 및 그것의 zip 파일이 "$(ProjectDir)$(PublishUrl)\..\.." 폴더를 대상으로 했기 때문에 "The process cannot access the file..." 오류가 발생하는 것입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 6/21/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2018-10-11 02시16분
[초보애저] 항상 감사드립니다.
*_SCM.log 에서 확인가능하다고 하셨는데요. *Main.log라는 것도 있던데 이것은 무엇을 의미하는지 알수 있을까요?
SCM log를 통해 적용이 확인되면, *Main.log가 없더라도 자동으로 실제 Main에도 적용이 되었다고보면 되는 것인가요?
[guest]
2018-10-11 05시02분
이 글에서 설명한 것은 *_SCM.log입니다. 해당 로그 파일이 생성되는 LogFiles 폴더는 다양한 로그들이 쌓이는 것이므로 *Main.log가 어떤 역할을 하는지는 그 내용을 보시고 판단하면 됩니다.
정성태

... 31  32  33  34  35  36  37  38  39  40  41  42  43  44  [45]  ...
NoWriterDateCnt.TitleFile(s)
12493정성태1/18/20218582.NET Framework: 1009. .NET 5에서의 네트워크 라이브러리 개선 (1) - HTTP 관련 [1]파일 다운로드1
12492정성태1/17/20217972오류 유형: 695. ASP.NET 0x80131620 Failed to bind to address
12491정성태1/16/20219557.NET Framework: 1008. 배열을 반환하는 C# COM 개체의 메서드를 C++에서 사용 시 메모리 누수 현상 [1]파일 다운로드1
12490정성태1/15/20219136.NET Framework: 1007. C# - foreach에서 열거 변수의 타입을 var로 쓰면 object로 추론하는 문제 [1]파일 다운로드1
12489정성태1/13/202110126.NET Framework: 1006. C# - DB에 저장한 텍스트의 (이모티콘을 비롯해) 유니코드 문자가 '?'로 보인다면? [1]
12488정성태1/13/202110344.NET Framework: 1005. C# - string 타입은 shallow copy일까요? deep copy일까요? [2]파일 다운로드1
12487정성태1/13/20218879.NET Framework: 1004. C# - GC Heap에 위치한 참조 개체의 주소를 알아내는 방법파일 다운로드1
12486정성태1/12/20219787.NET Framework: 1003. x64 환경에서 참조형의 기본 메모리 소비는 얼마나 될까요? [1]
12485정성태1/11/202110479Graphics: 38. C# - OpenCvSharp.VideoWriter에 BMP 파일을 1초씩 출력하는 예제파일 다운로드1
12484정성태1/9/202111150.NET Framework: 1002. C# - ReadOnlySequence<T> 소개파일 다운로드1
12483정성태1/8/20218352개발 환경 구성: 521. dotPeek - 훌륭한 역어셈블 소스 코드 생성 도구
12482정성태1/8/20219753.NET Framework: 1001. C# - 제네릭 타입/메서드에서 사용 시 경우에 따라 CS8377 컴파일 에러
12481정성태1/7/20219511.NET Framework: 1000. C# - CS8344 컴파일 에러: ref struct 타입의 사용 제한 메서드파일 다운로드1
12480정성태1/6/202112078.NET Framework: 999. C# - ArrayPool<T>와 MemoryPool<T> 소개파일 다운로드1
12479정성태1/6/20219448.NET Framework: 998. C# - OWIN 예제 프로젝트 만들기
12478정성태1/5/202111083.NET Framework: 997. C# - ArrayPool<T> 소개파일 다운로드1
12477정성태1/5/202113488기타: 79. github 코드 검색 방법 [1]
12476정성태1/5/202110148.NET Framework: 996. C# - 닷넷 코어에서 다른 스레드의 callstack을 구하는 방법파일 다운로드1
12475정성태1/5/202112727.NET Framework: 995. C# - Span<T>와 Memory<T> [1]파일 다운로드1
12474정성태1/4/202110284.NET Framework: 994. C# - (.NET Core 2.2부터 가능한) 프로세스 내부에서 CLR ETW 이벤트 수신 [1]파일 다운로드1
12473정성태1/4/20219073.NET Framework: 993. .NET 런타임에 따라 달라지는 정적 필드의 초기화 유무 [1]파일 다운로드1
12472정성태1/3/20219365디버깅 기술: 178. windbg - 디버그 시작 시 스크립트 실행
12471정성태1/1/20219833.NET Framework: 992. C# - .NET Core 3.0 이상부터 제공하는 runtimeOptions의 rollForward 옵션 [1]
12470정성태12/30/202010026.NET Framework: 991. .NET 5 응용 프로그램에서 WinRT API 호출 [1]파일 다운로드1
12469정성태12/30/202013618.NET Framework: 990. C# - SendInput Win32 API를 이용한 가상 키보드/마우스 [1]파일 다운로드1
12468정성태12/30/202010223Windows: 186. CMD Shell의 "Defaults"와 "Properties"에서 폰트 정보가 다른 문제 [1]
... 31  32  33  34  35  36  37  38  39  40  41  42  43  44  [45]  ...