使用Webjobs时如何读取文件?
尝试这样做:
sudo /usr/local/mysql/support-files/mysql.server restart
但是在本地运行失败
答案 0 :(得分:3)
根据您的描述,本地:
我们可以使用以下代码来获取WebJob项目的根路径。
rootPath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
对于Azure:
我们共享 D:\home
,我们可以在此路径中读取或写入文件。有关主目录访问的更多详细信息,请参阅document。 Azure上的文件结构请参考另一个document。我们也可以从Kudu(http://yourwebsite.scm.azurewebsites.net/)工具中浏览它。
为方便我们的客户,沙箱在内核模式下实现动态符号链接,将d:\ home映射到客户主目录。这样做是为了消除客户在访问站点时继续引用自己的网络共享路径的需要。无论站点在何处运行,或者在VM上运行了多少站点,每个站点都可以使用
访问其主目录
rootPath = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot"
如果没有环境变量“Home”,我们可以使用以下代码来执行此操作。
string path;
if (Environment.GetEnvironmentVariable("HOME")!=null)
{
path = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\testfilename.txt";
}
else
{
path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\testfilename.txt";
}
以下是详细测试步骤:
1.在项目中创建一个WebJob项目和test.text文件和文件夹测试
2.我在WebJob中使用计时器触发器,所以我需要在program.cs中添加config.UseTimers()
3。在Function.cs文件中添加以下代码
public static void ProcessQueueMessage([TimerTrigger("00:00:03")] TimerInfo timerInfo, TextWriter log)
{
string instance = Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID");
string newMsg = $"WEBSITE_INSTANCE_ID:{instance}, timestamp:{DateTime.Now}";
string path;
if (Environment.GetEnvironmentVariable("HOME")!=null)
{
path = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot" + @"\test.txt";
}
else
{
path = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\test.txt";
}
string template = File.ReadAllText(path);
log.WriteLine($"NewMsge: {newMsg},file Content:{template}");
Console.WriteLine($"NewMsge: {newMsg},file Content:{template}");
}
4.在本地机器上测试。
5.部署到Azure并从Azure WebJob仪表板获取日志。
6.部署到Azure并从Azure WebJob仪表板获取日志。