0

Is there a better way to call up and get my email templates from html files in my solution?

I'm calling 'AppDomain.CurrentDomain.BaseDirectory' from a Azure function project and my ASP.Net MVC project to get a path to my email templates. (.html files)

But the call made from the Azure function doesn't even return a path in the solution!

Simply speaking, the ASP.Net MVC and Azure function project both call EmailService.cs in another project named projectName.Services to construct the emails.

From both projects I'm calling the EmailService.cs like this

_emailService.CancelEventSendHostEmail(value1, someObject, anotherObject, "someMessage");

and in 'CancelEventSendHostEmail' I'm doing something like this

CancelEventSendHostEmail(/*incoming properties*/) {

  var outerTemplateHtml = GetEmailTemplate("email-outer-body-template.html");
  var specificTemplate= GetEmailTemplate("someTemplate.html");
  // do some work to fill in values in the email
  // send the email
}

now GetEmailTemplate looks like this

public StringBuilder GetEmailTemplate(string templateName)
{
    string htmlAsString 
            = File.ReadAllText(Path.Combine(Directory.GetParent(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).ToString()).ToString(), 
            @"Yogabandy2017.Services\EmailingTemplates", templateName));
        return new StringBuilder(htmlAsString);
 }

Now this works fine with the ASP.Net MVC project to find, get and read in the HTML template file as a string. EmailService.cs finds file no problem from the ASP.Net MVC project, but when I call 'GetEmailTemaplate' from my Azure function the

AppDomain.CurrentDomain.BaseDirectory

path returns a path that isn't in the dirrectory of the project or solution.

It returns a path that looks like this

C:\Users\chuckdawit\AppData\Local\AzureFunctionsTools\Releases\1.2.0\cli\

where as if I'm in the asp.net mvc app and I read in the text 'AppDomain.CurrentDomain.BaseDirectory' returns a path like this

C:\Users\chuckdawit\Source\Workspaces\YogaBandy2017\YogaBandy2017\YogaBandy2017\

where I can then search for the html email template file in a folder in that project!

enter image description here

chuckd
  • 13,460
  • 29
  • 152
  • 331

2 Answers2

3

There is API to get the current folder of a Function:

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context)
{
    var message = $"You are in {context.FunctionDirectory}";
    return req.CreateResponse(System.Net.HttpStatusCode.OK, message );
}

See documentation:

FunctionDirectory: Provides the current function directory (e.g. when running on Azure, d:\home\site\wwwroot\HttpTrigger1)

Note that locally it's going to be inside bin\debug. Be careful to plan how you will deploy Email Templates relative to Function folder.

A sensible alternative is to deploy the templates to Blob Storage and then use input bindings to load them into Function with no code required.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • but will this work the same way once I publish it to Azure and it's running up in the 'cloud'!? here is what I'm actually needing to do. Path.GetFullPath(Path.Combine(context.FunctionDirectory, @"..\..\..\..\..\")); this gets me to the next folder where my email templates are located – chuckd Jun 14 '18 at 07:28
  • @user1186050 Azure will only have binaries deployed, it won't preserve your C# solution structure, so you need to copy templates to `bin` folder as part of build, instead of `..\..\..` trickery. – Mikhail Shilkov Jun 14 '18 at 07:33
  • which bin folder? I see 'projectName\bin\Debug\net462\bin' I assume when I publish in release mode I'll see the Debug replaced with Release? So do I copy to the project/bin folder or farther down in the file path past the net462/bin folder? – chuckd Jun 14 '18 at 07:38
  • 1
    @user1186050 Just mark files as `Content` and `Copy to Output Directory` in file properties, Visual Studio should do this for you – Mikhail Shilkov Jun 14 '18 at 07:40
  • cool thanks! this helps me a lot! Should I do the same thing for my ASP.Net MVC project too or just the Azure function? – chuckd Jun 14 '18 at 07:44
1

To get the current folder of the function, we can do as below:

string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Screenshot of result:

enter image description here

Lee Liu
  • 1,981
  • 1
  • 12
  • 13