sourcetip

Visual Studio에서 Azure 기능을 게시할 때 파일 포함

fileupload 2023. 4. 23. 11:03
반응형

Visual Studio에서 Azure 기능을 게시할 때 파일 포함

간단한 일인 것 같지만 온라인에서는 어떤 도움도 찾을 수 없습니다.

Visual Studio를 사용하여 파일을 게시할 때 Azure 기능과 함께 파일(.html)을 포함시키고 싶습니다.그럼 내 Azure 기능으로 이 파일에 접근할 수 있으면 좋겠는데, 왜?게시할 때 .dll만 서버로 전송되는 것 같습니다.

이 파일은 이메일 템플릿이 되는 .html 파일입니다.제 기능에서 읽고 나서 메일을 보내고 싶어요.

어떤 도움이라도 감사합니다.

과연, [Azure 함수의 송신 그리드][1]을 사용할 수 있습니다만, 1개의 메일밖에 송신할 수 없고, 복수의 메일은 송신할 수 없는 것 같습니다.

먼저 html 파일을 프로젝트에 추가해야 하며 속성에서 Copy to Output Directory를 "Copy if new"로 설정합니다.

그런 다음 기능 코드에 추가 정보를 입력합니다.ExecutionContext contextparameter(이것은,Microsoft.Azure.WebJobs.ExecutionContext 아니라 System.Threading.ExecutionContexthtml 파일에 액세스 할 필요가 있는 경우는, 다음과 같이 기입할 수 있습니다.

string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "test.html");

즉, VS 프로젝트의 루트에 파일을 추가했다고 가정합니다.대신 일부에 추가한 경우Data폴더(better practice)는 다음과 같이 적습니다.

string htmlFilePath = Path.Combine(context.FunctionAppDirectory, "Data", "test.html");

풀가동 샘플은 여기를 참조해당 샘플은 이쪽입니다.

저도 당신과 같은 시나리오를 가지고 있습니다.다만, 액세스 할 수 없습니다.ExecutionContext요청에서만 사용할 수 있기 때문입니다.내 시나리오에서는 템플릿을 AzFunc 프로젝트에 포함시켜야 하지만 AzFunc의 기능 컨텍스트에 포함해서는 안 됩니다.알겠어요.nullinterface - implementation class 접근방식을 채택할 경우.
친구 덕분에 저는IOptions<ExecutionContextOptions>Azure Func의 루트디렉토리를 취득하기 위해서요

My Azure Func 프로젝트 (NET 6, Azure Function v4)

using Microsoft.Extensions.Options;
using Microsoft.Azure.WebJobs.Host.Bindings;
namespace AzureFuncApi
{
    public class TemplateHelper : ITemplateHelper
    {
        private readonly IOptions<ExecutionContextOptions> _executionContext;
        public TemplateHelper (IOptions<ExecutionContextOptions> executionContext)
        {
            _executionContext = executionContext;
        }
        public string GetTemplate()
        {
            var context = _executionContext.Value;
            var rootDir = context.AppDirectory; // <-- rootDir of AzFunc
            var template = Path.Combine(rootDir, "test.html"); // <-- browse for your template. Here's an example if you place test.html right in the root of your project
            // return your template here, raw, or after you do whatever you want with it...
        }
    }
}

실제 구현과는 별도로 다른 프로젝트에서 인터페이스를 정의하고 거기서 사용합니다.

namespace DifferentProject
{
    public interface ITemplateHelper
    {
        string GetTemplate(); // Use this to get the template
    }
}

언급URL : https://stackoverflow.com/questions/46537758/including-a-file-when-i-publish-my-azure-function-in-visual-studio

반응형