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.
Use the following steps to publish your project to a function app in Azure. In Solution Explorer, right-click the project and select Publish. In Target, select Azure then Next. Select Azure Function App (Windows) for the Specific target, which creates a function app that runs on Windows, and then select Next.
Use cloud-based function In Visual Studio Code, create a bash file named upload-azure.sh and copy the following code into the file. In Visual Studio Code, select the Azure explorer, then expand the node for your Function app, then expand Functions. Right-click the function name, upload and select Copy Function Url.
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.
I have the same scenario as you have. However, I cannot access ExecutionContext
because it is only available in requests. My scenario needs to get the template included in AzFunc project but not in the context of AzFunc's functions. I got it null
when I go with the interface - implementation class approach.
Thanks to this guy, I use IOptions<ExecutionContextOptions>
in my class to get the root directory of the Azure Func.
My Azure Func project (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... } } }
My different project defines the interface and uses it there, independently of the real implementation
namespace DifferentProject { public interface ITemplateHelper { string GetTemplate(); // Use this to get the template } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With