简体   繁体   中英

Debugging Azure function locally gives a funny path to AppDomain.CurrentDomain.BaseDirectory

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!

在此处输入图片说明

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 (eg 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.

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

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

Screenshot of result:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM