Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

c# - Including a file when I publish my Azure function in Visual Studio

I know this seems like a simple thing but I can't find any help online.

I want to include a file (.html) along with my Azure function when I publish it using Visual Studio. Then I want to be able to access this file in my Azure function. Why? It seems like only the .dll gets sent to the server when I publish.

This file will be an .html file that will be an email template. I want to read it in my function and then send emails out.

Any help is much appreciated.

I see I can use [send grid in Azure functions][1], but it looks like I can only send out one email and not multiple emails, which is what I want.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, you need to add the html file to your project, and in the properties, set Copy to Output Directory to "Copy if newer".

Then in your function code, take in an additional ExecutionContext context parameter (note that this is Microsoft.Azure.WebJobs.ExecutionContext and not System.Threading.ExecutionContext). And when you need to access your html file, you can then write:

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

That's assuming you added the file at the root of your VS project. If you instead added it in some Data folder (better practice), you'd write:

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

See here for full working sample.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...