57

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.

8
  • 1
    It would be more idiomatic to put html file to blob storage Commented Oct 3, 2017 at 6:02
  • Can I publish it with the function and then access it in the function in Visual Studio???
    – chuckd
    Commented Oct 3, 2017 at 6:04
  • Have your email template as a method inside the function itself and not as a separate file. Commented Oct 3, 2017 at 6:09
  • At least you can always create this file manually via Portal(View files -> Add) and access it by following path: D:\home\site\wwwroot\NameOfFunction\FileName.html Commented Oct 3, 2017 at 6:10
  • You can use blob input binding to access it. Upload has usual options, nothing specific to functions. Commented Oct 3, 2017 at 6:10

2 Answers 2

89
Answer recommended by Microsoft Azure Collective

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.

8
  • 1
    It works now, I had to make some other changes. Thanks
    – chuckd
    Commented Oct 3, 2017 at 19:53
  • 3
    Amazing answer, spent hours looking for this solution.
    – M0rty
    Commented Sep 18, 2018 at 9:23
  • 8
    Helpful answer thanks. A couple of potentially useful remarks if your helper function sits outside the main Azure Function class: You want type Microsoft.Azure.WebJobs.ExecutionContext and not System.Threading.ExecutionContext. Also, if you haven't already got context it in your Azure Function definition, do it like so: public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, ExecutionContext context)
    – golfalot
    Commented Oct 31, 2018 at 16:35
  • 2
    A link to the documentation github.com/Azure/azure-functions-host/wiki/…
    – golfalot
    Commented Oct 31, 2018 at 16:38
  • 3
    fyi - i was doing dependency injection inside my IWebJobsStartup class (which doesn't have access to ExecutionContext AFAIK) and wanted to load custom json configuration so the easiest way to obtain a working local path for me was by manipulating Assembly.GetExecutingAssembly().Location
    – alv
    Commented Nov 1, 2018 at 2:30
5

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
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.