在本地调试Azure功能为AppDomain.CurrentDomain.BaseDirectory

时间:2018-06-14 06:43:46

标签: c# asp.net-mvc azure azure-functions azure-functions-runtime

有没有更好的方法从我的解决方案中的html文件中调用和获取我的电子邮件模板?

我正在打电话给AppDomain.CurrentDomain.BaseDirectory'从Azure功能项目和我的ASP.Net MVC项目获取我的电子邮件模板的路径。 (.html文件)

但是Azure函数调用甚至不会在解决方案中返回路径!

简单来说,ASP.Net MVC和Azure功能项目都在另一个名为projectName.Services的项目中调用EmailService.cs来构建电子邮件。

在这两个项目中,我都像这样调用EmailService.cs

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

并在' CancelEventSendHostEmail'我正在做这样的事情

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
}

现在GetEmailTemplate看起来像这样

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

现在,这可以与ASP.Net MVC项目一起使用,以查找,获取和读取HTML模板文件中的字符串。 EmailService.cs从ASP.Net MVC项目中找到文件没问题,但是当我打电话给GetEmailTemaplate'来自我的Azure功能

  

AppDomain.CurrentDomain.BaseDirectory

路径返回项目或解决方案的目录中的路径。

它返回一个看起来像这样的路径

  

C:\用户\ chuckdawit \应用程序数据\本地\ AzureFunctionsTools \唱片集\ 1.2.0 \ CLI \

好像我在asp.net mvc应用程序中,我在文本中读到了AppDomain.CurrentDomain.BaseDirectory'返回这样的路径

  

C:\用户\ chuckdawit \源\工作区\ YogaBandy2017 \ YogaBandy2017 \ YogaBandy2017 \

然后我可以在该项目的文件夹中搜索html电子邮件模板文件!

enter image description here

2 个答案:

答案 0 :(得分:2)

有API来获取函数的当前文件夹:

UIScrollView

请参阅documentation

  

public static HttpResponseMessage Run(HttpRequestMessage req, ExecutionContext context) { var message = $"You are in {context.FunctionDirectory}"; return req.CreateResponse(System.Net.HttpStatusCode.OK, message ); } :提供当前的功能目录(例如,在Azure上运行时,d:\ home \ site \ wwwroot \ HttpTrigger1)

请注意,本地它将在FunctionDirectory内。请务必规划如何部署相对于Function文件夹的电子邮件模板。

一个明智的选择是将模板部署到Blob存储,然后使用输入绑定将它们加载到Function中,而无需代码。

答案 1 :(得分:0)

要获取该函数的当前文件夹,我们可以执行以下操作:

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

结果截图:

enter image description here