我遇到了一个问题,我似乎无法解决。
我们有一个处理特定请求的api。当这些请求失败时,我们的api应该向某些人发送电子邮件,通知他们这个问题。
为此,我在项目中创建了一个新文件夹,并编写了一些.html文件(模板)用于发送电子邮件。根据调用的函数和可能发生的错误动态填充某些字段。
我已经在本地主机上对此进行了广泛的测试,在这里一切正常。将应用程序发布到我们的IIS后,问题就开始了。
我第一次呼叫控制器发送电子邮件时返回以下错误:
{
"ClassName": "System.IO.DirectoryNotFoundException",
"Message": "Could not find a part of the path 'C:\\inetpub\\wwwroot\\API\\HtmlTemplates\\LastCodeTemplate.html'.",
"Data": null,
"InnerException": null,
"HelpURL": null,
"StackTraceString": " at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)\r\n at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)\r\n at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize)\r\n at API.Core.Services.Service1.<GetLastCodeTemplate>d__5.MoveNext() in C:\\Users\\User\\Documents\\Projects\\API\\API\\Core\\Services\\Service1.cs:line 90\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at API.Core.Services.Service2.<SendProblemEmail>d__6.MoveNext() in C:\\Users\\User\\Documents\\Projects\\API\\API\\Core\\Services\\Service2.cs:line 101\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at API.Core.Services.Service3.<SendEmail>d__10.MoveNext() in C:\\Users\\User\\Documents\\Projects\\API\\API\\Core\\Services\\Service3.cs:line 311\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at API.Controllers.Controller2.<SendEmail>d__4.MoveNext() in C:\\Users\\User\\Documents\\Projects\\API\\API\\Controllers\\Controller2.cs:line 51",
"RemoteStackTraceString": null,
"RemoteStackIndex": 0,
"ExceptionMethod": null,
"HResult": -2147024893,
"Source": "System.Private.CoreLib",
"WatsonBuckets": null
}
从文件夹中获取模板的代码:
public async Task<string> GetLastCodeTemplate(string startDate, string endDate)
{
string body;
var logo = _config["Logo:logoUrl"];
var currentFolder = Environment.CurrentDirectory;
using (var reader = new StreamReader(currentFolder + @"\HtmlTemplates\LastCodeTemplate.html"))
{
body = await reader.ReadToEndAsync();
}
body = body.Replace("{startDate}", startDate);
body = body.Replace("{endDate}", endDate);
body = body.Replace("{logo}", logo);
return body;
}
错误很明显,api在指定位置找不到文件夹。我已经检查过了,我们的IIS上没有该文件夹。
所以现在来回答这个问题:
-
这是否是由于发布应用程序和发布文件中未包含的文件夹而导致的问题?
-
如果是,我将如何着手解决这个问题,因此文件夹和后续文件都包括在内?我不想手动创建文件夹和文件,因为我觉得这些应该包含在项目中
-
如果没有,那是什么原因呢?我能在取文件的时候找错地方吗?