如何在使用Webjobs时读取文件?webjob在本地和在生产中读取文件
试图做到这一点:
using (StreamReader sr = new StreamReader(VirtualPathProvider.OpenFile("~/content/file/file.txt")))
{
template = sr.ReadToEnd();
}
但本地运行失败
如何在使用Webjobs时读取文件?webjob在本地和在生产中读取文件
试图做到这一点:
using (StreamReader sr = new StreamReader(VirtualPathProvider.OpenFile("~/content/file/file.txt")))
{
template = sr.ReadToEnd();
}
但本地运行失败
根据你的描述,对局部:
我们可以使用下面的代码来获取WebJob项目的根路径。
rootPath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory()));
为天青:
D:\home
为我们分享,我们可以阅读或在此路径写入文件。有关主目录访问的更多详细信息,请参阅document。 Azure上的文件结构请参考另一个document。我们也可以从Kudu(http://yourwebsite.scm.azurewebsites.net/)工具中浏览它。
为了方便我们的客户,沙箱在内核模式下实现了动态符号链接,该链接将d:\ home映射到客户主目录。这样做是为了消除客户在访问网站时继续参考他们自己的网络共享路径的需要。在站点运行,或有多少网站在虚拟机上运行没有问题,如果没有环境变量“家”,我们可以使用下面的代码来做到这一点每个人都可以访问使用
rootPath = Environment.GetEnvironmentVariable("HOME") + @"\site\wwwroot"
自己的主目录。
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.As我用定时器因此我需要在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.在本地计算机上进行测试。
画完部署到Azure中,并从Azure的WebJob仪表盘日志。
6.After部署到Azure中,并从Azure的WebJob仪表盘日志。