Including a file when I publish my azure function

2019-02-21 17:34发布

问题:

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.

回答1:

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.



回答2:

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

We also could sent multiple emails with Azure function during we use the SendGrid. We could use ICollector as output. We could use the Microsoft.Azure.WebJobs.Extensions.SendGrid SDK to do that.

The following is demo code.

using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;

namespace FunctionApp1
{
    public static class TestSendGrid
    {
        [FunctionName("TestSendGrid")]
        public static void Run([TimerTrigger("0 0 */1 * * *")]TimerInfo myTimer, TraceWriter log, [SendGrid(ApiKey = "sendGridKey", From ="send email")]ICollector<Mail> mails)
        {

            log.Info($"C#  function executed at: {DateTime.Now}");

            for (int i = 0; i < number; i++) // you  could use your own logic
            {
                Mail message = new Mail()
                {
                    Subject = $"multiple mails from SendGrid with Azure function !"
                };

                var personalization = new Personalization();
                personalization.AddTo(new Email("sunguiguan@hotmail.com"));

                Content content = new Content
                {
                    Type = "text/plain",
                    Value = $"Hello Azure SendGrid!{i}"
                };

                message.AddContent(content);
                message.AddPersonalization(personalization);
                mails.Add(message);
            }

        }
    }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "xxxx",
    "AzureWebJobsDashboard": "xxxx",
    "sendgridapikey": "xxxxx" 
  }
}